Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spurious wakeup bug CompletionListenerFuture #348

Closed
pveentjer opened this issue May 17, 2016 · 2 comments
Closed

Spurious wakeup bug CompletionListenerFuture #348

pveentjer opened this issue May 17, 2016 · 2 comments
Assignees
Labels
Milestone

Comments

@pveentjer
Copy link

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();
      }
    }
  }
@cruftex
Copy link
Member

cruftex commented May 17, 2016

Good finding. Just learned something.

Its described at Object.wait():

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.

@cruftex
Copy link
Member

cruftex commented Jun 10, 2016

Further tracked by #320

@cruftex cruftex closed this as completed Jun 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants