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 condition doesn't met, while notify() and notifyAll() method informs waiting thread for rechecking condition, after changing state of shared variable. One good example of how wait and notify method works is Producer consumer problem, where one thread produces, and wait if bucket is full; and other thread consumes and wait if bucket is empty. Both Producer and Consumer thread, notify each other as well. Producer thread notifies consumer thread after inserting an item in shared queue, while consumer thread notify producer, after consuming item from queue. Though Both notify() and notifyAll()  are used to notify waiting threads, waiting on shared queue object, but there are some subtle difference between notify and notifyAll in Java. Well, when we use notify(), only one of the sleeping thread will get notification, while in 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 wait and notify concept, along with why wait and notify is defined in Object class and why wait and notify called from synchronized method. In this article we will focus on difference between wait, notify, and notifyAll method in Java.

Difference between notify vs notifyAll in Java

Here are 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 thread is waiting on any lock in Java, notify 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 more safe because it send notification to all threads, so if any thread misses the notification, there are other threads to do the job, while in 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 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 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 correct in first attempt and concurrency bugs are often hard to figure out.

Related Java multithreading articles from Javarevisited

1 comment:

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