Hi all,
The following are my understanding of
1) Thread execute synchronized keyword
2) Thread execute wait() code
Please feel free to know if you disagree with any of them.
First check what happens
when Thread execute synchronized keyword : Step 1) Thread is in Ready state.
Step 2) at some instance of time, The thread scheduler picks the thread and assign CPU.
Step 3) Now, Thread is running
Step 4) at some instance of time , synchronized keyword is executed in the thread. it is necessary that the thread to get the object lock of this object or some other specified object.
Step 5) The thread cannot continue until the lock is obtained . So it is blocked from execution .
Step 6) The thread scheduler puts the Thread back to Ready state
Step 7) When the lock is free ,
The Thread scheduler picks the thread, assigns the lock to this thread and assign CPU.
Step 8) Thread continue executing the synchronized block .
Step 9) When the synchronized block is over ,the object lock is freed .
Step 10) Thread continues executing the other codes.
Next what happens when
Thread execute wait() : .
Lets assume we are at the
Step 6 of the Thread execute synchronized keyword .
Step 6) Thread continue executing the synchronized block .
Step 6.1) at some instance of time , wait() is executed in the thread.
Step 6.2) First checks whether this thread is the owner of the object lock on which wait() is called.
If "no " IllegalMonitorStateException is thrown.
If "yes" next step .
Step 6.3) The
thread scheduler frees the object lock , and puts the thread in waiting pool of the object.
Every operation about wait() is over Now.
The threads in the waiting pool are waiting for scheduler to get them out of waiting pool.The Scheduler needs notify()/notifyAll/timeout to put them to Ready state. In the wait state they are not waiting for object lock or any thing else they are just waiting for scheduler to get them out of waiting pool.
When some other thread have this object lock, notifies or timeout for waiting is over ,The
thread scheduler picks one of the thread from this objects waiting pool and puts the thread in Ready state.
[If it was notifyAll () The
thread scheduler picks all of the threads from this objects waiting pool and puts the threads in Ready state.]
Next the thread have to continue the execution of the synchronized code. So continues from Step 7 of the Thread execute synchronized keyword .
Step 7)When the lock is free ,the
Thread scheduler picks the thread, assigns the lock to this thread and assign CPU.
Step 8) Thread continue executing the synchronized block .
Step 9) When the synchronized block is over ,the object lock is freed .
Step 10) Thread continues executing the other codes.
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.
Cherry ,
All the threads waiting for the lock ,not goes into the wait state that is "waiting pool " of the object .
Don't confuse with
execution of synchronized keyword and
execution of wait() .
synchronized keyword : All the threads waiting for the lock, all goes to Ready state, just blocked from execution .When the object lock is free the scheduler picks them and assigns the object lock. There is no need for notify()/notifyAll().
wait() : All threads called wait() goes to waiting pool of the object. Needs notify()/notifyAll/timeout go to Ready state. In the wait state they are not waiting for object lock. They are just waiting for scheduler to get them out of wait state.
Please feel free to know if you agree or disagree with any of them above.
Rosemol.