wood burning stoves 2.0
The moose likes Threads and Synchronization and the fly likes wait(),notify()&notifyAll() of Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait(),notify()&notifyAll() of Object" Watch "wait(),notify()&notifyAll() of Object" New topic

wait(),notify()&notifyAll() of Object

Harshada Deshmukh

Joined: Mar 25, 2007
Posts: 12
The methods wait(),notify()¬ifyAll() are all required in multithreading then why they belong to Object class not Thread class
pls. explain me.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
It is the object which has to wait for a monitor to finish. That is why Object has the wait() method in.
In multithreading waiting is something which every object might ahve to do. That is why it is in the Object class.
When the Object has finished doing whatever it has to do, it holds the responsibility for notifying other Objects it has finished, not the Thread. The Thread doesn't "know" that a particular process has finished so the objects can notify one another.
Harshada Deshmukh

Joined: Mar 25, 2007
Posts: 12
when we are creating class which extend Thread or implement runnable,we creats the instance of that particular class called thread instance that are sending the message to each otherlike wait(),notify() then why it is
not in thread?
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
I understand your concern, but the reply by Ritchie was crystal clear. We don't request the thread to wait, we call it on the object, whose lock the thread possesses. So the behaviour is Object related not thread related.

By instructing the object to wait, we allow its lock to be released and available for other threads, operating on the same object, to access some resource of that object.

Hope it helps.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Think of the monitor as a thing you have to have in your hand before you can do something, say the restroom key in a crowded office. If you're waiting for the key to become available you don't much care (or really want to know) who has it right now. When they return the key to the hook on the wall, they fire notify() on the key, and somebody who is waiting on the key gets notified. If the returner fires notifyAll() then a bunch of people would run to get the key, but only one can win.

Let's say you are a thread standing around waiting, and the person who has the key is another thread running around the building. You wouldn't wait on your own thread because you don't have the key. You don't know who the other thread is so you can't wait on them. If you could find out and wait on the other thread he wouldn't know if you want to be notified about the key or something else he might be carrying around. So you wait on the key.

Now I have to run to the WC.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anindita Basak

Joined: May 11, 2007
Posts: 1
thread can be made waited by using wait() method how can be realised.. in prog like sleep() method as far as we use it in try & catch block ....
multithreading oprations helps us in operating sys design ... in details how can we xplain it with help of core java platform??
I agree. Here's the link: http://aspose.com/file-tools
subject: wait(),notify()&notifyAll() of Object
It's not a secret anymore!