Inter thread communication (wait/notify/notify all)

Important Point :

  • wait, notify and notifyAll methods are present in Object class
  1. Two threads can communicate with each other by using wait, notify and nofitfyAll methods.
    The thread which is expecting updation is responsible to call wait method, then immediately the thread will be entered into waiting state.
    The thread which is responsible to perform updation, after performing updation it is responsible to call notify method then waiting thread will get that notification and continue its execution with those updated items
  2. Wait, notify, nofitfyAll methods present in Object class but not in thread class because thread can call these methods on any java object.
  3. To call wait, notify, nofitfyAll method on any object, thread should be owner of that object i.e the thread should have lock of that object i.e the thread should be inside synchronized area, hence we can call wait, notify and nofitfyAll methods only from synchronized area otherwise we will get run time exception saying “IllegalMonitorStateException”
  4. If a thread calls wait method on any object, it immediately releases the lock of that perticular object and enteres into waiting state.
  5. If a thread calls notify method on any object, it releases the lock of that object but may not immediately. Except wait, notify, nofitfyAll there is no other method where thread releases the lock.
public final void wait() throws InterruptedException
public final native void wait(long ms) throws InterruptedException
public final void wait(long ms,int ns) throws InterruptedException

public final native void notify()
public final native void notifyAll()

Note : Every wait method throws InterruptedException which is check exception Hence when ever we are using wait method compulsory we should handle the interrupted Exception.

public class ThreadA {
	public static void main(String[] args) throws InterruptedException {
		ThreadB b = new ThreadB();
		synchronized(b) {
			System.out.println("Main Thread calling wait method");
			b.wait(); //waiting for notification
			System.out.println("Main Thread gets notification");
class ThreadB extends Thread{
	int total;
	public void run() { 
		synchronized(this) {
			System.out.println("Child thread starts calculation");
			for(int i=1;i<=100;i++) {
				total = total+i;
			System.out.println("Child thread giving notification");

notify() vs notifyAll()

notify() notifyAll()
We can use notify method to give the notification for only one waiting thread, if multiple thread are waiting then only one thread will be notified and the remaining threads have to wait for further notifications. Which thread will be notified we can’t expect, it depends on JVM We can use notifyAll to give notification for multiple threads of a particular object. Even though multiple threads are notified but execution will be performed one by one because threads are required lock and only one lock is available

On which object we are calling wait method thread requires lock of that particular object.
Example : We are calling wait method on s1 object then we have to get lock of s1 object, but not s2 object


2 thoughts on “Inter thread communication (wait/notify/notify all)”

Leave a Comment