Threads and lifecycle question 3 of 180 mid
What actually happens when you call interrupt() on a thread?
- The thread is terminated immediately
- Correct: A flag is set, and any blocking call that is interruptible throws
InterruptedExceptionand clears the flag - The thread moves to BLOCKED
- Nothing, unless the thread is inside
synchronized
interrupt() sets an internal flag. If the thread is parked in an interruptible call such as
sleep, wait, join, BlockingQueue.take or Lock.lockInterruptibly, the call throws
InterruptedException and the flag is cleared as part of throwing. If the thread is running, nothing
observable happens until it checks Thread.interrupted() or isInterrupted(), or reaches a blocking
call.
Some blocking is not interruptible: acquiring synchronized, Lock.lock(), and most plain socket or
file I/O. InterruptibleChannel is the exception, closing itself and throwing
ClosedByInterruptException.