Java Reference
In-Depth Information
There is nothing in the API or language specification that ties interruption to any specific
cancellation semantics, but in practice, using interruption for anything but cancellation is fra-
gile and difficult to sustain in larger applications.
Each thread has a boolean
interruptedstatus
; interrupting a thread sets its interrupted status to
true.
Thread
contains methods for interrupting a thread and querying the interrupted status
and
isInterrupted
returns the interrupted status of the target thread. The poorly named
static
interrupted
method
clears
the interrupted status of the current thread and returns
its previous value; this is the only way to clear the interrupted status.
Blocking library methods like
Thread.sleep
and
Object.wait
try to detect when a
thread has been interrupted and return early. They respond to interruption by clearing the in-
terrupted status and throwing
InterruptedException
, indicating that the blocking op-
eration completed early due to interruption. The JVM makes no guarantees on how quickly a
blocking method will detect interruption, but in practice this happens reasonably quickly.