Consider the following code taken from this article. It implements something similar to a CompletableFuture for learning purposes.
Here's the get()
function of WaitingFuture
:
@Override
public V get() throws ExecutionException {
this.thread = Thread.currentThread();
LockSupport.park(this);
if (throwable != null) {
throw new ExecutionException(throwable);
}
return result;
}
And here's the run()
function of RunnableWaitingFuture
:
@Override
public void run() {
try {
waitingFuture.result = userFunction.get();
} catch (Throwable throwable) {
waitingFuture.throwable = throwable;
} finally {
waitingFuture.finished = true;
LockSupport.unpark(waitingFuture.thread);
}
}
}
The Question:
It seems to me that if run()
will finish before get()
is even called then LockSupport.park(this);
will be called after LockSupport.unpark(waitingFuture.thread)
, leaving the thread parking forever.
Is that true?