wait, notify and notifyAll method in java with example

You might have noticed Object class has three final method called wait, notify and notifyAll. These methods are used for inter thread communication.  Java 5 has introduced executor framework which takes care of inter thread communication for you and internally uses wait, notify and notifyAll but you still require basic understanding of these methods and how inter thread communication works in java using wait , notify and notifyAll.

What are wait , notify and notifyAll methods?

wait , notify and notifyAll method are used to allow threads to communicate to each other via accessing common object or in other terms, Object can be considered a medium for inter thread communication via these methods. These methods need to be called from synchronized context,otherwise it will throw java.lang.IllegalMonitorStateException.

Some background on synchronized block :
  • Only one thread can enter at a time in synchronized block
  • A thread required lock on the object to enter in synchronized block.
  • If Thread A want to enter in synchronized block then Thread A has to wait for Thread B to release it.

Lets have some brief understanding of these methods:

wait():

When you call wait method on the object then it tell threads to give up the lock and go to sleep state unless and until some other thread enters in same monitor and calls notify or notifyAll methods on it.

notify():

When you call notify method on the object, it wakes one of thread waiting for that object. So if multiple threads are waiting for an object, it will wake of one of them. Now you must be wondering which one it will wake up. It actually depends on OS implementation.

notifyAll() :

notifyAll will wake up all threads waiting on that object unlike notify which wakes up only one of them.Which one will wake up first depends on thread priority and OS implementation.

Lets understand it with the help of example:

1. Create a class named Book.java:

It is java bean class on which thread will act and call wait and notify method.

package org.arpit.java2blog.thread;

public class Book {
 
 String title;
 boolean isCompleted;
 
 public Book(String title) {
  super();
  this.title = title;
 }
 
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public boolean isCompleted() {
  return isCompleted;
 }
 public void setCompleted(boolean isCompleted) {
  this.isCompleted = isCompleted;
 }
 
}

2. Create a class named BookReader.java

This thread will wait until other thread call notify method, then after it will complete its processing. It will first take a lock on book object and will be called from synchronized block .So in this example, it will wait for BookWriter to complete the book.

package org.arpit.java2blog.thread;

public class BookReader implements Runnable{
 
 Book book;
 
 public BookReader(Book book) {
  super();
  this.book = book;
 }

 @Override
 public void run() {
  synchronized (book) {
   System.out.println(Thread.currentThread().getName()+" is waiting for the book to be completed: "+book.getTitle());
   try {
    book.wait();
   } catch (InterruptedException e) {    
    e.printStackTrace();
   }
   System.out.println(Thread.currentThread().getName()+": Book has been completed now!! you can read it");
  }
 } 

}

3. Create a class named BookWriter.java

This class will notify thread(in case of notify) which is waiting on book object. It will not give away lock as soon as notify is called, it first complete its synchronized block. So in this example, BookWriter will complete the book and notify it to BookReaders.

package org.arpit.java2blog.thread;
public class BookWriter implements Runnable{

 Book book;
 
 public BookWriter(Book book) {
  super();
  this.book = book;
 }

 @Override
 public void run() {
  synchronized (book) {
   System.out.println("Author is Starting book : " +book.getTitle() );
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   book.setCompleted(true);
   System.out.println("Book has been completed now");
   
   book.notify();
   System.out.println("notify one reader");
  
  } 
 }
}

4. Create a class ThreadInterCommunicationMain,java.

This is our main class which will create object of above classes and run it.

package org.arpit.java2blog.thread;

public class ThreadInterCommunicationMain {
 
 public static void main(String args[])
 {
  // Book object on which wait and notify method will be called
  Book book=new Book("The Alchemist");
  BookReader johnReader=new BookReader(book);
  BookReader arpitReader=new BookReader(book);
  
  // BookReader threads which will wait for completion of book
  Thread johnThread=new Thread(johnReader,"John");
  Thread arpitThread=new Thread(arpitReader,"Arpit");
  
  arpitThread.start();
  johnThread.start();
  
  // To ensure both readers started waiting for the book
  try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   
   e.printStackTrace();
  }
  // BookWriter thread which will notify once book get completed
  BookWriter bookWriter=new BookWriter(book);
  Thread bookWriterThread=new Thread(bookWriter);
  bookWriterThread.start();
  
 }
 
}


In case of notify():

When you run above program, you will get following outputs:
Arpit is waiting for the book to be completed: The Alchemist
John is waiting for the book to be completed: The Alchemist
Author is Starting book : The Alchemist
Book has been completed now
notify one reader
Arpit: Book has been completed now!! you can read it

So here,two BookReader threads(arpit and john) are waiting for book to be completed,so they called book.wait(). Once BookWriter completes it book, it called book.notify() and arpit thread gets up and completes its processing.

In case of notifyAll() :

Lets change BookWriter class to call book.notifyAll().
package org.arpit.java2blog.thread;

public class BookWriter implements Runnable{

 Book book;
 
 public BookWriter(Book book) {
  super();
  this.book = book;
 }

 @Override
 public void run() {
  synchronized (book) {
   System.out.println("Author is Starting book : " +book.getTitle() );
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   book.setCompleted(true);
   System.out.println("Book has been completed now");
   
   book.notifyAll();
   System.out.println("notify readers");
  
  }
 }

}

When you run above program, you will get following output:

Arpit is waiting for the book to be completed: The Alchemist
John is waiting for the book to be completed: The Alchemist
Author is Starting book : The Alchemist
Book has been completed now
notify readers
John: Book has been completed now!! you can read it
Arpit: Book has been completed now!! you can read it

In case of notifyAll(), it notifies all threads waiting on that object.

Please go through  top 50 core java interview questions for more interview questions.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012