Tasks

public final class Tasks extends Object

Task utility methods.

Public Method Summary

static <TResult> TResult
await(Task<TResult> task, long timeout, TimeUnit unit)
Blocks until the specified Task is complete.
static <TResult> TResult
await(Task<TResult> task)
Blocks until the specified Task is complete.
static <TResult> Task<TResult>
call(Callable<TResult> callable)
This method is deprecated. Use TaskCompletionSource instead, which allows the caller to manage their own Executor.
static <TResult> Task<TResult>
call(Executor executor, Callable<TResult> callable)
This method is deprecated. Use TaskCompletionSource instead, which allows the caller to manage their own Executor.
static <TResult> Task<TResult>
forCanceled()
Returns a canceled Task.
static <TResult> Task<TResult>
forException(Exception e)
Returns a completed Task with the specified exception.
static <TResult> Task<TResult>
forResult(TResult result)
Returns a completed Task with the specified result.
static Task<Void>
whenAll(Collection<? extends Task<?>> tasks)
Returns a Task that completes successfully when all of the specified Tasks complete successfully.
static Task<Void>
whenAll(Task...<?> tasks)
Returns a Task that completes successfully when all of the specified Tasks complete successfully.
static Task<List<Task<?>>>
whenAllComplete(Task...<?> tasks)
Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete.
static Task<List<Task<?>>>
whenAllComplete(Executor executor, Task...<?> tasks)
Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete.
static Task<List<Task<?>>>
whenAllComplete(Executor executor, Collection<? extends Task<?>> tasks)
Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete.
static Task<List<Task<?>>>
whenAllComplete(Collection<? extends Task<?>> tasks)
Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete.
static <TResult> Task<List<TResult>>
whenAllSuccess(Executor executor, Task...<?> tasks)
Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.
static <TResult> Task<List<TResult>>
whenAllSuccess(Task...<?> tasks)
Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.
static <TResult> Task<List<TResult>>
whenAllSuccess(Executor executor, Collection<? extends Task<?>> tasks)
Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.
static <TResult> Task<List<TResult>>
whenAllSuccess(Collection<? extends Task<?>> tasks)
Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.
static <T> Task<T>
withTimeout(Task<T> task, long timeout, TimeUnit unit)
Returns a new Task which will return a TimeoutException if a result is not returned within the specified time period.

Inherited Method Summary

Public Methods

public static TResult await (Task<TResult> task, long timeout, TimeUnit unit)

Blocks until the specified Task is complete.

Returns
  • the Task's result
Throws
ExecutionException if the Task fails. getCause will return the original exception.
InterruptedException if an interrupt occurs while waiting for the Task to complete
TimeoutException if the specified timeout is reached before the Task completes

public static TResult await (Task<TResult> task)

Blocks until the specified Task is complete.

Returns
  • the Task's result
Throws
ExecutionException if the Task fails. getCause will return the original exception.
InterruptedException if an interrupt occurs while waiting for the Task to complete

public static Task<TResult> call (Callable<TResult> callable)

This method is deprecated.
Use TaskCompletionSource instead, which allows the caller to manage their own Executor.

Returns a Task that will be completed with the result of the specified Callable.

If a non-Exception throwable is thrown in the callable, the Task will be failed with a RuntimeException whose cause is the original throwable.

The Callable will be called on the main application thread.

public static Task<TResult> call (Executor executor, Callable<TResult> callable)

This method is deprecated.
Use TaskCompletionSource instead, which allows the caller to manage their own Executor.

Returns a Task that will be completed with the result of the specified Callable.

If a non-Exception throwable is thrown in the callable, the Task will be failed with a RuntimeException whose cause is the original throwable.

Parameters
executor the Executor to use to call the Callable
callable

public static Task<TResult> forCanceled ()

Returns a canceled Task.

public static Task<TResult> forException (Exception e)

Returns a completed Task with the specified exception.

public static Task<TResult> forResult (TResult result)

Returns a completed Task with the specified result.

