Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Spurios wakeups of Object.wait() ???

843829Aug 16 2007 — edited Aug 17 2007
Hi all,

I've been very surprised when I knew a strange behavior of wait() function. The documentation of Object.wait() says:
Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
But below we can read:
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait();
         ... // Perform action appropriate to condition
     }
Does it mean that all the code that I wrote without condition loop is unreliable???

Could anyone familiar with a VM explain the cases when a spurious wakeup may occur?

Why not the logic above (condition loop) was implemented inside the wait() functions?

Anton
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 14 2007
Added on Aug 16 2007
3 comments
640 views