Difference between notify() and notifyAll() methods, with program


Theoretically you must have heard or you must be aware of differences between notify() and notifyAll().But have you created program to achieve it? If not let’s do it.

First, I will like give you a brief description of what notify() and notifyAll() methods do.


notify() - Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is random and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.
public final native void notify();

notifyAll() - Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

public final native void notifyAll();


Now it’s time to write down a program to prove the point >

I guess by this time you must be well capable or writing a program to show differences between notify() and notifyAll() method. But did you saw above mentioned line The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object . What does it mean?
It means when notify() method is called on object, thread notifies the other thread waiting on this object's monitor. But thread does not immediately releases the object lock, it waits for synchronization block to complete.

Program (Execute code by commenting or uncommenting either of notify() or notifyAll() method)>
class MyRunnable1 extends Thread{
  
   public void run(){
         
          synchronized (this) {
                 System.out.println(Thread.currentThread().getName()+" started");
                
                 try {
                       this.wait();
                 } catch (InterruptedException e) {
                       e.printStackTrace();
                 }
                
                 System.out.println(Thread.currentThread().getName()+" has been notified");
          }
   }
  
}
class MyRunnable2 extends Thread{
  
   MyRunnable1 myRunnable1;
  
   MyRunnable2(MyRunnable1 MyRunnable1){
          this.myRunnable1=MyRunnable1;
   }
  
   public void run(){
         
          synchronized (this.myRunnable1) {
                 System.out.println(Thread.currentThread().getName()+ " started");
                
                 try {
                       this.myRunnable1.wait();
                 } catch (InterruptedException e) {
                       e.printStackTrace();
                 }
                 System.out.println(Thread.currentThread().getName()+" has been notified");
          }
         
   }
}
class MyRunnable3 extends Thread{
  
   MyRunnable1 myRunnable1;
  
   MyRunnable3(MyRunnable1 MyRunnable1){
          this.myRunnable1=MyRunnable1;
   }
  
   public void run(){
          synchronized (this.myRunnable1) {
                 System.out.println(Thread.currentThread().getName()+ " started");
                
                 this.myRunnable1.notify(); // Wakes up a single thread that is
                                //waiting on this object's monitor.
                               //If any threads are waiting on this object,
                                //one of them is chosen to be awakened.
                               //The choice is random and occurs at the
                               //discretion of the implementation.
                
                 //this.myRunnable1.notifyAll(); // Will wake up all threads
                                                  //waiting on object's monitor.
                 System.out.println(Thread.currentThread().getName()+
                                 " has notified waiting threads");
          }
         
   }
}

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class CallNotify {
   public static void main(String[] args) throws InterruptedException {
         
          MyRunnable1 myRunnable1=new MyRunnable1();
          MyRunnable2 myRunnable2=new MyRunnable2(myRunnable1);
          MyRunnable3 myRunnable3=new MyRunnable3(myRunnable1);
          Thread t1=new Thread(myRunnable1,"Thread-1");
          Thread t2=new Thread(myRunnable2,"Thread-2");
          Thread t3=new Thread(myRunnable3,"Thread-3");
         
          t1.start();
          t2.start();
          Thread.sleep(100);  //Used to ensure that thread1 and thread2 starts before thread-3
                      //because thread-1 and 2 calls wait(), while thread-3 calls notify or notifyAll()
          t3.start();
         
         
         
   }
}
/* OUTPUT with notify()
Thread-1 started
Thread-2 started
Thread-3 started
Thread-3 has notified waiting threads
Thread-1 has been notified
*/
/* OUTPUT with notifyAll()
Thread-1 started
Thread-2 started
Thread-3 started
Thread-3 has notified waiting threads
Thread-1 has been notified
Thread-2 has been notified
*/

Let’s take a quick look at output -

When we called notify() -
Only one of the two waiting thread was notified (i.e. Thread-1 and Thread-2 were waiting and only Thread-1 was notified)

When we called notifyall() -
Both of two waiting threads were notified (i.e. Thread-1 and Thread-2 were waiting and both were notified)





RELATED LINKS>


Important Thread methods (salient features, usage with programs)>

Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method-10 salient features of join

Sleep() method in threads - 10 key features with programs

Yield() method in threads - 8 key features with programs

Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.

Daemon threads - 12 salient features of Daemon Thread

2 alternate ways to stop thread, as stop() method is deprecated

Using Suspend and resume method in threads



Differences between important thread methods >

Difference between wait() and sleep() method in threads

Differences and similarities between yield() and sleep() in threads

Difference between notify() and notifyAll() methods, with program

Difference between wait() and wait(long timeout) -What will be Thread states



eEdit
Must read for you :