It is ambiguous which thread will wake up when notify()
is called.
There is no way to specify which thread will be awakened by calls to notify()
.
Example 1: In the following code, notifyJob()
calls notify()
.
public synchronized notifyJob() {
flag = true;
notify();
}
...
public synchronized waitForSomething() {
while(!flag) {
try {
wait();
}
catch (InterruptedException e)
{
...
}
}
...
}
wait()
, but it is possible that notify()
will notify a different thread than the intended one. [1] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 373
[2] Sun Microsystems, Inc. Java Sun Tutorial - Concurrency
[3] Sun Microsystems, Inc. Java Sun Tutorial - Concurrency