On Sat, Jul 4, 2009 at 9:28 AM, Mark Evensonevenson@panix.com wrote:
The one thing that bothers me is that apparently in Java, a thread in wait() can be woken up via so-called "spurious wakeups", i.e. no thread holding the object monitor that a wait() is called on has actually called notify() or notifyAll(). So, it is recommended (in the [documentation for java.lang.Thread.wait(timeout)][1]), that the following sort of code be used
synchronized(object) { while (<condition does not hold>) { object.wait(timeout); } }
but this seems seems like it would defeat the notion of specifying a timeout in the first place (i.e. as long as the condition did not hold), object.wait(timeout) would continue to be invoked). An explanation by someone of what I am not getting here would be helpful.
Well, if the timeout is a hard requirement (the thread MUST wait for at least x milliseconds), you would have probably needed to manually check that the timeout had really expired even without spurious wakeups, since wait() and sleep() do not guarantee any precision in respecting the timeout. If the timeout is not a hard requirement, you can probably live with the occasional (and very rare) spurious wakeup.
Bye, Alessio