Why do we need notify() for inter thread communication

ghostrider :

From the JAVA docs for Object notify()

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.

This means that unless the Thread which notifes, its synchronized block is complete and it releases the lock, the waiting thread cannot proceed. If that's the case then whats the point of having notify() if the sync block is going to be executed anyway? What's the actual use of notify() if it doesn't wake up the waiting thread and let it do its job?

Khanna111 :

Good question. Will point you to take a look at the Thread State Class.

A thread that calls the Object.notify method enables a thread that previously called Object.wait is now enabled to be scheduled by the thread scheduler. In parlance, the thread that was waiting is now "runnable". Although it is "runnable", it is not "running".

It can only continue running when the thread invoking notify releases the lock - one way is when it exits out of the synchronized block.

There are a lot of schematics on the web on the Thread States. Some of them are completely incorrect or confusing since they introduce terminology not in the official docs. Here is one that makes sense to me.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=415836&siteId=1