public abstract class AsyncFutureTask<T> extends java.lang.Object implements AsyncFuture<T>
Future as well as AsyncFuture, and
is approximately equivalent to FutureTask, however it
does not implement Runnable and is somewhat more flexible.| Modifier and Type | Class and Description |
|---|---|
private class |
AsyncFutureTask.Reg<A> |
AsyncFuture.AbstractListener<T,A>, AsyncFuture.Listener<T,A>, AsyncFuture.Status| Modifier and Type | Field and Description |
|---|---|
private java.util.concurrent.Executor |
executor |
private java.util.List<AsyncFutureTask.Reg<?>> |
listeners |
private java.lang.Object |
result |
private AsyncFuture.Status |
status |
| Modifier | Constructor and Description |
|---|---|
protected |
AsyncFutureTask(java.util.concurrent.Executor executor)
Construct a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
<A> void |
addListener(AsyncFuture.Listener<? super T,A> listener,
A attachment)
Add an asynchronous listener to be called when this operation completes.
|
void |
asyncCancel(boolean interruptionDesired)
Cancel this task.
|
AsyncFuture.Status |
await()
Wait if necessary for this operation to complete, returning the outcome.
|
AsyncFuture.Status |
await(long timeout,
java.util.concurrent.TimeUnit unit)
Wait if necessary for this operation to complete, returning the outcome, which may include
AsyncFuture.Status.WAITING if
the timeout expires before the operation completes. |
AsyncFuture.Status |
awaitUninterruptibly()
Wait (uninterruptibly) if necessary for this operation to complete, returning the outcome.
|
AsyncFuture.Status |
awaitUninterruptibly(long timeout,
java.util.concurrent.TimeUnit unit)
Wait if necessary for this operation to complete, returning the outcome, which may include
AsyncFuture.Status.WAITING if
the timeout expires before the operation completes. |
boolean |
cancel(boolean interruptionDesired)
Synchronously cancel a task, blocking uninterruptibly until it is known whether such cancellation was
successful.
|
T |
get() |
T |
get(long timeout,
java.util.concurrent.TimeUnit unit) |
AsyncFuture.Status |
getStatus()
Get (poll) the current status of the asynchronous operation.
|
T |
getUninterruptibly()
Waits (uninterruptibly) if necessary for the computation to complete, and then retrieves the result.
|
T |
getUninterruptibly(long timeout,
java.util.concurrent.TimeUnit unit)
Waits (uninterruptibly) if necessary for at most the given time for the computation to complete, and then
retrieves the result, if available.
|
private static java.lang.IllegalStateException |
invalidState() |
boolean |
isCancelled() |
boolean |
isDone() |
private static java.util.concurrent.CancellationException |
operationCancelled() |
private static java.util.concurrent.ExecutionException |
operationFailed(java.lang.Throwable cause) |
private static java.util.concurrent.TimeoutException |
operationTimedOut() |
private <A> void |
safeExecute(AsyncFutureTask.Reg<A> reg) |
protected boolean |
setCancelled()
Set the cancelled result of this operation.
|
protected boolean |
setFailed(java.lang.Throwable cause)
Set the failure result of this operation.
|
protected boolean |
setResult(T result)
Set the successful result of this operation.
|
private final java.util.concurrent.Executor executor
private AsyncFuture.Status status
private java.lang.Object result
private java.util.List<AsyncFutureTask.Reg<?>> listeners
protected AsyncFutureTask(java.util.concurrent.Executor executor)
executor - the executor to use for asynchronous notificationsprivate static java.util.concurrent.TimeoutException operationTimedOut()
private static java.util.concurrent.CancellationException operationCancelled()
private static java.util.concurrent.ExecutionException operationFailed(java.lang.Throwable cause)
private static java.lang.IllegalStateException invalidState()
protected final boolean setResult(T result)
set*() methods are ignored.result - the resulttrue if the result was successfully set, or false if a result was already setprotected final boolean setCancelled()
set*() methods are ignored.true if the result was successfully set, or false if a result was already setprotected final boolean setFailed(java.lang.Throwable cause)
set*() methods are ignored.cause - the cause of failuretrue if the result was successfully set, or false if a result was already setprivate <A> void safeExecute(AsyncFutureTask.Reg<A> reg)
public void asyncCancel(boolean interruptionDesired)
interruptionDesired flag.
Implementations are allowed to interrupt threads associated with tasks even if the flag is
false; likewise, implementations may choose not to interrupt threads even if the
flag is true.asyncCancel in interface AsyncCancellableasyncCancel in interface AsyncFuture<T>interruptionDesired - true if interruption of threads is desiredpublic final AsyncFuture.Status await() throws java.lang.InterruptedException
AsyncFuture.Status.COMPLETE, AsyncFuture.Status.CANCELLED, or AsyncFuture.Status.FAILED.await in interface AsyncFuture<T>java.lang.InterruptedException - if execution was interrupted while waitingpublic final AsyncFuture.Status await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
AsyncFuture.Status.WAITING if
the timeout expires before the operation completes.await in interface AsyncFuture<T>timeout - the maximum time to waitunit - the time unit of the timeout argumentjava.lang.InterruptedException - if execution was interrupted while waitingpublic final AsyncFuture.Status awaitUninterruptibly()
AsyncFuture.Status.COMPLETE, AsyncFuture.Status.CANCELLED, or AsyncFuture.Status.FAILED.awaitUninterruptibly in interface AsyncFuture<T>public final AsyncFuture.Status awaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)
AsyncFuture.Status.WAITING if
the timeout expires before the operation completes.awaitUninterruptibly in interface AsyncFuture<T>timeout - the maximum time to waitunit - the time unit of the timeout argumentpublic final T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
get in interface java.util.concurrent.Future<T>java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionpublic final T get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
get in interface java.util.concurrent.Future<T>java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutExceptionpublic final T getUninterruptibly() throws java.util.concurrent.CancellationException, java.util.concurrent.ExecutionException
getUninterruptibly in interface AsyncFuture<T>java.util.concurrent.CancellationException - if the computation was cancelledjava.util.concurrent.ExecutionException - if the computation threw an exceptionpublic final T getUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.CancellationException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
getUninterruptibly in interface AsyncFuture<T>timeout - the maximum time to waitunit - the time unit of the timeout argumentjava.util.concurrent.CancellationException - if the computation was cancelledjava.util.concurrent.ExecutionException - if the computation threw an exceptionjava.util.concurrent.TimeoutException - if the wait timed outpublic final AsyncFuture.Status getStatus()
getStatus in interface AsyncFuture<T>public final <A> void addListener(AsyncFuture.Listener<? super T,A> listener, A attachment)
addListener in interface AsyncFuture<T>A - the attachment typelistener - the listener to addattachment - the attachment to pass inpublic final boolean cancel(boolean interruptionDesired)
Future.cancel(boolean) is somewhat unclear about blocking semantics.
It is recommended to use AsyncFuture.asyncCancel(boolean) instead.cancel in interface java.util.concurrent.Future<T>cancel in interface AsyncFuture<T>interruptionDesired - if interruption is desired (if available)true if cancel succeeded, false otherwisepublic final boolean isCancelled()
isCancelled in interface java.util.concurrent.Future<T>public final boolean isDone()
isDone in interface java.util.concurrent.Future<T>