Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Harshada Deshmukh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Sid
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anindita Basak
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic