Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java notify() Method in Threads Synchronization with Examples

  • Last Updated : 17 Jun, 2021

The notify() method is defined in the Object class, which is Java’s top-level class. It’s used to wake up only one thread that’s waiting for an object, and that thread then begins execution. The thread class notify() method is used to wake up a single thread. If multiple threads are waiting for notification, and we use the notify() method, only one thread will receive the notification, and the others will have to wait for more. This method does not return any value.

It is used with the wait() method, in order to communicate between the threads as a thread that goes into waiting for state by wait() method, will be there until any other thread calls either notify() or notifyAll() method.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Syntax:

public final void notify() ; 

Implementation:

  • The synchronized keyword is used for exclusive accessing.
  • wait() instructs the calling thread to shut down the monitor and sleep until another thread enters the monitor and calls notify().
  • notify() wakes up the first thread that called wait() on the same object.

Example

Java




// Java program to Illustrate notify() method in Thread
// Synchronization.
  
// Importing required classes
import java.io.*;
import java.util.*;
  
// Class 1
// Thread1
// Helper class extending Thread class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating object(thread) of class 2
        Thread2 objB = new Thread2();
  
        // Starting the thread
        objB.start();
  
        synchronized (objB)
        {
  
            // Try block to check for exceptions
            try {
  
                // Display message only
                System.out.println(
                    "Waiting for Thread 2 to complete...");
  
                // wait() method for thread to be in waiting
                // state
                objB.wait();
            }
  
            // Catch block to handle the exceptions
            catch (InterruptedException e) {
  
                // Print the exception along with line
                // number using printStackTrace() method
                e.printStackTrace();
            }
  
            // Print and display the total threads on the
            // console
            System.out.println("Total is: " + objB.total);
        }
    }
}
// Class 2
// Thread2
// Helper class extending Thread class
class Thread2 extends Thread {
  
    int total;
  
    // run() method  which is called automatically when
    // start() is initiated for the same
    // @Override
    public void run()
    {
  
        synchronized (this)
        {
  
            // iterating over using the for loo
            for (int i = 0; i < 10; i++) {
  
                total += i;
            }
  
            // Waking up the thread in waiting state
            // using notify() method
            notify();
        }
    }
}
Output
Waiting for Thread 2 to complete...
Total is: 45



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!