Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interview q

 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are methods wait, notify,notifyall in Object class and not Thread class.
 
chad stevens
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I understand what you are talking about, this article will answer your question:
http://java.ittoolbox.com/documents/document.asp?i=2185&r=default.asp
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic