Difference between notify and notifyAll in Java

wait, notify, and notifyAll methods are used for inter-thread communication in Java. wait() allows a thread to check for a condition, and wait if the condition doesn't meet, while notifying() and notifyAll() method informs waiting for a thread for rechecking condition, after changing the state of a shared variable. One good example of how to wait and notify method works is Producer consumer problem, where one thread produces and wait if the bucket is full; and another thread consumes and waits if the bucket is empty. Both Producer and Consumer thread, notify each other as well. Producer thread notifies consumer thread after inserting an item in the shared queue, while consumer thread notifies producer, after consuming item from the queue. Though Both notify() and notifyAll()  are used to notify waiting for threads, waiting on shared queue object, but there are some subtle differences between notify and notifyAll in Java.
When we use notify(), only one of the sleeping thread will get a notification, while in the case of notifyAll(), all sleeping thread on that object will get notified. This concept confuses many Java programmers, both beginners and experienced alike, 

Infact this is one of three question which is very popular on a wait and notify concept, along with why to wait and notify is defined in Object class and why to wait and notify called from synchronized method. 

In this article we will focus on the difference between wait, notify, and notifyAll method in Java.

Difference between notify vs notifyAll in Java

Here is couple of main differences between notify and notifyAll method in Java :

1. First and main difference between notify() and notifyAll() method is that, if multiple threads is waiting on any lock in Java, notify method send notification to only one of waiting thread while notifyAll informs all threads waiting on that lock.

2. If you use notify method , It's not guaranteed that, which thread will be informed, but if you use notifyAll since all thread will be notified, they will compete for lock and the lucky thread which gets lock will continue. In a way, notifyAll method is safer because it sends notification to all threads, so if any thread misses the notification, there are other threads to do the job, while in the case of notify() method if the notified thread misses the notification then it could create subtle, hard to debug issues. 

3. Some people argue that using notifyAll can drain more CPU cycles than notify itself but if you really want to sure that your notification doesn't get wasted by any reason, use notifyAll. Since wait method is called from the loop and they check condition even after waking up, calling notifyAll won't lead any side effect, instead it ensures that notification is not dropped.

Difference between wait vs notify vs notifyAll in JavaHaving said that main difference between notify and notifyAll in Java comes down to the fact how many threads will be wake up. Prefer notifyAll over notify whenever in doubt and if you can, avoid using notify and notifyAll altogether, instead use concurrency utility like CountDownLatch, CyclicBarrier, and Semaphore to write your concurrency code. It's not easy to get wait and notify method working correctly in first attempt and concurrency bugs are often hard to figure out.

Related Java multithreading articles from Javarevisited

Related topics:

1 comment:

  1. how does linked list work in java and give the internal implementation for linkedlist.