...
Code Block |
---|
private Vector vector; //... public void consumeElement() throws InterruptedException { synchronized (vector) { while (vector.isEmpty()) { vector.wait(); } // ConsumeResume 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 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.
...
- 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 TOCTOU race condition.
- Malicious notification — A random or malicious notification can be received when the condition predicate is false. Such a notification would cancel the
wait()
. - 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 Spurious wakeups --- Certain JVM implementations are vulnerable to spurious wakeups that result in waiting threads waking up even without a notification \[[API 2006|AA. Bibliography#API 06]\].
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2ad105ce9cbb759f-e1ec83a2-435e4178-a537bd1e-c611f5490b324bd6fc9241e1"><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="cc916948bdee0b5d-11f208ef-403a4500-834a9a30-f30ba2de43384fe5876cc208"><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="baa26f8d69720438-37751bdd-4606491b-aa498c73-429d6dc40869802906e531ab"><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="105e1b3d609b77fe-bc5925a0-4c64418b-85a3b443-4d481598c083dad7358b4829"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Section 14.2, Using Condition Queues | ]]></ac:plain-text-body></ac:structured-macro> |
...