wait(), notify() and notifyAll()
- Two thread can communicate with each other by using wait(), notify() and notifyAll() methods.
- The thread which is expecting updation is responsible to call wait(), then immediately the thread will entered into waiting state.
- The thread which is responsible to perform updation, offer performing updation it is responsible to call notify() method. Then waiting thread will get that notification and continue its execution with those update items.
- wait(), notify(), nofityAll() method on any object, thread should be owner of that object.Means the thread should has lock of that object. i.e. the thread should be inside synchronized area.
- Hence we can call wait(), notify() and notifyAll() method only from synchronized area, otherwise get runtime exception.
- If a thread call wait(), it immediately release the lock of that particular object and entered into waiting state.
- If a thread calls notify() on any object, it release the lock of that object but may not immediately.
- Except wait(), notify() and notifyAll() there is no other method where thread releases the lock.
Valid and in-valid
- If a thread call wait(), immediately it will enter into waiting state without releasing any lock. (False)
- If a thread call wait(), it release the lock of that object but may not immediately. (False)
- If a thread call wait() on any object, it release all locks acquired by that thread and immediately entered into waiting state. (False)
- If a thread call wait() on any object, it immediately release the lock of that particular object and entered into waiting state. (True)
- If a thread call notify() on any object, it immediately releases the lock of that particular object. (False)
- If a thread call notify() on any object, it releases the lock of that object but may not immediately. (True)
SYNTAX
public final void wait() throws InterruptedException
public final native void wait(long timeout) throws InterruptedException
public final void wait(long timeout, int nanos) throws InterruptedException
public final native void notify();
public final native void notifyAll();
Note:
Every wait() method throws InterruptedException which is checked exception. Hence when ever we are using wait() method compulsory we should handle this exception either by try-catch or throws keyword. Otherwise we will get compile time error.
Ex:
Difference between notify() and notifyAll()
notify()
- We can use notify() to give the notification for only one waiting thread.
- If multiple thread are waiting, then only one thread will be notified and the remaining thread have to wait for further notification.
- Which thread will be notified we cannot expect, it depend on JVM.
notifyAll()
- We can use notifyAll() to give the notification for all waiting thread of a particular object.
- Even though multiple thread notified but execution will be performed one by one, because thread required and only one lock is available.
Note:
An which object we are calling wait(), thread required lock of that particular object.
Ex:
If we are calling wait() on p1, then we have to get lock of p1 object but not p2 object.