static final class AbstractFuture.Sync<V>
extends java.util.concurrent.locks.AbstractQueuedSynchronizer
Following the contract of AbstractQueuedSynchronizer
we create a
private subclass to hold the synchronizer. This synchronizer is used to
implement the blocking and waiting calls as well as to handle state changes
in a thread-safe manner. The current state of the future is held in the
Sync state, and the lock is released whenever the state changes to
COMPLETED
, CANCELLED
, or INTERRUPTED
To avoid races between threads doing release and acquire, we transition to the final state in two steps. One thread will successfully CAS from RUNNING to COMPLETING, that thread will then set the result of the computation, and only then transition to COMPLETED, CANCELLED, or INTERRUPTED.
We don't use the integer argument passed between acquire methods so we pass around a -1 everywhere.
Modifier and Type | Field and Description |
---|---|
(package private) static int |
CANCELLED |
(package private) static int |
COMPLETED |
(package private) static int |
COMPLETING |
private java.lang.Throwable |
exception |
(package private) static int |
INTERRUPTED |
(package private) static int |
RUNNING |
private static long |
serialVersionUID |
private V |
value |
Constructor and Description |
---|
Sync() |
Modifier and Type | Method and Description |
---|---|
(package private) boolean |
cancel(boolean interrupt)
Transition to the CANCELLED or INTERRUPTED state.
|
private boolean |
complete(V v,
java.lang.Throwable t,
int finalState)
Implementation of completing a task.
|
(package private) V |
get()
Blocks until
complete(Object, Throwable, int) has been
successfully called. |
(package private) V |
get(long nanos)
Blocks until the task is complete or the timeout expires.
|
private V |
getValue()
Implementation of the actual value retrieval.
|
(package private) boolean |
isCancelled()
Checks if the state is
CANCELLED or INTERRUPTED . |
(package private) boolean |
isDone()
|
(package private) boolean |
set(V v)
Transition to the COMPLETED state and set the value.
|
(package private) boolean |
setException(java.lang.Throwable t)
Transition to the COMPLETED state and set the exception.
|
protected int |
tryAcquireShared(int ignored) |
protected boolean |
tryReleaseShared(int finalState) |
(package private) boolean |
wasInterrupted()
Checks if the state is
INTERRUPTED . |
acquire, acquireInterruptibly, acquireShared, acquireSharedInterruptibly, compareAndSetState, getExclusiveQueuedThreads, getFirstQueuedThread, getQueuedThreads, getQueueLength, getSharedQueuedThreads, getState, getWaitingThreads, getWaitQueueLength, hasContended, hasQueuedPredecessors, hasQueuedThreads, hasWaiters, isHeldExclusively, isQueued, owns, release, releaseShared, setState, toString, tryAcquire, tryAcquireNanos, tryAcquireSharedNanos, tryRelease
private static final long serialVersionUID
static final int RUNNING
static final int COMPLETING
static final int COMPLETED
static final int CANCELLED
static final int INTERRUPTED
private V value
private java.lang.Throwable exception
protected int tryAcquireShared(int ignored)
tryAcquireShared
in class java.util.concurrent.locks.AbstractQueuedSynchronizer
protected boolean tryReleaseShared(int finalState)
tryReleaseShared
in class java.util.concurrent.locks.AbstractQueuedSynchronizer
V get(long nanos) throws java.util.concurrent.TimeoutException, java.util.concurrent.CancellationException, java.util.concurrent.ExecutionException, java.lang.InterruptedException
TimeoutException
if the timer expires, otherwise behaves like
get()
.java.util.concurrent.TimeoutException
java.util.concurrent.CancellationException
java.util.concurrent.ExecutionException
java.lang.InterruptedException
V get() throws java.util.concurrent.CancellationException, java.util.concurrent.ExecutionException, java.lang.InterruptedException
complete(Object, Throwable, int)
has been
successfully called. Throws a CancellationException
if the task
was cancelled, or a ExecutionException
if the task completed with
an error.java.util.concurrent.CancellationException
java.util.concurrent.ExecutionException
java.lang.InterruptedException
private V getValue() throws java.util.concurrent.CancellationException, java.util.concurrent.ExecutionException
java.util.concurrent.CancellationException
java.util.concurrent.ExecutionException
boolean isDone()
boolean isCancelled()
CANCELLED
or INTERRUPTED
.boolean wasInterrupted()
INTERRUPTED
.boolean set(@Nullable V v)
boolean setException(java.lang.Throwable t)
boolean cancel(boolean interrupt)
private boolean complete(@Nullable V v, @Nullable java.lang.Throwable t, int finalState)
v
or t
will
be set but not both. The finalState
is the state to change to
from RUNNING
. If the state is not in the RUNNING state we
return false
after waiting for the state to be set to a valid
final state (COMPLETED
, CANCELLED
, or INTERRUPTED
).v
- the value to set as the result of the computation.t
- the exception to set as the result of the computation.finalState
- the state to transition to.