Any thread that invokes wait()
expects to wake up and resume execution when some condition predicate becomes true. As recommended by CON18-J. Always invoke wait() and await() methods inside a loop, waiting threads should test their condition predicates upon receiving notifications and resume waiting if they evaluate are false (that is, the condition expression in the loop evaluates to true).
The methods notify()
and notifyAll()
of package java.lang.Object
are used to waken waiting thread(s). These methods must be invoked from code that holds the same object lock as the waiting thread(s). The notifyAll()
method wakes up all threads and allows ones whose condition predicate evaluates to falseis true (loop expression is false), to resume execution. Furthermore, if all the threads whose condition predicate now evaluates to false true held a specific lock before going into wait state, only one of them will reacquire the lock upon being notified, and the others may, presumably, resume waiting. The notify()
method wakes up only one thread, and makes no guarantees as to which thread is notified. If the thread's condition predicate doesn't allow, the chosen thread may not awaken, defeating the purpose of the notify()
call.
...
- Every condition predicate in every waiting thread would evaluate to false be true (condition expressions in loops will be false) if a notification were received by each, independently. Furthermore, all these threads must perform the same set of operations after waking up. In other words, any one thread can be selected to wake up and resume for a single invocation of
notify()
. - Only one thread is required to wake up on the notify signal. This is contingent on the condition predicate, in that, only one predicate must fulfill the condition and allow the thread to proceed. Multiple condition predicates in the same statement should be avoided.
- No untrusted code has access to the object being waited on. If untrusted code has access to this object, it can
wait()
on the object and intercept anotify()
call.
...