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.
Joined: Mar 25, 2007
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?
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.
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
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??