JavaGreenhorn
JavaGreenhorn

Reputation: 61

Threading - wait()

The wait() method on an object can be called only in the synchronized context i.e. the current thread must have a lock on the object to invoke the wait() method. Now if a thread T1 has a lock on an object(obj) and invokes its wait method obj.wait() . How can other threads get lock on this object(obj) so that they can also call wait, which is already possessed T1 ?

Upvotes: 3

Views: 282

Answers (2)

phihag
phihag

Reputation: 288298

wait releases the synchronized context. From the documentation:

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up (...)

Upvotes: 9

SingleShot
SingleShot

Reputation: 19131

You only need to be synchronized for the duration of calling the wait() method, not for the duration of the wait time.

Upvotes: 0

Related Questions