The JLS states that:
The method wait should be called for an object only when the current thread (call it T) has already locked the object's lock. Suppose that thread T has in fact performed N lock actions that have not been matched by unlock actions. The wait method then adds the current thread to the wait set for the object, disables the current thread for thread scheduling purposes, and performs N unlock actions to relinquish the lock. The thread T then lies dormant until one of three things happens:
Now, I understand this business with N lock and N unlock actions, to mean that
in the case that I hold multiple locks on the same object, an equal amount of unlock actions will occur when I call wait() on that object, in such a way that when wait() is called (no matter how many locks on that object I have gained), it will become "free (able to be locked by some other
thread)" when I perform that single wait() on it.
I also suppose that if I
have gained the monitor control (however many "lock actions" that may be) on two (or in the general case, or more) separate objects, and I call wait() off of one of the object references, then I retain control of the other monitor, which practically means that if I call obj2.wait() when I have control of obj1 and obj2 locks, and I intend to be notify()d by another thread when obj2 reaches some state S, but the other thread needs to lock and perform some action on obj1 before notifying me, I will have deadlock (of at least those two threads involved).
So I have two questions: 1: am I correct in assuming that no matter how many lock actions I perform on one object, a
single wait() performed on that object (by the same thread, of course) will release the object so that other threads can lock it, and 2: that holding locks on multiple objects and then waiting on one of them will retain the locks on the others?
Thanks,
John