You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code fails to deal with spurious wakeup. It is allowed that the thread is notified without a reason. To deal with this situation, the if(!completed) should be while(!completed). Of course the remaining timeout needs to be corrected.
@Override
public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
synchronized (this) {
if (!isCompleted) {
unit.timedWait(this, timeout);
}
if (isCompleted) {
if (exception == null) {
return null;
} else {
throw new ExecutionException(exception);
}
} else {
throw new TimeoutException();
}
}
}
The text was updated successfully, but these errors were encountered:
A thread can also wake up without being notified, interrupted, or
timing out, a so-called spurious wakeup. While this will rarely
occur in practice, applications must guard against it by testing for
the condition that should have caused the thread to be awakened, and
continuing to wait if the condition is not satisfied.
The following code fails to deal with spurious wakeup. It is allowed that the thread is notified without a reason. To deal with this situation, the if(!completed) should be while(!completed). Of course the remaining timeout needs to be corrected.
The text was updated successfully, but these errors were encountered: