wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes wait() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait()" Watch "wait()" New topic
Author

wait()

shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Hi,
This is from the JLS:
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:

Some other thread invokes the notify method for that object and thread T happens to be the one arbitrarily chosen as the one to notify.
Some other thread invokes the notifyAll method for that object.
If the call by thread T to the wait method specified a timeout interval, the specified amount of real time has elapsed.
The thread T is then removed from the wait set and re-enabled for thread scheduling. It then locks the object again (which may involve competing in the usual manner with other threads); once it has gained control of the lock, it performs additional lock actions and then returns from the invocation of the wait method. Thus, on return from the wait method, the state of the object's lock is exactly as it was when the wait method was invoked.
I just don't understand the thing given in bold letters.
The thread has N locks on the object, it encounters a wait() and then releases the N locks. when it gets the next chance to gain a lock on the object, why should it have N-1 locks? And, what does it mean when we say that on return from the wait method, the state of the object's lock is exactly as it was when the wait method of the object is invoked
------------------
Regards,
Shree


Regards,<BR>Shree
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Shree,
A thread may lock the same object multiple times (call this number N). If the thread then calls <code>wait()</code> on that object, all of the thread's locks (all N of them) on that object are released. When the thread leaves the object's wait set, it then contends again for the object's lock. Once it regains the lock, N-1 additional locking operations are performed before <code>wait()</code> returns, so that the thread holds the same number of locks on the object as it did when <code>wait()</code> was called.
The "state of the object" includes what threads are in the object's wait set, what threads are contending for the object's lock, what thread (call it T) currently holds the object's lock, and how many times T has locked the object.
Jerry
[This message has been edited by Jerry Pulley (edited December 27, 2000).]
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Originally posted by Jerry Pulley:
Shree,
A thread may lock the same object multiple times (call this number N). If the thread then calls <code>wait()</code> on that object, all of the thread's locks (all N of them) on that object are released. When the thread leaves the object's wait set, it then contends again for the object's lock. Once it regains the lock, N-1 additional locking operations are performed before <code>wait()</code> returns, so that the thread holds the same number of locks on the object as it did when <code>wait()</code> was called.The "state of the object" includes what threads are in the object's wait set, what threads are contending for the object's lock, what thread (call it T) currently holds the object's lock, and how many times T has locked the object.
Jerry
[This message has been edited by Jerry Pulley (edited December 27, 2000).]

Hi Jerry,
I guess the context in which we have to understand this is something like this (correct me if i am wrong)
</code>
synchronized (this)//first lock
{
synchronized (this) // second lock
{
// and so on upto
synchronized (this) // Nth lock
{
wait();
}
}
}
so, if the thread obtains the lock of the object after leaving the wait set and going through the scheduling routine, it will again get N locks on the objects ( 1 it has got first + N=1 within) is that interpretation correct?

------------------
Regards,
Shree
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Shree,
Yup, that's how it works.
Jerry
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Thanks,Jerry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait()
 
Similar Threads
Calling wait() when we have obtained two or more locks
Thread wait
object.wait()
Dan's: Blocked vs Waiting - possible mistake
Thread Question