Java Reference
In-Depth Information
For all these reasons, when you wake up from wait you must test the condition predicate
again , and go back to waiting (or fail) if it is not yet true. Since you can wake up repeatedly
without your condition predicate being true, you must therefore always call wait from with-
in a loop, testing the condition predicate in each iteration. The canonical form for a condition
wait is shown in Listing 14.7 .
Listing 14.7. Canonical Form for State-dependent Methods.
When using condition waits ( Object.wait or Condition.await ):
Always have a condition predicate—some test of object state that must hold before
proceeding;
Always test the condition predicate before calling wait , and again after returning
from wait ;
Always call wait in a loop;
Ensure that the state variables making up the condition predicate are guarded by the
lock associated with the condition queue;
Hold the lock associated with the the condition queue when calling wait , notify ,
or notifyAll ; and
Do not release the lock after checking the condition predicate but before acting on it.
Search WWH ::




Custom Search