Tasks

public final class Tasks extends Object

Task utility methods.

Public Method Summary

static <T> T
await(Task<T> task)
Blocks until the specified Task is complete.
static <T> T
await(Task<T> task, long timeout, TimeUnit unit)
Blocks until the specified Task is complete.
static <T> Task<T>
call(Callable<T> callable)
Returns a Task that will be completed with the result of the specified Callable.
static <T> Task<T>
call(Executor executor, Callable<T> callable)
Returns a Task that will be completed with the result of the specified Callable.
static <T> Task<T>
forException(Exception exception)
Returns a completed Task with the specified exception.
static <T> Task<T>
forResult(T 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.

Inherited Method Summary

Public Methods

public static T await (Task<T> task)

Blocks until the specified Task is complete.

Returns
  • the Task's result
Throws
ExecutionException if the Task fails
InterruptedException if an interrupt occurs while waiting for the Task to complete

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

Blocks until the specified Task is complete.

Returns
  • the Task's result
Throws
ExecutionException if the Task fails
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 Task<T> call (Callable<T> callable)

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

The Callable will be called on a shared thread pool.

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

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

Parameters
executor the Executor to use to call the Callable

public static Task<T> forException (Exception exception)

Returns a completed Task with the specified exception.

public static Task<T> forResult (T 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.

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.

Throws
NullPointerException if any of the provided Tasks are null

Send feedback about...

Need help? Visit our support page.