Monday, March 15, 2010

Can wait be resume without Notify??

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 (someCondition()==false)

obj.wait(timeout);

... // Perform action appropriate to condition

}

No comments:

Post a Comment