• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interview q

 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are methods wait, notify,notifyall in Object class and not Thread class.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic