Threads that invoke Object.wait()
expect to wake up and resume execution when their condition predicate becomes true. To be compliant with rule THI03-J. Always invoke wait() and await() methods inside a loop, waiting threads must test their condition predicates upon receiving notifications and must resume waiting if the predicates are false.
The notify()
and notifyAll()
methods of package java.lang.Object
are used to wake up waiting thread(s). These methods must be invoked from a thread that holds the same object lock as the waiting thread(s); these methods throw an IllegalMonitorStateException
when invoked from any other thread. The notifyAll()
method wakes up all threads associated with waiting on an object lock and allows threads whose condition predicate is true to resume execution. Furthermore, if all the threads whose condition predicate evaluates to true previously held a specific lock before going into the wait state, only one of them will reacquire the lock upon being notified. Presumably, the other threads will resume waiting. The notify()
method wakes up only one thread, and any with no guarantee regarding which specific thread is notified. The chosen thread is permitted to resume waiting if its condition predicate is unsatisfied; , and this often defeats the purpose of the notification.
...
These conditions are satisfied by threads that are identical and provide a stateless service or utility.
The java.util.concurrent.locks
utilities (Condition
interface) provide the Condition.signal()
and Condition.signalAll()
methods to awaken threads that are blocked on an a Condition.await()
call. Condition
objects are required when using java.util.concurrent.locks.Lock
objects. A Lock
object allows the use of Object.wait()
and notify, Object.notify()
, and Object.notifyAll()
methods. However, code that synchronizes using , however this is prohibited by rule "LCK03-J. Do not synchronize on the intrinsic locks of high-level concurrency objects". However, code that synchronizes using a Lock
object uses one or more Condition
objects associated with the Lock
object rather than using its own intrinsic lock. These objects interact directly with the locking policy enforced by the Lock
object. Consequently, the Condition.await()
, Condition.signal()
, and Condition.signalAll()
methods are used in place of the Object.wait()
, Object.notify()
, and Object.notifyAll()
methods.
Use of the The signal()
method is insecure when multiple threads await the same Condition
object unless the following must not be used unless all of these conditions are met:
- The
Condition
object is identical for each waiting thread. - All threads must perform the same set of operations after waking up. This means that any one thread can be selected to wake up and resume for a single invocation of
signal()
. - Only one thread is required to wake upon receiving the signal.
Insecure use of the signal()
method is forbidden.
- signal.
or all of these Use of the signal()
method is also permitted when both of the following conditions are met:
- Each thread uses a unique
Condition
object. - Each
Condition
object is associated with a commonLock
object.
The When used securely, the signal()
method has better performance than signalAll()
when used securely.
Noncompliant Code Example (notify()
)
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="01c2bf0d5554f063-2e75f6f0-46594d5f-8810b4eb-87b183ae27d3362f38b97feb"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d99684d5dd7878d9-65b112be-40594095-8dceba7c-9e80f847328f24ed1c9a4406"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Chapter 17, Threads and Locks | http://java.sun.com/docs/books/jls/third_edition/html/memory.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="810fdbf0072cb7ca-1ebaca0d-4b3b4976-be6aa82b-f9690b5651841349c46d865c"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Section 14.2.4, Notification | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b4922e69e98f5e13-90bbe08a-4bf04ec9-a023bd53-3d23044720f21d56cfaa3838"><ac:plain-text-body><![CDATA[ | [[Bloch 2001 | AA. Bibliography#Bloch 01]] | Item 50: Never invoke wait outside a loop | ]]></ac:plain-text-body></ac:structured-macro> |
...