The Object.wait()
method temporarily cedes possession of a lock so that other threads that may be requesting the lock can proceed. Object.wait()
must always be called from a synchronized block or method. The waiting thread resumes execution only after it has been notified, generally due to as the result of the invocation of the notify()
or notifyAll()
method by some other thread. The wait()
method must be invoked from a loop that checks whether a condition predicate holds. Note that a condition predicate is the negation of the condition expression in the loop. For example, the condition predicate for removing an element from a vector is !isEmpty()
, whereas the condition expression for the while loop condition is isEmpty()
. The Following is the correct way to invoke the wait()
method when the vector is empty is shown below.
Code Block |
---|
private Vector vector; //... public void consumeElement() throws InterruptedException { synchronized (vector) { while (vector.isEmpty()) { vector.wait(); } // Consume when condition holds } } |
The notification mechanism notifies the waiting thread and allows it to check its condition predicate. The invocation of notify()
or notifyAll()
in another thread cannot precisely determine which waiting thread will be resumed. Condition predicate statements allow notified threads to determine whether they should resume upon receiving the notification. Condition predicate predicates are also useful when a thread is required to block until a condition becomes true, for example, when waiting for data to arrive on an input stream before reading the data.
...
Wiki Markup |
---|
To guarantee safety, programs must test the {{while}} loop condition after returning from the {{wait()}} method. Although {{wait()}} is intended to block indefinitely until a notification is received, it must still be encased within a loop to prevent the following vulnerabilities \[[Bloch 2001|AA. Bibliography#Bloch 01]\]: |
- thread Thread in the middle — A third thread can acquire the lock on the shared object during the interval between a notification being sent and the receiving thread resuming execution. This third thread can change the state of the object, leaving it inconsistent. This is a time-of-check-to-time-of-use ( TOCTOU ) condition.
- malicious Malicious notification — A random or malicious notification can be received when the condition predicate is false. Such a notification would cancel the
wait()
. - misdelivered Misdelivered notification — The order in which threads execute after receipt of a
notifyAll()
signal is unspecified. Consequently, an unrelated thread could start executing and discover that its condition predicate is satisfied. Consequently, it could resume execution, although it was required to remain dormant. Wiki Markup spuriousSpurious wake-upswakeups --- Certain JVM implementations are vulnerable to spurious wake-upswakeups that result in waiting threads waking up even without a notification \[[API 2006|AA. Bibliography#API 06]\].
...
New code should use the java.util.concurrent.locks
concurrency utilities in place of the wait/notify mechanism. However, legacy code that complies with the other requirements of this rule is permitted to depend upon on the wait/notify mechanism.
...
This noncompliant code example invokes the wait()
method inside a traditional if
block and fails to check the post-condition postcondition after the notification is received. When If the notification was were accidental or malicious, the thread could wake up prematurely.
...
Failure to encase the wait()
or await()
methods inside a while
loop can lead to indefinite blocking and denial of service (DoS).
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
THI03-J | low | unlikely | medium | P2 | L3 |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b1b64a569d3947e6-0d8f01a0-4add4c95-b21696b5-c2bd707c8cbc9762eaf6b9fc"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [Class Object | http://java.sun.com/javase/6/docs/api/java/lang/Object.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="07c9041fb1592e89-89683a5d-49154b16-813da4be-0a3c19fda8d25862654e982d"><ac:plain-text-body><![CDATA[ | [[Bloch 2001 | AA. Bibliography#Bloch 01]] | Item 50: . Never invoke | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0b57c5b2728d1d8b-0bed48b7-4f6049e9-b8f79c77-bfaf51c5d15c65f92c8d5a53"><ac:plain-text-body><![CDATA[ | [[Lea 2000 | AA. Bibliography#Lea 00]] | 3.2.2, Monitor Mechanics, ; 1.3.2, Liveness | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="61787605855fdaed-ca5dc228-4e5549ee-96d89692-c8b989ce7dbdc05ebf5a8d74"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Section 14.2, Using Condition Queues | ]]></ac:plain-text-body></ac:structured-macro> |
...