What are wait , notify and notifyAll methods?
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.
wait():
notify():
notifyAll() :
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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"); } } } |
This is our main class which will create object of above classes and run it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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(); } } |
When you run above program, you will get following outputs:
1 2 3 4 5 6 7 8 |
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 |
In case of notifyAll() :
Lets change BookWriter class to call book.notifyAll().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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:
1 2 3 4 5 6 7 8 9 |
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.