• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Notify method

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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,
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic