Tasks

Kotlin|Java

public final classTasks


Task utility methods.

Summary

Public methods

static TResult
<TResult>await(@NonNullTask<TResult> task)

Blocks until the specified Task is complete.

static TResult
<TResult>await(
    @NonNullTask<TResult> task,
    long timeout,
    @NonNullTimeUnit unit
)

Blocks until the specified Task is complete.

static @NonNullTask<TResult>
<TResult>call(@NonNullCallable<TResult> callable)

This method is deprecated.

UseTaskCompletionSource instead, which allows the caller to manage their ownExecutor.

static @NonNullTask<TResult>
<TResult>call(
    @NonNullExecutor executor,
    @NonNullCallable<TResult> callable
)

This method is deprecated.

UseTaskCompletionSource instead, which allows the caller to manage their ownExecutor.

static @NonNullTask<TResult>
<TResult>forCanceled()

Returns a canceled Task.

static @NonNullTask<TResult>

Returns a completed Task with the specified exception.

static @NonNullTask<TResult>
<TResult>forResult(TResult result)

Returns a completed Task with the specified result.

static @NonNullTask<Void>
whenAll(@Nullable Task[] tasks)

Returns a Task that completes successfully when all of the specified Tasks complete successfully.

static @NonNullTask<Void>

Returns a Task that completes successfully when all of the specified Tasks complete successfully.

static @NonNullTask<List<Task<Object>>>
whenAllComplete(@Nullable Task[] tasks)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete.

static @NonNullTask<List<Task<Object>>>

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete.

static @NonNullTask<List<Task<Object>>>
whenAllComplete(Executor executor, @Nullable Task[] tasks)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete.

static @NonNullTask<List<Task<Object>>>
whenAllComplete(
    Executor executor,
    @NullableCollection<Task<Object>> tasks
)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete.

static @NonNullTask<List<TResult>>
<TResult>whenAllSuccess(@Nullable Task[] tasks)

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.

static @NonNullTask<List<TResult>>

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.

static @NonNullTask<List<TResult>>
<TResult>whenAllSuccess(Executor executor, @Nullable Task[] tasks)

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.

static @NonNullTask<List<TResult>>
<TResult>whenAllSuccess(
    Executor executor,
    @NullableCollection<Task<Object>> tasks
)

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.

staticTask<T>
<T>withTimeout(@NonNullTask<T> task, long timeout, @NonNullTimeUnit unit)

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

Public methods

await

public static TResult <TResult>await(@NonNullTask<TResult> task)

Blocks until the specified Task is complete.

Returns
TResult

the Task's result

Throws
java.util.concurrent.ExecutionException

if the Task fails.getCause will return the original exception.

java.lang.InterruptedException

if an interrupt occurs while waiting for the Task to complete

await

public static TResult <TResult>await(
    @NonNullTask<TResult> task,
    long timeout,
    @NonNullTimeUnit unit
)

Blocks until the specified Task is complete.

Returns
TResult

the Task's result

Throws
java.util.concurrent.ExecutionException

if the Task fails.getCause will return the original exception.

java.lang.InterruptedException

if an interrupt occurs while waiting for the Task to complete

java.util.concurrent.TimeoutException

if the specified timeout is reached before the Task completes

call

public static @NonNullTask<TResult> <TResult>call(@NonNullCallable<TResult> callable)
This method is deprecated.

UseTaskCompletionSource instead, which allows the caller to manage their ownExecutor.

Returns aTask that will be completed with the result of the specifiedCallable.

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

TheCallable will be called on the main application thread.

call

public static @NonNullTask<TResult> <TResult>call(
    @NonNullExecutor executor,
    @NonNullCallable<TResult> callable
)
This method is deprecated.

UseTaskCompletionSource instead, which allows the caller to manage their ownExecutor.

Returns aTask that will be completed with the result of the specifiedCallable.

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

Parameters
@NonNullExecutor executor

the Executor to use to call theCallable

forCanceled

public static @NonNullTask<TResult> <TResult>forCanceled()

Returns a canceled Task.

forException

public static @NonNullTask<TResult> <TResult>forException(@NonNullException e)

Returns a completed Task with the specified exception.

forResult

public static @NonNullTask<TResult> <TResult>forResult(TResult result)

Returns a completed Task with the specified result.

whenAll

public static @NonNullTask<VoidwhenAll(@Nullable 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
java.lang.NullPointerException

if any of the provided Tasks are null

whenAll

public static @NonNullTask<VoidwhenAll(@NullableCollection<Task<Object>> 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
java.lang.NullPointerException

if any of the provided Tasks are null

whenAllComplete

public static @NonNullTask<List<Task<Object>>> whenAllComplete(@Nullable 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 towhenAllComplete where an executor can be specified to use a background thread.

Throws
java.lang.NullPointerException

if any of the provided Tasks are null

whenAllComplete

public static @NonNullTask<List<Task<Object>>> whenAllComplete(@NullableCollection<Task<Object>> 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 towhenAllComplete where an executor can be specified to use a background thread.

Throws
java.lang.NullPointerException

if any of the provided

Tasks are null

whenAllComplete

public static @NonNullTask<List<Task<Object>>> whenAllComplete(Executor executor, @Nullable 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
java.lang.NullPointerException

if any of the provided Tasks are null

whenAllComplete

public static @NonNullTask<List<Task<Object>>> whenAllComplete(
    Executor executor,
    @NullableCollection<Task<Object>> 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
java.lang.NullPointerException

if any of the provided Tasks are null

whenAllSuccess

public static @NonNullTask<List<TResult>> <TResult>whenAllSuccess(@Nullable 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 towhenAllSuccess where an executor can be specified to use a background thread.

Throws
java.lang.NullPointerException

if any of the provided Tasks are null

whenAllSuccess

public static @NonNullTask<List<TResult>> <TResult>whenAllSuccess(@NullableCollection<Task<Object>> 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 towhenAllSuccess where an executor can be specified to use a background thread.

Throws
java.lang.NullPointerException

if any of the provided Tasks are null

whenAllSuccess

public static @NonNullTask<List<TResult>> <TResult>whenAllSuccess(Executor executor, @Nullable 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
java.lang.NullPointerException

if any of the provided Tasks are null

whenAllSuccess

public static @NonNullTask<List<TResult>> <TResult>whenAllSuccess(
    Executor executor,
    @NullableCollection<Task<Object>> 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 executor

the Executor to use to run theContinuation

Throws
java.lang.NullPointerException

if any of the provided Tasks are null

withTimeout

public static Task<T> <T>withTimeout(@NonNullTask<T> task, long timeout, @NonNullTimeUnit unit)

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

Returns
Task<T>

A new Task.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-10-31 UTC.