Difference Between wait() and notify() in Java
The wait() and notify() are methods of the Object class. They were introduced to part ways with polling, which is the process of repeatedly checking for a condition to be fulfilled. Polling wastes CPU resources considerably, hence it is not preferred.
wait() method is a part of java.lang.Object class. When wait() method is called, the calling thread stops its execution until notify() or notifyAll() method is invoked by some other Thread.
The wait() method has 3 variations:
1. wait(): This is a basic version of the wait() method which does not take any argument. It will cause the thread to wait till notify is called.
public final void wait()
2. wait(long timeout): This version of the wait() method takes a single timeout argument. It will cause the thread to wait either till notify is called or till timeout (One which occurs earlier).
public final void wait(long timeout)
3. wait(long timeout, int nanoseconds): This version of the wait() method takes a timeout argument as well as a nanosecond argument for extra precision.
public final void wait(long timeout, int nanoseconds)
The notify() method is defined in the Object class, which is Java’s top-level class. It’s used to wake up only one thread that’s waiting for an object, and that thread then begins execution. The thread class notify() method is used to wake up a single thread.
public final void notify()
Differences between wait() and notify()
|1.||When wait() is called on a thread holding the monitor lock, it surrenders the monitor lock and enters the waiting state.||When the notify() is called on a thread holding the monitor lock, it symbolizes that the thread is soon going to surrender the lock.|
|2.||There can be multiple threads in the waiting state at a time.||
One of the waiting threads is randomly selected and notified about the same. The notified thread then exits the waiting state and enters the blocked state where it waits till the previous thread has given up the lock and this thread has acquired it. Once it acquires the lock, it enters the runnable state where it waits for CPU time and then it starts running.
|3.||The wait() method is defined in the Object class||The notify() method is defined in the Object class|
|4.||The wait() method is used for interthread communication.||The notify() method is used to wake up a single thread|
|5.||The wait() method is a part of java.lang.Object class||The notify() method does not have any return type value|
|6.||The wait() method is tightly integrated with the synchronization lock||The notify() method is used to give the notification for one thread for a particular object|
Below is demonstration of wait() and notify() method:
Thread t2 waiting Welcome to India Thread t1 about to surrender lock Thread t2 running again Do visit Taj Mahal
- 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 InterruptedException.
- The other two variations of wait housing parameters will throw IllegalArgumentException if the value of timeout is negative or the value of nanoseconds is not in the range 0 to 9,99,999.
Below is the implementation for the exception handling.
Welcome to India Exception : class java.lang.InterruptedException
Unlike wait(), the notify method does not throw an InterruptedException hence it is not mandatory to house it inside a try-catch block
- wait() and notify() both have a tendency to throw IllegalMonitorStateException
- This occurs when a thread is holding the monitor lock of object A and tries to call wait or notify on object B.
- In all the preceding examples, the methods were synchronized on “this” i.e. the object used to call those methods (obj). Also, the wait() & notify() were being called as this.wait() and this.notify() (usage of this was redundant). Hence, there was no issue.
- In the below example, the methods part1 and part2 are now synchronized on an Integer object, but wait() & notify() are still being called on the object which called these methods (obj).
- This causes an IllegalMonitorStateException
Below is the implementation of the exception handling.
Exception: class java.lang.IllegalMonitorStateException
Note: To fix the above code, just replace notify() with a.notify() on line 17 and wait() with a.wait() on line 29.