One thing I've never understood with Java threads
From SUN's java.lang.Object Javadoc:
- 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...(Cyberpunk, 2003/07/08 11:01) lien permanent

As soon as you wait, the monitor is released.
Posté par Chris Nokleberg, le mardi 8 juillet 2003 à 10:43 #
Ok, that's a bit confusing, since I'm still in a synchronised block for instance.
Posté par Damien B, le mardi 8 juillet 2003 à 10:54 #
Well, it appears that one must read all three wait() Javadoc description to get the full picture.
Posté par Damien B, le mardi 8 juillet 2003 à 11:02 #
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
Posté par Alan Green, le mardi 8 juillet 2003 à 19:29 #
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).
Posté par Damien B, le mercredi 9 juillet 2003 à 05:31 #