Difference between notify and notifyAll in Java? [Answered]

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 the Producer consumer problem, where one thread produces and waits 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 objects, there are some subtle differences between notify and notifyAll in Java.

When we use notify(), only one of the sleeping threads will get a notification, while in the case of notifyAll(), all sleeping threads on that object will get notified. This concept confuses many Java programmers, both beginners and experienced alike, 

In fact, this is one of three questions that are very popular on a wait and notifies concept, along with why to wait and notify is defined in Object class and why to wait and notify called from the synchronized method. 

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




Difference between notify vs notifyAll in Java Multithreading? Answer

Here is a 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 are waiting on any locks in Java, notify method sends a notification to only one of the waiting threads while notifyAll informs all threads waiting on that lock.

2. If you use notify method, It's not guaranteed which thread will be informed, but if you use notifyAll since all threads will be notified, they will compete for lock, and the lucky thread which gets locked will continue.

 In a way, the notifyAll method is safer because it sends a 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 for any reason, use notifyAll. 

Since the wait method is called from the loop and they check condition even after waking up, calling notifyAll won't lead to any side effect, instead it ensures that notification is not dropped. You can also see these Java Multithreading courses to learn more about the wait, notify, and notifyAll in Java. 

Difference between notify and notifyAll in Java? Answer


Having said that main difference between notify and notifyAll in Java comes down to the fact of 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 utilities like CountDownLatch, CyclicBarrier, and Semaphore to write your concurrency code. It's not easy to get the wait and notify method working correctly in the first attempt and concurrency bugs are often hard to figure out.


Other Java Multithreading Articles you may like

3 comments:

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

    ReplyDelete
    Replies
    1. A singly Linked List node is just a node object which has value of the current item and the address to the next item stored in it. For doubly linked list, the address of the previous and next nodes are both stored. It is very simple.

      Delete
    2. Yes, very well explained Jeet.

      Delete

Feel free to comment, ask questions if you have any doubt.