The following is a translation of an atomic wait in Java.
Here what is the need for while? Isn’t if sufficient?
///<await (condition) statements; >
synchronized(obj)
{
while ( !condition)
{
obj.wait();
}
statements;
}
You need the
while, because just because someone sent you anotifydoes not mean that the condition is now true (or is still true).All
notifydoes (or is supposed to do) is tell the waiting threads (of which there can be many, with different conditions to wait for, even on the same monitor) that now would be a good time to re-evaluate if they still need towait.