If zero or one threads are waiting on the object, notify() and notifyAll() do the same. If more than one thread is waiting, notify() only wakes up one of them, but notifyAll() wakes up all of them. You can't control which one is woken by notify().
In practice, notifyAll() is used much more often.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I actually use notify() more than notifyAll(). It is rare that I have more than one thread waiting on a given monitor. Instead I usually control very carefully what thread is waiting on a given monitor and don't have to worry about interference because the monitor isn't exposed. Of course, that doesn't mean it's right. I am but an infant in the realm of concurrency.
Agreed. There should be very few cases where notifyAll() is needed instead of notify(). The two obvious cases are...
- A condition is in a state that can satisfy multiple threads at the same time. - The waiting threads are waiting for different conditions, but using the same lock.
With the Lock and Condition classes of Java 5, this second case should no longer apply.
Unfortunately, most of the time, notifyAll() is used, because the developer could not figure out how to get notifications to work correctly.