[concurrency-interest] spurious wakeups semantics

Tim Peierls tim at peierls.net
Wed Nov 2 10:35:39 EST 2005


P.H.Welch wrote:
> Isn't it time that spurious wakeups were removed from Java semantics?

Who would benefit?

Not JVM designers, since they would have to work around the possibility of spurious wakeups at the 
OS level, with a consequent cost in performance.

Not programmers: The only burden imposed on the programmer by the possibility of spurious wakeups 
is the need to recheck a condition predicate. But this is such a good practice in any case that it 
doesn't really count as a burden.

Imagine what would happen if spurious wakeups were disallowed. First, programmers (those who never 
really bought into the idea that premature optimization is evil) would rush to turn this:

   synchronized (this) {
       while (!condition) wait();
       // do things that need condition to hold
   }

into this (to avoid a condition test that they "just know" is unnecessary):

   synchronized (this) {
       if (!condition) wait();
       // assert condition;  // "commented out to save cycles" (would be the claim)
       // do things that need condition to hold
   }

and then the notifying methods would change subtly so that the condition would *not* necessarily 
hold while you are doing things that require it to be true. The resulting bugs would manifest 
years later and not in a way that is obviously connected to the wait() call.


--tim



More information about the Concurrency-interest mailing list