Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: wordsmithing

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 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 correct way to invoke the wait() method when the vector is empty is shown below.

...

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 the wait/notify mechanism.

...

Code Block
bgColor#ccccff
synchronized (object) {
  while (<condition does not hold>) {
    object.wait();
  }
  // Proceed when condition holds
}

Invocations of the await() method of the java.util.concurrent.locks.Condition interface .await() method must also be enclosed in a similar loop.

Risk Assessment

To guarantee liveness and safety, Failure to encase the wait() and or await() methods must always be invoked inside a while loop can lead to indefinite blocking and denial of service.

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="90b22ca95b5fdcec-bd3211ee-41924079-a1f08d8c-f38c2ccb6a663730c293f15e"><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="ae1ac1410fe3ee5b-0c2c13cc-4ac84040-ab408136-eb5292cffa4cc1be543bfa84"><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>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8f3433f1cc6b6b59-f934857c-4c8042a4-99b28ddf-eec01302534708e7ef960aa8"><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="565075b064279274-f69d7254-43d9497b-9cf4a6b5-9a59fea427e06faa24f0dd88"><ac:plain-text-body><![CDATA[

[[Goetz 2006

AA. Bibliography#Goetz 06]]

Section 14.2, Using Condition Queues

]]></ac:plain-text-body></ac:structured-macro>

...