The notifyAll method should be called for an object only when the current thread has already locked the object's lock. Every thread in the wait set for the object is removed from the wait set and re-enabled for thread scheduling. (Of course, those threads will not be able to proceed until the current thread relinquishes the object's lock.)
- Note the differences between blocked and waiting.
Blocked Waiting Thread is waiting to get a lock on the monitor. (or waiting for a blocking i/o method) Thread has been asked to wait. (by means of wait method) Caused by the thread tried to execute some synchronized code. (or a blocking i/o method) The thread already acquired the lock and executed some synchronized code before coming across a wait call. Can move to ready only when the lock is available. ( or the i/o operation is complete) Can move to ready only when it gets notified (by means of notify or notifyAll)
One thread gets it, and the others go back to waiting.
Originally posted by Cherry Mathew:
I want to get one more doubt clarified.
I know that when a wait() is called the thread goes into the wait state but will a thread go into the wait or blocked state when it is tries to execute a synchronized block where lock is with another thread.
Wont it be better if all the threads waiting for the lock goes into the wait state and notify() method called at the end of each synchronized block.
Originally posted by V Srinivasan:
I got it, you are right. It was my mind boggling issue for the past one week to "Explicitly" put on pen the flow of the Thread. V Srinivasan