-
public class Task<TResult>Represents the result of an asynchronous operation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfaceTask.UnobservedExceptionHandlerInterface for handlers invoked when a failed
{@code Task}is about to be finalized, but theexception has not been consumed.The handler will execute in the GC thread, so if the handler needs to do anything timeconsuming or complex it is a good idea to fire off a
{@code Task}to handle the exception.public classTask.TaskCompletionSource
-
Field Summary
Fields Modifier and Type Field Description public final static ExecutorServiceBACKGROUND_EXECUTORpublic final static ExecutorUI_THREAD_EXECUTORprivate static volatile Task.UnobservedExceptionHandlerunobservedExceptionHandlerprivate TResultresultprivate Exceptionerror
-
Method Summary
Modifier and Type Method Description static Task.UnobservedExceptionHandlergetUnobservedExceptionHandler()Returns the handler invoked when a task has an unobserved exception or {@code null}.static voidsetUnobservedExceptionHandler(Task.UnobservedExceptionHandler eh)Set the handler invoked when a task has an unobserved exception. TResultgetResult()ExceptiongetError()booleanisCompleted()booleanisCancelled()booleanisFaulted()voidwaitForCompletion()Blocks until the task is complete. booleanwaitForCompletion(long duration, TimeUnit timeUnit)Blocks until the task is complete or times out. static <TResult> Task<TResult>forResult(TResult value)Creates a completed task with the given value. static <TResult> Task<TResult>forError(Exception error)Creates a faulted task with the given error. static <TResult> Task<TResult>cancelled()Creates a cancelled task. static Task<Void>delay(long delay)Creates a task that completes after a time delay. static Task<Void>delay(long delay, CancellationToken cancellationToken)Creates a task that completes after a time delay. <TOut> Task<TOut>cast()Makes a fluent cast of a Task's result possible, avoiding an extra continuation just to castthe type of the result. Task<Void>makeVoid()Turns a Taskinto a Task static <TResult> Task<TResult>callInBackground(Callable<TResult> callable)Invokes the callable on a background thread, returning a Task to represent the operation. static <TResult> Task<TResult>callInBackground(Callable<TResult> callable, CancellationToken ct)Invokes the callable on a background thread, returning a Task to represent the operation. static <TResult> Task<TResult>call(Callable<TResult> callable, Executor executor)Invokes the callable using the given executor, returning a Task to represent the operation. static <TResult> Task<TResult>call(Callable<TResult> callable, Executor executor, CancellationToken ct)Invokes the callable using the given executor, returning a Task to represent the operation. static <TResult> Task<TResult>call(Callable<TResult> callable)Invokes the callable on the current thread, producing a Task. static <TResult> Task<TResult>call(Callable<TResult> callable, CancellationToken ct)Invokes the callable on the current thread, producing a Task. static <TResult> Task<Task<TResult>>whenAnyResult(Collection<out Task<TResult>> tasks)Creates a task that will complete when any of the supplied tasks have completed. static Task<Task<out Object>>whenAny(Collection<out Task<out Object>> tasks)Creates a task that will complete when any of the supplied tasks have completed. static <TResult> Task<List<TResult>>whenAllResult(Collection<out Task<TResult>> tasks)Creates a task that completes when all of the provided tasks are complete. static Task<Void>whenAll(Collection<out Task<out Object>> tasks)Creates a task that completes when all of the provided tasks are complete. Task<Void>continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation)Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation. Task<Void>continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, CancellationToken ct)Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation. Task<Void>continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, Executor executor)Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation. Task<Void>continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, Executor executor, CancellationToken ct)Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation. <TContinuationResult> Task<TContinuationResult>continueWith(Continuation<TResult, TContinuationResult> continuation, Executor executor)Adds a continuation that will be scheduled using the executor, returning a new task thatcompletes after the continuation has finished running. <TContinuationResult> Task<TContinuationResult>continueWith(Continuation<TResult, TContinuationResult> continuation, Executor executor, CancellationToken ct)Adds a continuation that will be scheduled using the executor, returning a new task thatcompletes after the continuation has finished running. <TContinuationResult> Task<TContinuationResult>continueWith(Continuation<TResult, TContinuationResult> continuation)Adds a synchronous continuation to this task, returning a new task that completes after thecontinuation has finished running. <TContinuationResult> Task<TContinuationResult>continueWith(Continuation<TResult, TContinuationResult> continuation, CancellationToken ct)Adds a synchronous continuation to this task, returning a new task that completes after thecontinuation has finished running. <TContinuationResult> Task<TContinuationResult>continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor)Adds an Task-based continuation to this task that will be scheduled using the executor,returning a new task that completes after the task returned by the continuation has completed. <TContinuationResult> Task<TContinuationResult>continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor, CancellationToken ct)Adds an Task-based continuation to this task that will be scheduled using the executor,returning a new task that completes after the task returned by the continuation has completed. <TContinuationResult> Task<TContinuationResult>continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation)Adds an asynchronous continuation to this task, returning a new task that completes after thetask returned by the continuation has completed. <TContinuationResult> Task<TContinuationResult>continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct)Adds an asynchronous continuation to this task, returning a new task that completes after thetask returned by the continuation has completed. <TContinuationResult> Task<TContinuationResult>onSuccess(Continuation<TResult, TContinuationResult> continuation, Executor executor)Runs a continuation when a task completes successfully, forwarding along or cancellation. <TContinuationResult> Task<TContinuationResult>onSuccess(Continuation<TResult, TContinuationResult> continuation, Executor executor, CancellationToken ct)Runs a continuation when a task completes successfully, forwarding along or cancellation. <TContinuationResult> Task<TContinuationResult>onSuccess(Continuation<TResult, TContinuationResult> continuation)Runs a continuation when a task completes successfully, forwarding along s or cancellation. <TContinuationResult> Task<TContinuationResult>onSuccess(Continuation<TResult, TContinuationResult> continuation, CancellationToken ct)Runs a continuation when a task completes successfully, forwarding along s or cancellation. <TContinuationResult> Task<TContinuationResult>onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor)Runs a continuation when a task completes successfully, forwarding along s or cancellation. <TContinuationResult> Task<TContinuationResult>onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor, CancellationToken ct)Runs a continuation when a task completes successfully, forwarding along s or cancellation. <TContinuationResult> Task<TContinuationResult>onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation)Runs a continuation when a task completes successfully, forwarding along s or cancellation. <TContinuationResult> Task<TContinuationResult>onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct)Runs a continuation when a task completes successfully, forwarding along s or cancellation. -
-
Method Detail
-
getUnobservedExceptionHandler
static Task.UnobservedExceptionHandler getUnobservedExceptionHandler()
Returns the handler invoked when a task has an unobserved exception or
{@code null}.
-
setUnobservedExceptionHandler
static void setUnobservedExceptionHandler(Task.UnobservedExceptionHandler eh)
Set the handler invoked when a task has an unobserved exception.
- Parameters:
eh- the object to use as an unobserved exception handler.
-
isCompleted
boolean isCompleted()
-
isCancelled
boolean isCancelled()
-
isFaulted
boolean isFaulted()
-
waitForCompletion
void waitForCompletion()
Blocks until the task is complete.
-
waitForCompletion
boolean waitForCompletion(long duration, TimeUnit timeUnit)
Blocks until the task is complete or times out.
-
forResult
static <TResult> Task<TResult> forResult(TResult value)
Creates a completed task with the given value.
-
forError
static <TResult> Task<TResult> forError(Exception error)
Creates a faulted task with the given error.
-
delay
static Task<Void> delay(long delay)
Creates a task that completes after a time delay.
- Parameters:
delay- The number of milliseconds to wait before completing the returned task.
-
delay
static Task<Void> delay(long delay, CancellationToken cancellationToken)
Creates a task that completes after a time delay.
- Parameters:
delay- The number of milliseconds to wait before completing the returned task.cancellationToken- The optional cancellation token that will be checked prior tocompleting the returned task.
-
cast
<TOut> Task<TOut> cast()
Makes a fluent cast of a Task's result possible, avoiding an extra continuation just to castthe type of the result.
-
callInBackground
static <TResult> Task<TResult> callInBackground(Callable<TResult> callable)
Invokes the callable on a background thread, returning a Task to represent the operation.
If you want to cancel the resulting Task throw a from the callable.
-
callInBackground
static <TResult> Task<TResult> callInBackground(Callable<TResult> callable, CancellationToken ct)
Invokes the callable on a background thread, returning a Task to represent the operation.
-
call
static <TResult> Task<TResult> call(Callable<TResult> callable, Executor executor)
Invokes the callable using the given executor, returning a Task to represent the operation.
If you want to cancel the resulting Task throw a from the callable.
-
call
static <TResult> Task<TResult> call(Callable<TResult> callable, Executor executor, CancellationToken ct)
Invokes the callable using the given executor, returning a Task to represent the operation.
-
call
static <TResult> Task<TResult> call(Callable<TResult> callable)
Invokes the callable on the current thread, producing a Task.
If you want to cancel the resulting Task throw a from the callable.
-
call
static <TResult> Task<TResult> call(Callable<TResult> callable, CancellationToken ct)
Invokes the callable on the current thread, producing a Task.
-
whenAnyResult
static <TResult> Task<Task<TResult>> whenAnyResult(Collection<out Task<TResult>> tasks)
Creates a task that will complete when any of the supplied tasks have completed.
The returned task will complete when any of the supplied tasks has completed. The returnedtask will always end in the completed state with its result set to the first task to complete.This is true even if the first task to complete ended in the canceled or faulted state.
- Parameters:
tasks- The tasks to wait on for completion.
-
whenAny
static Task<Task<out Object>> whenAny(Collection<out Task<out Object>> tasks)
Creates a task that will complete when any of the supplied tasks have completed.
The returned task will complete when any of the supplied tasks has completed. The returnedtask will always end in the completed state with its result set to the first task to complete.This is true even if the first task to complete ended in the canceled or faulted state.
- Parameters:
tasks- The tasks to wait on for completion.
-
whenAllResult
static <TResult> Task<List<TResult>> whenAllResult(Collection<out Task<TResult>> tasks)
Creates a task that completes when all of the provided tasks are complete.
If any of the supplied tasks completes in a faulted state, the returned task will alsocomplete in a faulted state, where its exception will resolve to that if a single task fails or an AggregateException of all the s if multiple tasks fail.
If none of the supplied tasks faulted but at least one of them was cancelled, the returnedtask will end as cancelled.
If none of the tasks faulted and none of the tasks were cancelled, the resulting task willend completed. The result of the returned task will be set to a list containing all of theresults of the supplied tasks in the same order as they were provided (e.g. if the input taskscollection contained t1, t2, t3, the output task's result will return an
{@code * List<TResult>}where{@code list.get(0) == t1.getResult(), list.get(1) == t2.getResult(), * and list.get(2) == t3.getResult()}).If the supplied collection contains no tasks, the returned task will immediately transitionto a completed state before it's returned to the caller. The returned
{@code * List<TResult>}will contain 0 elements.- Parameters:
tasks- The tasks that the return value will wait for before completing.
-
whenAll
static Task<Void> whenAll(Collection<out Task<out Object>> tasks)
Creates a task that completes when all of the provided tasks are complete.
If any of the supplied tasks completes in a faulted state, the returned task will alsocomplete in a faulted state, where its exception will resolve to that if a single task fails or an AggregateException of all the s if multiple tasks fail.
If none of the supplied tasks faulted but at least one of them was cancelled, the returnedtask will end as cancelled.
If none of the tasks faulted and none of the tasks were canceled, the resulting task willend in the completed state.
If the supplied collection contains no tasks, the returned task will immediately transitionto a completed state before it's returned to the caller.
- Parameters:
tasks- The tasks that the return value will wait for before completing.
-
continueWhile
Task<Void> continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation.
-
continueWhile
Task<Void> continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, CancellationToken ct)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation.
-
continueWhile
Task<Void> continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, Executor executor)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation.
-
continueWhile
Task<Void> continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, Executor executor, CancellationToken ct)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation.
-
continueWith
<TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult, TContinuationResult> continuation, Executor executor)
Adds a continuation that will be scheduled using the executor, returning a new task thatcompletes after the continuation has finished running. This allows the continuation to bescheduled on different thread.
-
continueWith
<TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult, TContinuationResult> continuation, Executor executor, CancellationToken ct)
Adds a continuation that will be scheduled using the executor, returning a new task thatcompletes after the continuation has finished running. This allows the continuation to bescheduled on different thread.
-
continueWith
<TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult, TContinuationResult> continuation)
Adds a synchronous continuation to this task, returning a new task that completes after thecontinuation has finished running.
-
continueWith
<TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult, TContinuationResult> continuation, CancellationToken ct)
Adds a synchronous continuation to this task, returning a new task that completes after thecontinuation has finished running.
-
continueWithTask
<TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor)
Adds an Task-based continuation to this task that will be scheduled using the executor,returning a new task that completes after the task returned by the continuation has completed.
-
continueWithTask
<TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor, CancellationToken ct)
Adds an Task-based continuation to this task that will be scheduled using the executor,returning a new task that completes after the task returned by the continuation has completed.
-
continueWithTask
<TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation)
Adds an asynchronous continuation to this task, returning a new task that completes after thetask returned by the continuation has completed.
-
continueWithTask
<TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct)
Adds an asynchronous continuation to this task, returning a new task that completes after thetask returned by the continuation has completed.
-
onSuccess
<TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult, TContinuationResult> continuation, Executor executor)
Runs a continuation when a task completes successfully, forwarding along or cancellation.
-
onSuccess
<TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult, TContinuationResult> continuation, Executor executor, CancellationToken ct)
Runs a continuation when a task completes successfully, forwarding along or cancellation.
-
onSuccess
<TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult, TContinuationResult> continuation)
Runs a continuation when a task completes successfully, forwarding along s or cancellation.
-
onSuccess
<TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult, TContinuationResult> continuation, CancellationToken ct)
Runs a continuation when a task completes successfully, forwarding along s or cancellation.
-
onSuccessTask
<TContinuationResult> Task<TContinuationResult> onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor)
Runs a continuation when a task completes successfully, forwarding along s or cancellation.
-
onSuccessTask
<TContinuationResult> Task<TContinuationResult> onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor, CancellationToken ct)
Runs a continuation when a task completes successfully, forwarding along s or cancellation.
-
onSuccessTask
<TContinuationResult> Task<TContinuationResult> onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation)
Runs a continuation when a task completes successfully, forwarding along s or cancellation.
-
onSuccessTask
<TContinuationResult> Task<TContinuationResult> onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct)
Runs a continuation when a task completes successfully, forwarding along s or cancellation.
-
-
-
-