File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Jobs Discussion and the fly likes Interview q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Careers » Jobs Discussion
Bookmark "Interview q" Watch "Interview q" New topic

Interview q

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Why are methods wait, notify,notifyall in Object class and not Thread class.

chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
I don't know if I understand your question correctly, but you must have an object associated with a thread and they will be in a wait state, just like when you synchronize, you synchronize on an object.
Here is an explanation for those terms maybe it will be clearer:
Wait : synchronized, will release the lock and do other process before it continues. Uses try-catch block exceptions. There is a wait method with a timeout in ms. After the time is exhausted, the Thread attempts to proceed but is allowed to do so only if no other Thread has a lock on the object. This method is handy when you are not sure that another Thread will be able to call notify, because it guarantees that the Thread will not be stuck in the wait state forever.
Notify : synchronized , when called, a Thread is removed from the wait set and returned to the list of Runnable Threads.
NotifyAll : synchronized, call if there is more than one thread waiting. This removes all waiting threads from the wait list. Only one of these actually get the lock on the object and is allowed to execute the synchonized method; the others run and find that the object is still locked.
If a thread has a lock on one or more objects dies, the JVM removes the locks and does the equivalent of notifyAll for each object locked.
chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
Ok, I understand what you are talking about, this article will answer your question:
I agree. Here's the link:
subject: Interview q
It's not a secret anymore!