Java Reference
In-Depth Information
For example, if a thread
t
is in the wait set for
m
, and then both an interrupt of
t
and a notification of
m
occur, there must be an order over these events. If the in-
terrupt is deemed to have occurred first, then
t
will eventually return from
wait
by throwing
InterruptedException
, and some other thread in the wait set for
m
(if
any exist at the time of the notification) must receive the notification. If the no-
tification is deemed to have occurred first, then
t
will eventually return nor-
mally from
wait
with an interrupt still pending.
3.
Thread
t
performs
n
lock actions on
m
.
4.
If thread
t
was removed from
m
's wait set in step 2 due to an interrupt, then
t
's
interruption status is set to false and the
wait
method throws
InterruptedException
.
17.2.2. Notification
Notification actions occur upon invocation of methods
notify
and
notifyAll
.
Let thread
t
be the thread executing either of these methods on object
m
, and let
n
be the
number of lock actions by
t
on
m
that have not been matched by unlock actions. One of the
following actions occurs:
• If
n
is zero, then an
IllegalMonitorStateException
is thrown.
This is the case where thread
t
does not already possess the lock for target
m
.
• If
n
is greater than zero and this is a
notify
action, then if
m
's wait set is not empty,
a thread
u
that is a member of
m
's current wait set is selected and removed from
the wait set.
There is no guarantee about which thread in the wait set is selected. This removal
from the wait set enables
u
's resumption in a wait action. Notice, however, that
u
's
lock actions upon resumption cannot succeed until some time after
t
fully unlocks
the monitor for
m
.
• If
n
is greater than zero and this is a
notifyAll
action, then all threads are removed
from
m
's wait set, and thus resume.
Notice, however, that only one of them at a time will lock the monitor required
during the resumption of wait.
17.2.3. Interruptions
Interruption actions occur upon invocation of
Thread.interrupt
, as well as methods defined to
invoke it in turn, such as
ThreadGroup.interrupt
.
Let
t
be the thread invoking
u
.interrupt
, for some thread
u
, where
t
and
u
may be the same.
This action causes
u
's interruption status to be set to true.