- wait
- Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. The current thread must own this object's monitor.
- notify
- This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways: * By executing a synchronized instance method of that object. * By executing the body of a synchronized statement that synchronizes on the object. * For objects of type Class, by executing a synchronized static method of that class. Only one thread at a time can own an object's monitor.
- notifyAll
- Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods. This method should only be called by a thread that is the owner of this object's monitor.
So, in order to call wait(), I must have the object monitor... but at that moment my thread is blocked, so I can't release the monitor, so nobody can call notify() or notifyAll() on my object. What am I missing?
Update: I've something working, I'm still not sure why it works, I'll post the code later this evening
Update 2: as Chris Nokleberg mentionned, the monitor on the object is lost when wait is called, and this is written in the Javadoc... lost between other things. By the way, the description of wait(), wait(long) and wait(long, int) should be homogenous...
5 réactions
1 De Chris Nokleberg - 08/07/2003, 10:43
As soon as you wait, the monitor is released.
2 De Damien B - 08/07/2003, 10:54
Ok, that's a bit confusing, since I'm still in a synchronised block for instance.
3 De Damien B - 08/07/2003, 11:02
Well, it appears that one must read all three wait() Javadoc description to get the full picture.
4 De Alan Green - 08/07/2003, 19:29
Giving wait() magical monitor releasing powers has always looked like a hack to me. On the plus side, this kind of threading is not something that we have to worry about very often.
Here's some more general notes about threading: http://www.cardboard.nu/archives/000047.html
5 De Damien B - 09/07/2003, 05:31
Thanks for the link, now I know I've not solid grounding for Java threads :-)
Regarding your heuristics, I think wait()/notify() i s a good candidate for a time-out scenario on an interruptible task (see code in the following post).