[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[equinox-dev] Object.wait and spurious wakeups

I have just learned (how slow am I?) that Object.wait can return because 
of a spurious wakeup! This means that in addition to notify/notifyAll, 
interrupt and timeout, Java 5 javadoc has been "clarified" to allow 
Object.wait to return for any old reason the VM likes. I think we should 
make sure to review all calls to Object.wait to make sure they are 
properly done in light of spurious wakeups. See the example code below 
from the Java 5 javadocs:

[ http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#wait(long) 
]

"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
     }



BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
hargrave@xxxxxxxxxx

office: +1 407 849 9117
mobile: +1 386 848 3788