A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious
wakeup. While this will rarely occur in practice, applications must guard against it by testing for
the condition that should have caused the thread to be awakened, and continuing to wait if the
condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) {
while (<condition does not hold>![]()
obj.wait(timeout);
... // Perform action appropriate to condition
}
Steve
In above prgm i am trying to create two thread to wait on one object and when notify is called, than only one of the two waiting thread should get active. but when i run this prgm both of the two waiting threads run.
Originally posted by Abhijeet Nalawade:
I actually have written a code on similar lines as written by Vijay and it is functioning as expected..But i am not able to figure out the change in Vijay's code in order to function on similar lines.
Steve
A thread that calls wait()
releases the virtual CPU; at the same time, it releases the lock. It enters a pool of waiting threads,
which is managed by the object whose wait() method got called. Every object has such a pool.
...
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|