Use of wait() and notify() in Java

wait() and notify()

1. Both wait() and notify() methods belongs to threads but they are not defined in java.lang.Thread class, instead they are defined in the Object class.
2. Both the methods must called from synchronized context to avoid IllegalMonitorStateException.
3. When the wait() method is called by a thread in Java, then this thread release the lock and go to the wait state. Now this lock is acquired by the other thread who can notify this thread.
4. A thread in wait state due to a call to wait() method can wake up either by calling notify() or notifyAll() methods on the same object or due to interruption.
5. When notify() method is called on a shared object and if more than one thread is waiting on that lock then any thread will get the notification, it is not guaranteed. If only one thread is waiting then it will get the notification.

Java Professional with rich experience in Java development.
– Designing and developing high-volume, low-latency applications with high-availability and performance.
– Writing well designed, robust and efficient code.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By :