File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notifyAll() 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 "notifyAll()" Watch "notifyAll()" New topic


Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
Does notifyAll() only notify threads synchronized on the same lock as the context that notifyAll() was called from?

So a thread that is waiting that was synchronized on another object's lock will not return to runnable when notifyAll() is called from a thread that is synchronized on a different objects lock?

Am I making sense?

SCJP 5.0
Chandran Madesh

Joined: Nov 30, 2007
Posts: 18
Hi Joshua Fix ,

can you please give your question little bit clear...


<b>Chandran.M<br /> <br /> <br /> Do Your Best <br /></b>
Peter Ricke

Joined: Dec 07, 2007
Posts: 24
notify and notifyAll will wake up one or all threads which are in the
threat-waiting pool of the object whose notify(all) methode is called.

Your question is a little confusing:
in generall, i wouldnt say "a thread is synchronized" but instead "one or more method(s) (and therefore the data accessed by this method(s) are synchronized".

to call wait on an object, a thread has to have the objects lock, which it will gain by entering a method synchronised on this object.

If now wait() is called on this object, the object`s lock is set free again for other threads and the thread calling wait is in the objects waiting pool.

Now, if an other thread gets the lock on this object, it is able to call notify on this object and therefore wake up the thread described above.

threads waiting in the waitong pool of other objects are not influenced

I hope this is helpfull

========<br />class a{<br /> a a(a a){return (a)a;}<br />}
Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
Thanks for the replies... here's some code I used to test my question:

My question was, does the call to nc.notifyAll() notify f to return to runnable and print "finished waiting". Looks like a simple test answered my own question. I had to have a block of code synchronized on "f" that calls notifyAll(). Only when I remove the commented code do I see the text "finished waiting".
I agree. Here's the link:
subject: notifyAll()
It's not a secret anymore!