Android SDK Version


public class Task.Companion
Field Summary
Modifier and TypeFieldDescription
public final ExecutorServiceBACKGROUND_EXECUTOR
public final ExecutorUI_THREAD_EXECUTOR
public final static Task.CompanionINSTANCE
Method Summary
Modifier and TypeMethodDescription
final Task.UnobservedExceptionHandlergetUnobservedExceptionHandler() Returns the handler invoked when a task has an unobserved exception or null.
final UnitsetUnobservedExceptionHandler(Task.UnobservedExceptionHandler eh) Set the handler invoked when a task has an unobserved exception.
final <TResult extends Any> Task<TResult>forResult(TResult value) Creates a completed task with the given value.
final <TResult extends Any> Task<TResult>forError(Exception error) Creates a faulted task with the given error.
final <TResult extends Any> Task<TResult>cancelled() Creates a cancelled task.
final Task<Void>delay(Long delay) Creates a task that completes after a time delay.
final Task<Void>delay(Long delay, CancellationToken cancellationToken) Creates a task that completes after a time delay.
final <TResult extends Any> Task<TResult>callInBackground(Callable<TResult> callable) Invokes the callable on a background thread, returning a Task to represent the operation.
final <TResult extends Any> Task<TResult>callInBackground(Callable<TResult> callable, CancellationToken ct) Invokes the callable on a background thread, returning a Task to represent the operation.
final <TResult extends Any> Task<TResult>call(Callable<TResult> callable, Executor executor) Invokes the callable using the given executor, returning a Task to represent the operation.
final <TResult extends Any> Task<TResult>call(Callable<TResult> callable, Executor executor, CancellationToken ct) Invokes the callable using the given executor, returning a Task to represent the operation.
final <TResult extends Any> Task<TResult>call(Callable<TResult> callable) Invokes the callable on the current thread, producing a Task.
final <TResult extends Any> Task<TResult>call(Callable<TResult> callable, CancellationToken ct) Invokes the callable on the current thread, producing a Task.
final <TResult extends Any> Task<Task<TResult>>whenAnyResult(Collection<Task<TResult>> tasks) Creates a task that will complete when any of the supplied tasks have completed.
final Task<Task<?>>whenAny(Collection<Task<?>> tasks) Creates a task that will complete when any of the supplied tasks have completed.
final <TResult extends Any> Task<List<TResult>>whenAllResult(Collection<Task<TResult>> tasks) Creates a task that completes when all of the provided tasks are complete.
final Task<Void>whenAll(Collection<Task<?>> tasks) Creates a task that completes when all of the provided tasks are complete.
final ExecutorServicegetBACKGROUND_EXECUTOR() An java.util.concurrent.Executor that executes tasks in parallel.
final ExecutorgetUI_THREAD_EXECUTOR() An java.util.concurrent.Executor that executes tasks on the UI thread.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Method Detail

getUnobservedExceptionHandler

 final Task.UnobservedExceptionHandlergetUnobservedExceptionHandler()
Returns the handler invoked when a task has an unobserved exception or null.

setUnobservedExceptionHandler

 final UnitsetUnobservedExceptionHandler(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.

forResult

 final <TResult extends Any> Task<TResult> forResult(TResult value)
Creates a completed task with the given value.

forError

 final <TResult extends Any> Task<TResult> forError(Exception error)
Creates a faulted task with the given error.

cancelled

 final <TResult extends Any> Task<TResult> cancelled()
Creates a cancelled task.

delay

 final 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

 final 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 to completing the returned task.

callInBackground

 final <TResult extends Any> 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 Exception from the callable.

callInBackground

 final <TResult extends Any> Task<TResult> callInBackground(Callable<TResult> callable, CancellationToken ct)
Invokes the callable on a background thread, returning a Task to represent the operation.

call

 final <TResult extends Any> 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 Exception from the callable.

call

 final <TResult extends Any> 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

 final <TResult extends Any> 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 Exception from the callable.

call

 final <TResult extends Any> Task<TResult> call(Callable<TResult> callable, CancellationToken ct)
Invokes the callable on the current thread, producing a Task.

whenAnyResult

 final <TResult extends Any> Task<Task<TResult>> whenAnyResult(Collection<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 returned task 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

 final Task<Task<?>> whenAny(Collection<Task<?>> 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 returned task 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

 final <TResult extends Any> Task<List<TResult>> whenAllResult(Collection<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 also complete in a faulted state, where its exception will resolve to that Exception if a single task fails or an AggregateException of all the Exceptions if multiple tasks fail.
If none of the supplied tasks faulted but at least one of them was cancelled, the returned task will end as cancelled.
If none of the tasks faulted and none of the tasks were cancelled, the resulting task will end completed. The result of the returned task will be set to a list containing all of the results of the supplied tasks in the same order as they were provided (e.g. if the input tasks collection contained t1, t2, t3, the output task's result will return an List&lt;TResult&gt; where 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 transition to a completed state before it's returned to the caller. The returned List&lt;TResult&gt; will contain 0 elements.
Parameters:
tasks - The tasks that the return value will wait for before completing.

whenAll

 final Task<Void> whenAll(Collection<Task<?>> 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 also complete 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 returned task will end as cancelled.
If none of the tasks faulted and none of the tasks were canceled, the resulting task will end in the completed state.
If the supplied collection contains no tasks, the returned task will immediately transition to a completed state before it's returned to the caller.
Parameters:
tasks - The tasks that the return value will wait for before completing.

getBACKGROUND_EXECUTOR

 final ExecutorServicegetBACKGROUND_EXECUTOR()
An java.util.concurrent.Executor that executes tasks in parallel.

getUI_THREAD_EXECUTOR

 final ExecutorgetUI_THREAD_EXECUTOR()
An java.util.concurrent.Executor that executes tasks on the UI thread.