public static Task<Void> whenAll (Collection<? extends Task<?>> tasks)

Returns a Task that completes successfully when all of the specified Tasks complete successfully. Does not accept nulls.

The returned Task would fail if any of the provided Tasks fail. The returned Task would be set to canceled if any of the provided Tasks is canceled and no failure is detected.

Throws
NullPointerException if any of the provided Tasks are null

public static Task<Void> whenAll (Task...<?> tasks)

Returns a Task that completes successfully when all of the specified Tasks complete successfully. Does not accept nulls.

This Task would fail if any of the provided Tasks fail. This Task would be set to canceled if any of the provided Tasks is canceled and no failure is detected.

Throws
NullPointerException if any of the provided Tasks are null

public static Task<List<Task<?>>> whenAllComplete (Task...<?> tasks)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete. This Task would always succeed even if any of the provided Tasks fail or canceled. Does not accept nulls.

This method execute tasks on the main thread, and can cause stuttering or delay when the main thread is busy. Consider migrating to whenAllComplete(Executor, Collection) where an executor can be specified to use a background thread.

Throws
NullPointerException if any of the provided Tasks are null

public static Task<List<Task<?>>> whenAllComplete (Executor executor, Task...<?> tasks)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete. This Task would always succeed even if any of the provided Tasks fail or canceled. Does not accept nulls.

Throws
NullPointerException if any of the provided Tasks are null

public static Task<List<Task<?>>> whenAllComplete (Executor executor, Collection<? extends Task<?>> tasks)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete. This Task would always succeed even if any of the provided Tasks fail or canceled. Does not accept nulls.

Throws
NullPointerException if any of the provided Tasks are null

public static Task<List<Task<?>>> whenAllComplete (Collection<? extends Task<?>> tasks)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete. This Task would always succeed even if any of the provided Tasks fail or canceled. Does not accept nulls.

This method execute tasks on the main thread, and can cause stuttering or delay when the main thread is busy. Consider migrating to whenAllComplete(Executor, Collection) where an executor can be specified to use a background thread.

Throws
NullPointerException if any of the provided

Tasks are null

public static Task<List<TResult>> whenAllSuccess (Executor executor, Task...<?> tasks)

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully. This Task would fail if any of the provided Tasks fail. Does not accept nulls.

This Task would be set to canceled if any of the provided Tasks is canceled and no failure is detected.

Throws
NullPointerException if any of the provided Tasks are null

public static Task<List<TResult>> whenAllSuccess (Task...<?> tasks)

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully. This Task would fail if any of the provided Tasks fail. Does not accept nulls.

This Task would be set to canceled if any of the provided Tasks is canceled and no failure is detected.

This method execute tasks on the main thread, and can cause stuttering or delay when the main thread is busy. Consider migrating to whenAllSuccess(Executor, Collection) where an executor can be specified to use a background thread.

Throws
NullPointerException if any of the provided Tasks are null

public static Task<List<TResult>> whenAllSuccess (Executor executor, Collection<? extends Task<?>> tasks)

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully. This Task would fail if any of the provided Tasks fail. Does not accept nulls.

This Task would be set to canceled if any of the provided Tasks is canceled and no failure is detected.

Parameters
executor the Executor to use to run the Continuation
tasks
Throws
NullPointerException if any of the provided Tasks are null

public static Task<List<TResult>> whenAllSuccess (Collection<? extends Task<?>> tasks)

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully. This Task would fail if any of the provided Tasks fail. Does not accept nulls.

This Task would be set to canceled if any of the provided Tasks is canceled and no failure is detected.

This method execute tasks on the main thread, and can cause stuttering or delay when the main thread is busy. Consider migrating to whenAllSuccess(Executor, Collection) where an executor can be specified to use a background thread.

Throws
NullPointerException if any of the provided Tasks are null

public static Task<T> withTimeout (Task<T> task, long timeout, TimeUnit unit)

Returns a new Task which will return a TimeoutException if a result is not returned within the specified time period.

Returns
  • A new Task.