Java Reference
In-Depth Information
Additionally, if there exists some object
m
whose wait set contains
u
, then
u
is removed
from
m
's wait set. This enables
u
to resume in a wait action, in which case this wait will,
after re-locking
m
's monitor, throw
InterruptedException
.
Invocations of
Thread.isInterrupted
can determine a thread's interruption status. The
static
method
Thread.interrupted
may be invoked by a thread to observe and clear its own interrup-
tion status.
17.2.4. Interactions of Waits, Notification, and Interruption
The above specifications allow us to determine several properties having to do with the in-
teraction of waits, notification, and interruption.
If a thread is both notified and interrupted while waiting, it may either:
• return normally from
wait
, while still having a pending interrupt (in other words, a
call to
Thread.interrupted
would return true)
• return from
wait
by throwing an
InterruptedException
The thread may not reset its interrupt status and return normally from the call to
wait
.
Similarly, notifications cannot be lost due to interrupts. Assume that a set
s
of threads is in
the wait set of an object
m
, and another thread performs a
notify
on
m
. Then either:
• at least one thread in
s
must return normally from
wait
, or
• all of the threads in
s
must exit
wait
by throwing
InterruptedException
Note that if a thread is both interrupted and woken via
notify
, and that thread returns from
wait
by throwing an
InterruptedException
, then some other thread in the wait set must be noti-
fied.
17.3. Sleep and Yield
Thread.sleep
causes the currently executing thread to sleep (temporarily cease execution) for
the specified duration, subject to the precision and accuracy of system timers and sched-
ulers. The thread does not lose ownership of any monitors, and resumption of execution
will depend on scheduling and the availability of processors on which to execute the thread.
It is important to note that neither
Thread.sleep
nor
Thread.yield
have any synchronization se-
mantics. In particular, the compiler does not have to flush writes cached in registers out to
shared memory before a call to
Thread.sleep
or
Thread.yield
, nor does the compiler have to re-
load values cached in registers after a call to
Thread.sleep
or
Thread.yield
.