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.
It is java bean class on which thread will act and call wait and notify method.
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.
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.
4. Create a class ThreadInterCommunicationMain,java.
This is our main class which will create object of above classes and run it.
This is our main class which will create object of above classes and run it.
In case of notify():
When you run above program, you will get following outputs:
When you run above program, you will get following outputs:
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().
Lets change BookWriter class to call book.notifyAll().
When you run above program, you will get following output:
In case of notifyAll(), it notifies all threads waiting on that object.