In Joshua Bloch's
Effective Java, chapter 50 is entitled "Never invoke
wait outside a loop." To summarize, the availability condition must be checked both
before the
wait, to avoid waiting forever for a notification that will never come; and
after the
wait, to ensure safety: some other
thread could have grabbed the resource between the
wait and the use of the shared resource, or some other thread could have called
notify or
notifyAll when the resource wasn't really available.
Habibi et al's
SCJD prep book puts it differently:
An if statement will resume exactly where it left off... If the value of [the while-condition] has been changed by another thread... in the meantime the if statement will not recheck it.... A while loop, on the other hand, will always recheck the condition before resuming thread execution.
[ December 06, 2004: Message edited by: Stephen Bloch ]