You would need to use notifyAll
to catch all cases (typically notify
is more specific). Your while loop (don't use an if
) protects against continuing in unwanted cases.
More efficiently, but also more verbosely, you could use Condition
from the locks in java.util.concurrent.locks
.