The Object.wait()
method is used to temporarily cede possession of a lock so that another requesting thread can proceed. It must always be used inside a synchronized
block or method. To let the waiting thread resume, the requesting thread must notify it. Furthermore, the wait()
method should be invoked in a loop that checks if a condition predicate holds. Note that a condition predicate is not the same as the condition expression in the loop. For example, the condition predicate for removing an element from a vector is !isEmpty()
whereas the condition expression for the while loop condition is isEmpty()
. The correct way to invoke wait()
is shown below.
...