After execution of notifyAll() ,the lock may not be released because there may be some code with the locked object.Here in your case one statement is there so the lock won't be released ,But after completion of execution of the synchronized method it will be released.
The threads will be notified after calling notifyAll().But they will go to Read-to- run state.But the lock may not be released.
[ May 31, 2007: Message edited by: anil kumar ] [ May 31, 2007: Message edited by: anil kumar ]
Hi, As anil said, eventhough you call notifyAll(), but still the current thread which is holding on the object is still under processing. Therefore it seems to be meaningless if the lock is released soon after the notifyAll() method is executed.
You may think that irrespective of the place where notifyAll() is called, but it says that the current thread has decided to say that the other threads waiting can be notified. So the lock can be released and the next thread in the waiting queue will be given the lock.
In this case, both the threads seem to be running (provided if the previous thread is still not yet finished its processing). But, the 'synchronized' keyword will lose it meaning. Right?
Thats how i think the lock will be released only after the end of exeuction of the current method.
Does this make sense? Fellow ranchers, please suggest if i missed something.
The threads waiting on the lock are waiting because of invocation of a wait() method on the object refered by reference 'r'. the wait method has 3 steps internally. 1. release the lock 2. wait for a notification (which completes with notify or notifyAll) 3. obtain the lock (ie. it is blocked for the lock)
The Zen of Java Programming.
Joined: Feb 07, 2006
One more question to add.....
when does a Thread returns to Runnable state...after notify() is issued or after notify() + lock released...??
Joined: May 20, 2007
after notify + lock obtained for the waiting thread There is a bug with the notify method and it is unable to update the state of the waiting thread from WAITING to BLOCKED when notify is called on the object for the threads which gets the notification. The state for the waiting thread changes to RUNNABLE only when the thread obtains the lock (i.e. when the thread calling the notify/notifyAll releases the lock by leaving the synchronized block).
When are the threads waiting for lock on object(r) are notified ..?
It is not true. Threads are not notified. An object is notified and the thread scheduler picks one thread from the waiting threads pool to grab the lock of the object to perform the operation it intends to do.
In the above code, there is a lock on "r" object. Lock of the object "r" will not be released at least until the block is completed by the thread grabbing the lock of object "r". It is also not sure that after completing the block immediately the lock will be released, as is the most frequently asked question.
Joined: May 20, 2007
Notify is invoked on an Object, but threads are waiting on an Object for notification on an Object. Till a notify/notifyAll is invoked on the object the thread which invoked wait on the object is in the WAITING state. Once the notify/notifyAll is invoked the thread which is in WAITING state is supposed to be now in BLOCKED state, as is the description in the API for the BLOCKED state. BLOCKED state means it is now waiting for the lock to be released if it is held by another thread.