File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait() and notify() methods

 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Sravan Kumar
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Arno den Hond
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Michael Valentino
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • 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....
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic