However, the lock is not actually given up until the notifier’s synchronized block has completed. It tells a waiting thread that that thread can wake up. It should be noted that calling notify() does not actually give up a lock on a resource. It wakes up one single thread that called wait() on the same object. General syntax for calling wait() method is like this: In other words, it is not possible for us to implement the wait() method purely in Java.
The wait() method is actually tightly integrated with the synchronization lock, using a feature not available directly from the synchronization mechanism. The wait() method releases the lock prior to waiting and reacquires the lock prior to returning from the wait() method. It tells the calling thread to give up the lock and go to sleep until some other thread enters the same monitor and calls notify(). The Object class in Java has three final methods that allow threads to communicate about the locked status of a resource. What are wait(), notify() and notifyAll() methods? Read more : Difference between wait() and sleep() in Java 1. We will understand the difference between wait and notify. In this tutorial, I am discussing the purpose of wait() notify() notifyall() in Java. A good knowledge around these methods will help you in such situation when arrived. I will also recommend to use these newer APIs over synchronization yourself, BUT many times we are required to do so for various reasons e.g. Programmers using concurrency classes will feel a lot more confident than programmers directly handling synchronization stuff using wait(), notify() and notifyAll() method calls. Java 5, introduced some classes like BlockingQueue and Executors which take away some of the complexity by providing easy to use APIs.
Java concurrency is pretty complex topic and requires a lot of attention while writing application code dealing with multiple threads accessing one/more shared resources at any given time.