GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Notify method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Notify method" Watch "Notify method" New topic
Author

Notify method

Hardik Raja
Ranch Hand

Joined: Feb 07, 2006
Posts: 117
Hello Friends,



Although the lock(of object r) will be released when the synchronized block completes...

When are the threads waiting for lock on object(r) are notified ..?

At the end of the synchronized block or when this line is executed.....r.notifyAll()...??;

Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Hardik
Raja

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.

Thanks

Anil Kumar

[ May 31, 2007: Message edited by: anil kumar ]
[ May 31, 2007: Message edited by: anil kumar ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
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.
Hardik Raja
Ranch Hand

Joined: Feb 07, 2006
Posts: 117
One more question to add.....

when does a Thread returns to Runnable state...after notify() is issued or after notify() + lock released...??
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
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).
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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.


Thanks,


cmbhatt
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Notify method