Site icon JavaGoal

notify() method in Java

In this article, we will discuss the notify() method in Java. As you already know notify() method is used with the wait() method. We will recommend you read the wait() method before the move further.

Here is the table content of the article will we will cover this topic.
1. What is the notify() method in Java?
2. How does it notify() method work?
3. What is the notifyAll() method in Java?
4. How does it work?

What is the notify() method in Java?

The notify() method is defined in the Object class which is the super most class in Java. It is used to wake up only one thread that is waiting on the object and that thread starts execution.

Suppose there are multiple threads that are waiting for an object, then it will wake up only one of them. Only one thread gets the notification and the remaining thread have to wait for further notification. It is a final method, so we can’t override it.

Let’s have a look at the code.

public final native void notify();

How does it work?

1. It totally depends on the OS implementation of thread management. This method wakes up only one thread called the wait() method on the same object.
2. The awakened thread will not be able to proceed further until the current thread gives the lock on this object.
3. It does not actually give up a lock on a resource.  It just tells the waiting thread that it can wake up.
4. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object.
5. This method should only be called by a thread that is the owner of this object’s monitor.

Let’s say a thread calls notify() method on a resource but the thread still needs 5 seconds to perform some actions on the resource within its synchronized block. The thread that had been waiting will need to wait 5 seconds to release the lock on the object. 

class Printer 
{ 
	// Initial 100 paper are set in Printer
	int noOfPaper = 100; 

	// Synchronized the method for inter-thread communication 
	synchronized void printingPages(int pages) 
	{ 
		System.out.println("Printing the Pages"); 
		for(int i = 0; i < 100; i++)
		{
			// Printing Pages
		}
		
		// If balance number of Papers are less than user input 
		// then wait for addPages() synchronized method   
		// and printing will resume after that
		if (this.noOfPaper < pages) 
		{ 
			System.out.println("Number of Papers in printer are less"); 
			try 
			{ 
				System.out.println("Waiting...");
				wait(); 
			} 
			catch (Exception e) 
			{ 
				
			} 
		} 
		System.out.println("After called notify() method number of Paper : " +	this.noOfPaper); 
		System.out.println("Printing process complete");
		 
	} 

	synchronized void addPages(int noOfPages) 
	{ 
		// Adding more Papers in Printer; 
		this.noOfPaper += noOfPages; 
		// After adding the paper in printer. Notify the Paused thread; 
		notify(); 
		System.out.println("Notify called but thread needs 5 more seconds");
		// After call notify() method lock will be give up after 5 seconds 
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
} 

public class MainClass 
{ 
	public static void main(String args[]) 
	{ 
		Printer printer = new Printer(); 
		// create two new thread and start them simultaneously 
		// First thread for print the pages
		new Thread() 
		{ 
			@Override
			public void run() 
			{ 
				// User want to print 120 pages
				printer.printingPages(120); 
			} 
		}.start(); 
		
		// Second thread for Add pages in printer
		new Thread() 
		{ 
			@Override
			public void run() 
			{ 
				// Add 100 more pages in Printer
				printer.addPages(100); 
			} 
		}.start(); 
	} 
} 

Output: Printing the Pages
Number of Papers in printer are less
Waiting…
Notify called but thread needs 5 more seconds
After called notify() method number of Paper : 200
Printing process complete

notifyAll() method in Java

The notifyAll() method is defined in the Object class which is the super most class in Java. It is used to wake up all threads that are waiting on the given object. This method gives the notification to all waiting threads of an object.

How does it work?

Whenever we use the notifyAll() method and multiple threads that are waiting for the notification, then all the threads got the notification. But it will execute all the threads one by one. All thread gets the notification but only one thread gets a lock. So that only one thread can execute at a time after that next one will be executed.

Now the question arises which thread should get locked first?
1. It totally depends on the OS implementation of thread management. This method wakes up all the threads that are called the wait() method on the same object. But only one thread gets the lock.
2. The awakened threads will not be able to proceed further until the current thread gives the lock on this object.
3. It does not actually give up a lock on a resource. It just tells the waiting threads that they can wake up.
4. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object.
5. This method should only be called by a thread that is the owner of this object’s monitor

Exit mobile version