File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Difference beteween notify and notifyAll. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Difference beteween notify and notifyAll." Watch "Difference beteween notify and notifyAll." New topic
Author

Difference beteween notify and notifyAll.

ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
What is the practical difference beteween notify and notifyAll.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.<br /> <br />#:^P
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Same.

I used notifyAll() once long time ago when I created an implementing of MPI (message passing interface) in Java.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16690
    
  19

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference beteween notify and notifyAll.
 
Similar Threads
Difference beteween interrupted() and isInterrupted()
Difference beteween HashSet and LinkedHashSet.
difference beteween SWING and AWT
Differences beteween ArrayList and Vector.
Difference beteween two dates