What is the use of wait() method?
It causes the current thread to go to leave the lock and go to waiting state.
What is the use of notify() and notifyAll() method?
- Causes one of the threads waiting on same object to leave the waiting state and go to runnable state.
- So if we have multiple threads waiting on the same object/lock, only one thread (arbitrary chosen) will be sent to runnable state.
- Causes all of the thread waiting on the same object to go to runnable state.
- In this case, all the threads sent to runnable state will try to acquire the lock but at the end only one thread will be able to acquire the lock.
If no threads are waiting in the waiting queue, then notify() and notifyAll() have no effect.
What is entry queue and waiting queue in terms of java object?
In Java any object can act as a monitor. By literal definition of monitor in concurrent system is a construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true.
In terms of Java object, it means that any object can act as an entity with:
- single lock
- entry queue
- waiting queue
When one thread have acquired the lock on an object and is executing the synchronized method, and at the same time if there is another thread trying to call synchronized method on object, in this case the other thread will be put in the blocked state and will be pushed to entry queue.
When a thread is in synchronized method, and calls wait, that thread releases the lock and is added to waiting queue.
Why a thread needs to call wait() method from synchronized block or method?
One of the important step as part of thread calling wait() method is to release the lock. A thread can only release the lock only if it has acquired it :-).
To make sure that thread have the lock on an object, Java have implemented a check to ensure thread calling the wait() method actually have the lock with itself. And best way to ensure that thread have lock on an object is that the thread is in synchronized method or synchronized block.
Why a thread needs to call notify() or notifyAll() method from synchronized block or method?
As we have read above, when a thread invokes notify or notifyAll method, it causes one or more threads to go from waiting state to runnable state. This actually means that thread or threads are taken from waiting queue and pushed to entry queue.
Now if you think carefully here, we are actually changing the state of object in terms of changing state of waiting queue. For this it is very important to own the lock of the object. If thread calling notify/notifyAll is not owning the lock and changing the waiting queue, it is very much possible that there is another thread owning the lock and have called wait() method also and hence adding itself to waiting queue. All such possibilities can lead to bad state. Hence notify/notifyAll must own the lock of the object. And again one easy way to make sure that thread have the lock, is to simply call notify/notifyAll from synchronized method/block.
What if we call from block or method which is not synchronized?
You will get java.lang.IllegalMonitorStateException: current thread not owner” at runtime.