• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

wait() and notify() methods

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We had wait and notify methods in Object class? Why are the wait and notify methods provided in the Object class instead of in the Thread class, though these methods are used by a Thread.


Thank you
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You call wait() and notify() on an object from within a synchronized context (synchronized block or method)

Calling wait() on an object causes the thread to wait (be blocked) till it can get hold of this object's lock (Every object has a lock associated with it)

notify() is called on an Object from within a synchronized context when a Thread is about to exit the context. This notifies a Thread that is waiting for this object's lock.

Try developing producer-consumer problem for some practical work on this.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, if wait is called from within a synchronized block, the calling thread releases the lock on which that block is synchronized thus allowing other threads to enter the synchronized block.
btw, isnt it also possible to call notify not from within a synchronized block? quite usefull in case you want to tell some other thread to continue running without any synchronization.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it's possible to call notify() or notifyAll() outside of a synchronized block or method. After all, the point of notify() and notifyAll() is to coordinate synchronized access to an object....
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Calling wait() on an object causes the thread to wait (be blocked) till it can get hold of this object's lock (Every object has a lock associated with it)



It's actually the synchronized keyword that behaves as you describe. wait() is a completely different ballgame. For a thread to call wait() on an object it must first be the owner of that object's monitor. It is not waiting to acquire the monitor, it already owns it. What it does is release the monitor and then wait until another thread notifies threads waiting on that monitor to wake up either through notify() or notifyAll().

notify() is called on an Object from within a synchronized context when a Thread is about to exit the context. This notifies a Thread that is waiting for this object's lock.



A thread gives up ownership of a monitor when it leaves the critical section or "synchronized context" as you described it. notify() is used to wakeup any object waiting on the monitor. Note this does not mean they were waiting to get ownership of the monitor (they already had it when they called wait() to begin with), simply that they invoked wait() on the same object you're invoking notify() or notifyAll() on. When a thread that's waiting is woken up it will try to get ownership of the monitor and will be blocked until it can. This should be no surprise as wait() was called from a critical section to begin with. Since notify() and notifyAll() can only be called from a thread that owns the monitor it should also be no surprise that any threads woken up can't proceed (they need the monitor) until the current thread relinquishes it.

btw, isnt it also possible to call notify not from within a synchronized block? quite usefull in case you want to tell some other thread to continue running without any synchronization.



No.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic