A given thread is required to own a lock on a object to be able to call wait(long)
on it. This is achieved by using a synchronized block on the said object.
See J2SE specification on using wait
.
Acquiring a lock/monitor in java can be done in various ways:
- In a
synchronized
(non-static) method, the thread owns a monitor on the object referenced bythis
. - In a
static synchronized
method, the thread owns a monitor on theClass<?>
descriptor for the class that defines the said method. - In a
synchronized(x)
block, the thread owns a monitor onx
.
That lock will be released if:
- You get outside of the synchronized code block (be it a method, static method, or explicit block).
- You have called
wait()
or one of its variations (and you'll re-acquire it just before the method returns).
Both these two lists may omit specific cases but should cover at least a large portion of the typical use cases.