Interface AsyncFuture<T>
- All Superinterfaces:
AsyncCancellable,Future<T>
- All Known Implementing Classes:
AsyncFutureTask
This interface represents the result of an asynchronous future task, which provides all the features
of
Future while also adding several additional convenience methods and the ability to add asynchronous
callbacks.- Author:
- David M. Lloyd
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classAn abstract base class for an implementation of theListenerinterface.static interfaceA listener for an asynchronous future computation result.static enumThe possible statuses of anAsyncFuture. -
Method Summary
Modifier and TypeMethodDescription<A> voidaddListener(AsyncFuture.Listener<? super T, A> listener, A attachment) Add an asynchronous listener to be called when this operation completes.voidasyncCancel(boolean interruptionDesired) Handle an asynchronous cancellation.await()Wait if necessary for this operation to complete, returning the outcome.Wait if necessary for this operation to complete, returning the outcome, which may includeAsyncFuture.Status.WAITINGif the timeout expires before the operation completes.Wait (uninterruptibly) if necessary for this operation to complete, returning the outcome.awaitUninterruptibly(long timeout, TimeUnit unit) Wait if necessary for this operation to complete, returning the outcome, which may includeAsyncFuture.Status.WAITINGif the timeout expires before the operation completes.booleancancel(boolean interruptionDesired) Synchronously cancel a task, blocking uninterruptibly until it is known whether such cancellation was successful.Get (poll) the current status of the asynchronous operation.Waits (uninterruptibly) if necessary for the computation to complete, and then retrieves the result.getUninterruptibly(long timeout, TimeUnit unit) Waits (uninterruptibly) if necessary for at most the given time for the computation to complete, and then retrieves the result, if available.Methods inherited from interface java.util.concurrent.Future
get, get, isCancelled, isDone
-
Method Details
-
await
Wait if necessary for this operation to complete, returning the outcome. The outcome will be one ofAsyncFuture.Status.COMPLETE,AsyncFuture.Status.CANCELLED, orAsyncFuture.Status.FAILED.- Returns:
- the outcome
- Throws:
InterruptedException- if execution was interrupted while waiting
-
await
Wait if necessary for this operation to complete, returning the outcome, which may includeAsyncFuture.Status.WAITINGif the timeout expires before the operation completes.- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the outcome
- Throws:
InterruptedException- if execution was interrupted while waiting
-
getUninterruptibly
Waits (uninterruptibly) if necessary for the computation to complete, and then retrieves the result.- Returns:
- the computed result
- Throws:
CancellationException- if the computation was cancelledExecutionException- if the computation threw an exception
-
getUninterruptibly
T getUninterruptibly(long timeout, TimeUnit unit) throws CancellationException, ExecutionException, TimeoutException Waits (uninterruptibly) if necessary for at most the given time for the computation to complete, and then retrieves the result, if available.- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the computed result
- Throws:
CancellationException- if the computation was cancelledExecutionException- if the computation threw an exceptionTimeoutException- if the wait timed out
-
awaitUninterruptibly
AsyncFuture.Status awaitUninterruptibly()Wait (uninterruptibly) if necessary for this operation to complete, returning the outcome. The outcome will be one ofAsyncFuture.Status.COMPLETE,AsyncFuture.Status.CANCELLED, orAsyncFuture.Status.FAILED.- Returns:
- the outcome
-
awaitUninterruptibly
Wait if necessary for this operation to complete, returning the outcome, which may includeAsyncFuture.Status.WAITINGif the timeout expires before the operation completes.- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the outcome
-
getStatus
AsyncFuture.Status getStatus()Get (poll) the current status of the asynchronous operation.- Returns:
- the current status
-
addListener
Add an asynchronous listener to be called when this operation completes.- Type Parameters:
A- the attachment type- Parameters:
listener- the listener to addattachment- the attachment to pass in
-
cancel
boolean cancel(boolean interruptionDesired) Synchronously cancel a task, blocking uninterruptibly until it is known whether such cancellation was successful. Note that theFuture.cancel(boolean)is somewhat unclear about blocking semantics. It is recommended to useasyncCancel(boolean)instead. -
asyncCancel
void asyncCancel(boolean interruptionDesired) Handle an asynchronous cancellation. Does not block, and may be called more than once; in particular, this method may be re-invoked to set theinterruptionDesiredflag even if it has already been called without that flag set before. Otherwise, this method is idempotent, and thus may be called more than once without additional effect.- Specified by:
asyncCancelin interfaceAsyncCancellable- Parameters:
interruptionDesired-trueif interruption of threads is desired
-