Concurrency in Java: using wait(), notify() and notifyAll()
- 4.0/5
- 4909
- Jul 20, 2024
Signaling is used to synchronise multiple threads by signalling to each other that a new thread can do something that was previously blocked by calling the wait() method.
In Java, thread signalling is implemented via the wait(), notify(), and notifyAll() methods that are part of the "Object" class.
A thread that calls "wait()" on any object becomes inactive (blocked) until another thread calls "notify()" or "notifyAll()" on that object.
In order to call "wait(), notify(), or notifyAll()" on an object, a thread must obtain the lock on that object first. That is, the calling thread must call wait(), notify(), or notifyAll() from inside a synchronized block or method.
If a thread tries to call "wait(), notify(), or notifyAll()" on an object without holding the synchronization lock on that object, an IllegalMonitorStateException is thrown.
In the example above, when a thread enters the synchronized block and calls wait() on the monitor object, the calling thread releases the lock on the monitor object and is "blocked" until another thread calls notify() or notifyAll() on the monitor object.
When another thread enters the synchronized block and calls the "notify()" method on the monitor object, it wakes up any one of the threads blocked by the wait() call on the same monitor object.
However, this awakened thread cannot exit the wait() method until the thread calling notify() has released the lock on the monitor object.
Multiple threads can call "wait()" on the same monitor object and thus become blocked waiting for a "notify() or notifyAll()" call.
Difference between notify() and notifyAll()
The main difference between the "notify()" and "notifyAll()" methods is that, if multiple threads are waiting on any locks in Java, the notify method sends a notification to only one of the waiting threads, while notifyAll informs all threads waiting on that lock.
If you use "notifyAll()", since all threads will be notified, they will compete for a lock, and the lucky thread that gets a lock can continue.
In a way, the notifyAll() method is safer because it sends a notification to all threads, so if any thread misses the notification, there are other threads to do the job.
Calling both of these methods does not give up the lock on the resource; rather, their job is to wake up the threads that have been sent to the waiting state using the "wait()" method.
It is mandatory to enclose wait() in a try-catch block because if a thread present in the waiting state gets interrupted, then it will throw an InterruptedException.
The wait() method variations
The wait() method has three variations:
1) wait(): This will cause the thread to wait until "notify()" or "notifyAll()" is called.
2) wait(long timeout): This will cause the thread to wait either until "notify()" or "notifyAll()" is called or until the timeout duration expires.
3) wait(long timeout, int nanoseconds): This will cause the thread to wait either until "notify()" or "notifyAll()" is called or until the timeout duration expires.
Missed Signals
A missed signal can occur when you have two threads where one calls "notify()" before the other calls "wait()".
In some cases, this may result in the waiting thread waiting forever and never waking up because the signal to wake up was missed.
In order to avoid losing signals, they should be stored in a member variable inside the signal class.
In the above-mentioned modified version of signalling implementation, the thread calling the "doNotify()" method sets the isSignalled variable to true before calling notify().
Likewise, the thread calling the "doWait()" method now checks the "isSignalled" variable before calling wait() and only calls wait() if no signal was received in between.
Exercise
Print alternate even and odd numbers using two threads
Given an integer "n," the task is to print numbers from "1" to "n" in increasing order using two threads.
Also, make sure that even numbers are printed by one thread and odd numbers are printed by other thread.
Print Even : 1 Print Odd : 2 Print Even : 3 Print Odd : 4 Print Even : 5 Print Odd : 6 Print Even : 7 Print Odd : 8 Print Even : 9 Print Odd : 10