TResult - The type of the result of the task.public class Task<TResult>
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
class |
Task.TaskCompletionSource
Allows safe orchestration of a task's completion, preventing the consumer from prematurely
completing the task.
|
| Modifier and Type | Field and Description |
|---|---|
static java.util.concurrent.ExecutorService |
BACKGROUND_EXECUTOR
An
Executor that executes tasks in parallel. |
static java.util.concurrent.Executor |
UI_THREAD_EXECUTOR
An
Executor that executes tasks on the UI thread. |
| Modifier and Type | Method and Description |
|---|---|
static <TResult> Task<TResult> |
call(java.util.concurrent.Callable<TResult> callable)
Invokes the callable on the current thread, producing a Task.
|
static <TResult> Task<TResult> |
call(java.util.concurrent.Callable<TResult> callable,
java.util.concurrent.Executor executor)
Invokes the callable using the given executor, returning a Task to represent the operation.
|
static <TResult> Task<TResult> |
callInBackground(java.util.concurrent.Callable<TResult> callable)
Invokes the callable on a background thread, returning a Task to represent the operation.
|
static <TResult> Task<TResult> |
cancelled()
Creates a cancelled task.
|
<TOut> Task<TOut> |
cast()
Makes a fluent cast of a Task's result possible, avoiding an extra continuation just to cast
the type of the result.
|
Task<java.lang.Void> |
continueWhile(java.util.concurrent.Callable<java.lang.Boolean> predicate,
Continuation<java.lang.Void,Task<java.lang.Void>> continuation)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop is
a task continuation.
|
Task<java.lang.Void> |
continueWhile(java.util.concurrent.Callable<java.lang.Boolean> predicate,
Continuation<java.lang.Void,Task<java.lang.Void>> continuation,
java.util.concurrent.Executor executor)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop is
a task continuation.
|
<TContinuationResult> |
continueWith(Continuation<TResult,TContinuationResult> continuation)
Adds a synchronous continuation to this task, returning a new task that completes after the
continuation has finished running.
|
<TContinuationResult> |
continueWith(Continuation<TResult,TContinuationResult> continuation,
java.util.concurrent.Executor executor)
Adds a continuation that will be scheduled using the executor, returning a new task that
completes after the continuation has finished running.
|
<TContinuationResult> |
continueWithTask(Continuation<TResult,Task<TContinuationResult>> continuation)
Adds an asynchronous continuation to this task, returning a new task that completes after the
task returned by the continuation has completed.
|
<TContinuationResult> |
continueWithTask(Continuation<TResult,Task<TContinuationResult>> continuation,
java.util.concurrent.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.
|
static <TResult> Task.TaskCompletionSource |
create()
Creates a TaskCompletionSource that orchestrates a Task.
|
static <TResult> Task<TResult> |
forError(java.lang.Exception error)
Creates a faulted task with the given error.
|
static <TResult> Task<TResult> |
forResult(TResult value)
Creates a completed task with the given value.
|
java.lang.Exception |
getError() |
TResult |
getResult() |
boolean |
isCancelled() |
boolean |
isCompleted() |
boolean |
isFaulted() |
Task<java.lang.Void> |
makeVoid()
Turns a Task
|
<TContinuationResult> |
onSuccess(Continuation<TResult,TContinuationResult> continuation)
Runs a continuation when a task completes successfully, forwarding along
Exceptions or cancellation. |
<TContinuationResult> |
onSuccess(Continuation<TResult,TContinuationResult> continuation,
java.util.concurrent.Executor executor)
Runs a continuation when a task completes successfully, forwarding along
Exception or cancellation. |
<TContinuationResult> |
onSuccessTask(Continuation<TResult,Task<TContinuationResult>> continuation)
Runs a continuation when a task completes successfully, forwarding along
Exceptions or cancellation. |
<TContinuationResult> |
onSuccessTask(Continuation<TResult,Task<TContinuationResult>> continuation,
java.util.concurrent.Executor executor)
Runs a continuation when a task completes successfully, forwarding along
Exceptions or cancellation. |
void |
waitForCompletion()
Blocks until the task is complete.
|
static Task<java.lang.Void> |
whenAll(java.util.Collection<? extends Task<?>> tasks)
Creates a task that completes when all of the provided tasks are complete.
|
public static final java.util.concurrent.ExecutorService BACKGROUND_EXECUTOR
Executor that executes tasks in parallel.public static final java.util.concurrent.Executor UI_THREAD_EXECUTOR
Executor that executes tasks on the UI thread.public static <TResult> Task.TaskCompletionSource create()
public boolean isCompleted()
true if the task completed (has a result, an error, or was cancelled.
false otherwise.public boolean isCancelled()
true if the task was cancelled, false otherwise.public boolean isFaulted()
true if the task has an error, false otherwise.public TResult getResult()
null otherwise.public java.lang.Exception getError()
null otherwise.public void waitForCompletion()
throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic static <TResult> Task<TResult> forResult(TResult value)
public static <TResult> Task<TResult> forError(java.lang.Exception error)
public static <TResult> Task<TResult> cancelled()
public <TOut> Task<TOut> cast()
public Task<java.lang.Void> makeVoid()
public static <TResult> Task<TResult> callInBackground(java.util.concurrent.Callable<TResult> callable)
public static <TResult> Task<TResult> call(java.util.concurrent.Callable<TResult> callable, java.util.concurrent.Executor executor)
public static <TResult> Task<TResult> call(java.util.concurrent.Callable<TResult> callable)
public static Task<java.lang.Void> whenAll(java.util.Collection<? extends Task<?>> tasks)
tasks - The tasks that the return value will wait for before completing.Void when all the tasks are resolved. If a single
task fails, it will resolve to that Exception. If multiple tasks fail, it
will resolve to an AggregateException of all the Exceptions.public Task<java.lang.Void> continueWhile(java.util.concurrent.Callable<java.lang.Boolean> predicate, Continuation<java.lang.Void,Task<java.lang.Void>> continuation)
public Task<java.lang.Void> continueWhile(java.util.concurrent.Callable<java.lang.Boolean> predicate, Continuation<java.lang.Void,Task<java.lang.Void>> continuation, java.util.concurrent.Executor executor)
public <TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult,TContinuationResult> continuation, java.util.concurrent.Executor executor)
public <TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult,TContinuationResult> continuation)
public <TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult,Task<TContinuationResult>> continuation, java.util.concurrent.Executor executor)
public <TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult,Task<TContinuationResult>> continuation)
public <TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult,TContinuationResult> continuation, java.util.concurrent.Executor executor)
Exception or cancellation.public <TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult,TContinuationResult> continuation)
Exceptions or cancellation.public <TContinuationResult> Task<TContinuationResult> onSuccessTask(Continuation<TResult,Task<TContinuationResult>> continuation, java.util.concurrent.Executor executor)
Exceptions or cancellation.public <TContinuationResult> Task<TContinuationResult> onSuccessTask(Continuation<TResult,Task<TContinuationResult>> continuation)
Exceptions or cancellation.