Guarded Suspension

Origin: Lea 97
Reason: A method should wait to execute until a precondition holds.
Synopsis: Use an object's methods wait and notify.
Example: A queue is used by two threads to exchange objects. A thread can take an object from the queue only if the queue contains elements.
Solution: In its simplest form, the method guarded by a precondition looks like:
synchronized void guardedMethod () {
    while (! precondition) { wait (); }
        ... // code requiring precondition
}
Any method that might change the precondition looks like:
synchronized void stateChangingMethod () {
    ... // code possibly changing precondition
    notify ();
}
Both methods are synchronized on the same object.
See also: Balking (is similar to guardedMethod, but it returns rather than waiting)