permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes wait() and notify() methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait() and notify() methods" Watch "wait() and notify() methods" New topic
Author

wait() and notify() methods

Vallabhaneni Suresh Kumar
Ranch Hand

Joined: Mar 01, 2004
Posts: 68
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

Joined: Sep 11, 2005
Posts: 121
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.


keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
Arno den Hond
Greenhorn

Joined: Nov 02, 2005
Posts: 4
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

Joined: Nov 01, 2005
Posts: 96
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....


SCJP 1.4, SCWCD J2EE 1.4, SCJD J2SE 1.5, SCBCD J2EE 1.3, SCDJWS (In Progress)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() and notify() methods
 
Similar Threads
Interview q
thread question
Why wait and notify are methods of Object?
wait, notify and notifyAll
Threads