I did read in Core Java, Volume II that notify() is not recommended, but instead, use notifyAll(),
<RANT>
This is a Rant. Please feel free to ignore.
NotifyAll() is to be used when a thread has changed a condition that can satisfy more than one thread... hence, you have to wake up more than one thread. There are very few cases where this is needed, as most threaded designs are for thread safety of a common (single) resource. In fact, in most cases, they are only needed during shutdown, where all the threads needs to be awaken so that they may exit.
Unfortunately... most people use notifyAll() because...
- Having threads waiting for different conditions waiting on the same notification object. You have to wake them all because you can't target the correct thread.
- Having incorrect synchronization which cause wait/notify not to work correctly. Using notifyAll partly makes it work better, but not completely.
- Just poor (or no) notification design. Just call notifyAll() to tell all threads something happened, and pray that it works.
Prior to Java 5, only the first case is acceptable -- as it was not possible to have two conditions share the same synchronization lock. This has been fixed with the Lock/Condition class.
Remember, waking up *all* waiting threads, when one thread is needed. Or worse, waking up *all* waiting threads, because there is a bug somewhere in your code, is just plain silly. Fix your design. Or fix your code. Recommending notifyAll() over notify() cause it works better is just wrong.
</RANT>
Sorry about that. I will take my meds now...
Henry