How to use methods Wait and Notify in Java thread

In Java, Object class contains three final methods that allows threads to communicate about the lock status of a resource. These methods are wait(), notify() and notifyAll(). We will dig into methods wait and notify in Java.

Let’s begin

1. The wait() Method
When we call wait() – this forces the current thread to wait until some other thread invokes notify() or notifyAll() on the same object.
This wait() method comes with three overloaded signatures. Let’s have a look at these.

a) wait()
The wait() method causes the current thread to wait until another thread either invokes notify() for this object or notifyAll(). Since we call wait(0) is the same as calling wait().

b) wait(long timeout)
Using this method, we can specify a timeout after which thread will be woken up automatically. A thread can be woken up before reaching the timeout using notify() or notifyAll().

c) wait(long timeout, int nanos)
This is the same functionality but the only difference being that we can provide higher precision. The total timeout period (in nanoseconds), is calculated as 1000000*timeout + nanos.

2. notify() and notifyAll()
There are two ways of notifying waiting threads.

a) notify()
The method notify() notifies any one of all threads waiting on this object’s monitor to wake up arbitrarily. The choice of exactly which thread to wake is non-deterministic.

b) notifyAll()
This method wakes all threads that are waiting on this object’s monitor and all awakened threads will complete in the usual manner.

Let’s see an example where multiple threads work on the same object and we use wait, notify and notifyAll methods

3) Message class
A java bean class on which threads will work and call wait and notify methods.

4) MyWaiter class
This class acts like a receiver that will wait for other threads to invoke notify methods to complete it’s processing.
Notice that MyWaiter thread uses synchronized block to own monitor on Message object.

5) MyNotifier class
This class acts like a sender that will work on Message object and then invoke notify method to wake up threads waiting for Message object.
Notice that MyNotifier thread uses synchronized block to own the monitor of Message object.

6) MyWaiterNotifierDemo
Demo class will create multiple threads of MyWaiter and MyNotifier and start them.

When we run the above program, then the output will be printed but our program will not complete because there are three threads waiting on
Message object and notify() method is called twice has wake up only two of them, the rest thread is still waiting to get notified.
Wait and Notify in Java

If we comment all the notify() call and uncomment the notifyAll() call in MyNotifier class, here is new output.
Wait and NotifyAll in Java

While using the wait and notify or notifyAll methods in Java the following things must be remembered:

a)Use notifyAll instead of notify if you expect that more than one thread will be waiting for a lock.
b)The wait and notify methods must be called in a synchronized context. See the link for a more detailed explanation.
c)Always call the wait() method in a loop because if multiple threads are waiting for a lock and one of them got the lock and reset the condition, then the other threads need to check the condition after they wake up to see whether they need to wait again or can start processing.
d)Use the same object for calling wait() and notify() method; every object has its own lock so calling wait() on object A and notify() on object B will not make any sense.

That’s all about wait and notify in Java.

Class Object

Download the complete source code, click link below (185 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons