Difference between notify() and notifyAll() in Java

notify() and notifyAll() methods with wait() method are used to for communication between the threads. A thread which goes into waiting state by calling wait() method will be in waiting state until any other thread calls either notify() or notifyAll() method on the same object.
Now the question is both notify() and notifyAll() method is used to give notification to the waiting thread, then what is the difference between them or where we should use notify() method and where we should go for notifyAll() method?
Lets understand how notify() method behaves:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// behaviour of notify() method
class Geek1 extends Thread {
public void run()
    {
        synchronized(this)
        {
            System.out.println
            (Thread.currentThread().getName() + "...starts");
            try {
                this.wait();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println
            (Thread.currentThread().getName() + "...notified");
        }
    }
} class Geek2 extends Thread {
    Geek1 geeks1;
    Geek2(Geek1 geeks1)
    {
        this.geeks1 = geeks1;
    }
public void run()
    {
        synchronized(this.geeks1)
        {
            System.out.println
            (Thread.currentThread().getName() + "...starts");
  
            try {
                this.geeks1.wait();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println
            (Thread.currentThread().getName() + "...notified");
        }
    }
} class Geek3 extends Thread {
    Geek1 geeks1;
    Geek3(Geek1 geeks1)
    {
        this.geeks1 = geeks1;
    }
public void run()
    {
        synchronized(this.geeks1)
        {
            System.out.println
            (Thread.currentThread().getName() + "...starts");
            this.geeks1.notify();
            System.out.println
            (Thread.currentThread().getName() + "...notified");
        }
    }
} class MainClass {
public static void main(String[] args) throws InterruptedException
    {
  
        Geek1 geeks1 = new Geek1();
        Geek2 geeks2 = new Geek2(geeks1);
        Geek3 geeks3 = new Geek3(geeks1);
        Thread t1 = new Thread(geeks1, "Thread-1");
        Thread t2 = new Thread(geeks2, "Thread-2");
        Thread t3 = new Thread(geeks3, "Thread-3");
        t1.start();
        t2.start();
        Thread.sleep(100);
        t3.start();
    }
}

chevron_right


Output:

Thread-1...start
Thread-2...starts
Thread-3...starts
Thread-3...notified
Thread-1...notified

Lets understand how notifyAll() method behaves:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// behavior of notifyAll() method
class Geek1 extends Thread {
public void run()
    {
        synchronized(this)
        {
            System.out.println
            (Thread.currentThread().getName() + "...starts");
            try {
                this.wait();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println
            (Thread.currentThread().getName() + "...notified");
        }
    }
} class Geek2 extends Thread {
    Geek1 geeks1;
    Geek2(Geek1 geeks1)
    {
        this.geeks1 = geeks1;
    }
public void run()
    {
        synchronized(this.geeks1)
        {
            System.out.println
            (Thread.currentThread().getName() + "...starts");
  
            try {
                this.geeks1.wait();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println
            (Thread.currentThread().getName() + "...notified");
        }
    }
} class Geek3 extends Thread {
    Geek1 geeks1;
    Geek3(Geek1 geeks1)
    {
        this.geeks1 = geeks1;
    }
public void run()
    {
        synchronized(this.geeks1)
        {
            System.out.println
            (Thread.currentThread().getName() + "...starts");
  
            this.geeks1.notifyAll();
            System.out.println
            (Thread.currentThread().getName() + "...notified");
        }
    }
} class MainClass {
public static void main(String[] args) throws InterruptedException
    {
  
        Geek1 geeks1 = new Geek1();
        Geek2 geeks2 = new Geek2(geeks1);
        Geek3 geeks3 = new Geek3(geeks1);
        Thread t1 = new Thread(geeks1, "Thread-1");
        Thread t2 = new Thread(geeks2, "Thread-2");
        Thread t3 = new Thread(geeks3, "Thread-3");
        t1.start();
        t2.start();
        Thread.sleep(100);
        t3.start();
    }
}

chevron_right


Output:

Thread-1...starts
Thread-2...starts
Thread-3...starts
Thread-3...notified
Thread-2...notified
Thread-1...notified

Differences between notify() and notifyAll()



  1. Notification to number of threads : We can use notify() method to give the notification for only one thread which is waiting for a particular object whereas by the help of notifyAll() methods we can give the notification to all waiting threads of a particular object.
  2. Notifying a thread by JVM : If multiple threads are waiting for the notification and we use notify() method then only one thread get the notification and the remaining thread have to wait for further notification. Which thread will get the notification we can’t expect because it totally depends upon the JVM. But when we use notifyAll() method then multiple threads got the notification but execution of threads will be performed one by one because thread requires lock and only one lock is available for one object.
  3. Interchangeability of threads : We should go for notify() if all your waiting threads are interchangeable (the order they wake up doesn’t matter). A common example is a thread pool. But we should use notifyAll() for other cases where the waiting threads may have different purposes and should be able to run concurrently. An example is a maintenance operation on a shared resource, where multiple threads are waiting for the operation to complete before accessing the resource.
  4. When to use notify() method and notifyAll()

    • In case of mutually exclusive locking, only one of the waiting threads can do something useful after being notified (in this case acquire the lock). In such a case, you would rather use notify(). Properly implemented, you could use notifyAll() in this situation as well, but you would unnecessarily wake threads that can’t do anything anyway.
    • In some cases, all waiting threads can take useful action once the wait finishes. An example would be a set of threads waiting for a certain task to finish; once the task has finished, all waiting threads can continue with their business. In such a case you would use notifyAll() to wake up all waiting threads at the same time.

    Applications of notify() and notifyAll()

    • A maintenance operation on a shared resource, where multiple threads are waiting for the operation to complete before accessing the resource; for these we should go for notifyAll().
    • Let’s say we have a producer thread and a consumer thread. Each “packet” produced by the producer should be consumed by a consumer. The consumer puts something in a queue and then calls notify().
    • We want to have a notification when a lengthy process has finished. You want a beep and a screen update. The process performs notifyAll() to notify both the beeping-thread and the screen-update-thread.

    Reference : https://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again

    This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



    My Personal Notes arrow_drop_up


    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.