Importance of wait(), notify() and notifyAll() methods in Java?



The threads can communicate with each other through wait(), notify(), and notifyAll() methods in Java. These are the final methods defined in the Object class and can be called only from within a synchronized context.

The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object.

These methods will throw an IllegalMonitorStateException if the current thread is not the owner of the object's monitor.

The wait() Method

In Java, the wait() method of the Object class allows threads to pause their execution and wait for a specific condition to be satisfied or until the notify() or notifyAll() method is invoked by some other thread of this object.

Following is the syntax of the wait() method:

public final void wait() throws InterruptedException 

Example

The following is a basic example of the wait() method, which pauses the threads for execution:

class myThread { synchronized void waitForSignal() { try { System.out.println(Thread.currentThread().getName() + " is waiting..."); //calling wait() method to pause execution of current threads wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public class Demo { public static void main(String[] args) { myThread th = new myThread(); // Create threads that will wait Runnable waitingTask = () -> th.waitForSignal(); Thread t1 = new Thread(waitingTask, "Thread-1"); Thread t2 = new Thread(waitingTask, "Thread-2"); t1.start(); t2.start(); } } 

Following is the output of the above program:

Thread-1 is waiting... Thread-2 is waiting... 

The notify() Method

The notify() method of the Object class is used to wake up a single thread that is waiting on this object's monitor. Following is the syntax of the notify() method:

public final void notify() 

Example

The following example uses the notify() method to wake up a single thread whose execution is paused:

class myThread { synchronized void waitForSignal() { try { System.out.println(Thread.currentThread().getName() + " is waiting..."); //calling wait() method to pause execution of current threads wait(); //start execution when the gets notified by notify() or notifyAll() method System.out.println(Thread.currentThread().getName() + " got notified!"); } catch (InterruptedException e) { e.printStackTrace(); } } //method to notify only single thread synchronized void notifyOneThread() { System.out.println("Notifying one thread..."); //using notify() method to wake up a single thread and notify to wait() method notify(); } } public class Demo { public static void main(String[] args) { myThread th = new myThread(); // Create 3 threads that will wait Runnable waitingTask = () -> th.waitForSignal(); Thread t1 = new Thread(waitingTask, "Thread-1"); Thread t2 = new Thread(waitingTask, "Thread-2"); t1.start(); t2.start(); // Give some time for threads to start and wait try { Thread.sleep(1000); } catch (InterruptedException e) {} // Notify only one thread th.notifyOneThread(); } } 

Below is the output of the above program:

Thread-1 is waiting... Thread-2 is waiting... Notifying one thread... Thread-1 got notified! 

The notifyAll() Method

The notifyAll() method of the Object class is used to wake up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling the object's wait() method.

Following is the syntax of the notifyAll() method:

public final void notifyAll() 

Example

The following example uses all the wait(), notify() and notifyAll() method. The wait() method pauses execution, and notify() and notifyAll() methods wake up single or multiple threads to notify the wait() method to start execution:

class myThread { synchronized void waitForSignal() { try { System.out.println(Thread.currentThread().getName() + " is waiting..."); //calling wait() method to pause execution of current threads wait(); //start execution when the gets notified by notify() or notifyAll() method System.out.println(Thread.currentThread().getName() + " got notified!"); } catch (InterruptedException e) { e.printStackTrace(); } } //method to notify only single thread synchronized void notifyOneThread() { System.out.println("Notifying one thread..."); //using notify() method to wake up a single thread and notify to wait() method notify(); } //method to notify to all threads synchronized void notifyAllThreads() { System.out.println("Notifying all threads..."); //using notifyAll() method to wakeup all threads and notify to wait() method notifyAll(); } } public class Demo { public static void main(String[] args) { myThread th = new myThread(); // Create 3 threads that will wait Runnable waitingTask = () -> th.waitForSignal(); Thread t1 = new Thread(waitingTask, "Thread-1"); Thread t2 = new Thread(waitingTask, "Thread-2"); Thread t3 = new Thread(waitingTask, "Thread-3"); t1.start(); t2.start(); t3.start(); // Give some time for threads to start and wait try { Thread.sleep(1000); } catch (InterruptedException e) {} // Notify only one thread th.notifyOneThread(); // Give time to see only one thread resume try { Thread.sleep(1000); } catch (InterruptedException e) {} // Notify all remaining threads th.notifyAllThreads(); } } 

Following is the output of the above program:

Thread-1 is waiting... Thread-3 is waiting... Thread-2 is waiting... Notifying one thread... Thread-1 got notified! Notifying all threads... Thread-3 got notified! Thread-2 got notified! 

Importance of the wait(), notify(), and notifyAll() Method

Below is a list of a few important points of the above method

  • The wait(), notify(), and notifyAll() enable communication between threads for coordinated access to shared resources.
  • The wait() pauses a thread and releases the lock until it's notified, preventing busy waiting.
  • These methods are important in synchronization problems like Producer-Consumer for thread management.
Updated on: 2025-06-23T11:05:39+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements