views:

195

answers:

3

Hi, I was wondering why threads spontaneously awake from wait() in java.
Is it a design decision? Is it a compromise?

EDIT: (from Java Concurrency in Practice, p. 300)

wait is even allowed to return "spuriously" - not in response to any thread calling notify.

Further the authors state:

this is like a toaster with a loose connection that makes the bell go off when the toast is ready but also sometimes when it is not ready.

This is why you always have to code like

synchronized(this){
    while(!condition)
        wait();
    }
}

and never

synchronized(this){
    if(!condition){
        wait();
    }
}

Even if the condition transitions only from false to true.

A: 

Well, the Java API says:

Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

So they should wait until they are notified. However, there are overloads of this method that will wake up after a timeout has expired - perhaps that explains your observations?

Justin Ethier
Please refer to my edit. I guess the API should be corrected...
Enno Shioji
+15  A: 

These spontaneous wakeups are also called "spurious wakeups". In the Java specification, the spurious wakeups are permitted (though not encouraged) for jvm implementations.

The reason they are permitted is because many implementations may be based on pthreads (POSIX threads), that have this behaviour. Why?

Wikipedia:

According to David R. Butenhof's Programming with POSIX Threads ISBN 0-201-63392-2: "This means that when you wait on a condition variable, the wait may (occasionally) return when no thread specifically broadcast or signalled that condition variable. Spurious wakeups may sound strange, but on some multiprocessor systems, making condition wakeup completely predictable might substantially slow all condition variable operations. The race conditions that cause spurious wakeups should be considered rare."

bitc
Thanks for the background on POSIX, very informative!
Ricket
That makes a lot of sense. Thank you!
Enno Shioji
I think we posted this about 60 seconds apart
matt b
But what's the deal with the braces?
Lirik
+6  A: 

It's hard to answer this definitively since the Java Language Specification doesn't state why a JVM implementation might want to do this (it only specifies that it can, in this section), but I found a pretty interesting history of spurious wake-ups on Wikipedia.

The actual article in about POSIX threads but I don't think it's too far of a stretch to assume that threading in Java was somewhat influenced by the behavior of POSIX threads:

Spurious wakeups may sound strange, but on some multiprocessor systems, making condition wakeup completely predictable might substantially slow all condition variable operations. The race conditions that cause spurious wakeups should be considered rare.

This quote is from David R. Butenhof, who then goes on to say:

Though there were indeed some members of the working group who argued that it was theoretically possible to imagine that there might be such an implementation, that wasn't really the reason. (And they were never able to prove it.) POSIX threads were the result of a lot of tension between pragmatic hard realtime programmers and largely academic researchers. Spurious wakeups are the mechanism of an academic computer scientist clique to make sure that everyone had to write clean code that checked and verified predicates!

"But the (perhaps) largely spurious (or at least arcanely philosophical) 'efficiency' argument went over better with the realtime people, and the real reason was usually relegated to second place in the rationale.

"I've thought many times about how you might construct a correct and practical implementation that would really have spurious wakeups. I've never managed to construct an example. Doesn't mean there isn't one, though, and it makes a good story.

matt b