-
public class Task<TResult>
Copyright 2017 Meniga Iceland Inc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description interface
Task.UnobservedExceptionHandler
Interface for handlers invoked when a failed
{@code Task}
is about to befinalized, but the exception has not been consumed.The handler will execute in the GC thread, so if the handler needs to doanything time consuming or complex it is a good idea to fire off a
{@code Task}
to handle the exception.
-
Field Summary
Fields Modifier and Type Field Description final static ExecutorService
BACKGROUND_EXECUTOR
public final static Executor
UI_THREAD_EXECUTOR
private static volatile Task.UnobservedExceptionHandler
unobservedExceptionHandler
private TResult
result
private Exception
error
-
Constructor Summary
Constructors Constructor Description Task()
-
Method Summary
Modifier and Type Method Description static <TResult> TaskCompletionSource
create(Call<TResult> call)
boolean
isCompleted()
boolean
isCancelled()
boolean
isFaulted()
void
waitForCompletion()
Blocks until the task is complete. boolean
waitForCompletion(long duration, TimeUnit timeUnit)
Blocks until the task is complete or times out. static Task<Void>
delay(long delay)
Creates a task that completes after a time delay. static Task<Void>
delay(long delay, CancellationToken cancellationToken)
Creates a task that completes after a time delay. static Task<Void>
delay(long delay, ScheduledExecutorService executor, CancellationToken cancellationToken)
<TOut> Task<TOut>
cast()
Makes a fluent cast of a Task's result possible, avoiding an extra continuation just to castthe type of the result. static <TResult> Task<TResult>
callInBackground(Callable<TResult> callable)
Invokes the callable on a background thread, returning a Task to represent the operation. static <TResult> Task<TResult>
callInBackground(Callable<TResult> callable, CancellationToken ct)
Invokes the callable on a background thread, returning a Task to represent the operation. static <TResult> Task<TResult>
call(Callable<TResult> callable, Executor executor)
Invokes the callable using the given executor, returning a Task to represent the operation. static <TResult> Task<TResult>
call(Callable<TResult> callable, Executor executor, CancellationToken ct)
Invokes the callable using the given executor, returning a Task to represent the operation. static <TResult> Task<TResult>
call(Callable<TResult> callable)
Invokes the callable on the current thread, producing a Task. static <TResult> Task<TResult>
call(Callable<TResult> callable, CancellationToken ct)
Invokes the callable on the current thread, producing a Task. static <TResult> Task<Task<TResult>>
whenAnyResult(Collection<out Task<TResult>> tasks)
Creates a task that will complete when any of the supplied tasks have completed. static Task<Task<out Object>>
whenAny(Collection<out Task<out Object>> tasks)
Creates a task that will complete when any of the supplied tasks have completed. static <TResult> Task<List<TResult>>
whenAllResult(Collection<out Task<TResult>> tasks)
Creates a task that completes when all of the provided tasks are complete. static Task<Void>
whenAll(Collection<out Task<out Object>> tasks)
Creates a task that completes when all of the provided tasks are complete. Task<Void>
continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation. Task<Void>
continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, CancellationToken ct)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation. Task<Void>
continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, Executor executor)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation. <TContinuationResult> Task<TContinuationResult>
continueWith(Continuation<TResult, TContinuationResult> continuation, Executor executor)
Adds a continuation that will be scheduled using the executor, returning a new task thatcompletes after the continuation has finished running. <TContinuationResult> Task<TContinuationResult>
continueWith(Continuation<TResult, TContinuationResult> continuation)
Adds a synchronous continuation to this task, returning a new task that completes after thecontinuation has finished running. <TContinuationResult> Task<TContinuationResult>
continueWith(Continuation<TResult, TContinuationResult> continuation, Executor executor, CancellationToken ct)
Adds a continuation that will be scheduled using the executor, returning a new task thatcompletes after the continuation has finished running. <TContinuationResult> Task<TContinuationResult>
continueWith(Continuation<TResult, TContinuationResult> continuation, CancellationToken ct)
Adds a synchronous continuation to this task, returning a new task that completes after thecontinuation has finished running. <TContinuationResult> Task<TContinuationResult>
continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, 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. <TContinuationResult> Task<TContinuationResult>
continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor, CancellationToken ct)
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. <TContinuationResult> Task<TContinuationResult>
continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation)
Adds an asynchronous continuation to this task, returning a new task that completes after thetask returned by the continuation has completed. <TContinuationResult> Task<TContinuationResult>
continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct)
Adds an asynchronous continuation to this task, returning a new task that completes after thetask returned by the continuation has completed. <TContinuationResult> Task<TContinuationResult>
onSuccess(Continuation<TResult, TContinuationResult> continuation, Executor executor)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exception or cancellation. <TContinuationResult> Task<TContinuationResult>
onSuccess(Continuation<TResult, TContinuationResult> continuation)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exceptions or cancellation. <TContinuationResult> Task<TContinuationResult>
onSuccess(Continuation<TResult, TContinuationResult> continuation, CancellationToken ct)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exceptions or cancellation. <TContinuationResult> Task<TContinuationResult>
onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exceptions or cancellation. <TContinuationResult> Task<TContinuationResult>
onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exceptions or cancellation. boolean
trySetCancelled()
Sets the cancelled flag on the Task if the Task hasn't already been completed. boolean
trySetResult(TResult result)
Sets the result on the Task if the Task hasn't already been completed. boolean
trySetError(Exception error)
Sets the error on the Task if the Task hasn't already been completed. static Task.UnobservedExceptionHandler
getUnobservedExceptionHandler()
Returns the handler invoked when a task has an unobservedexception or {@code null}
.TResult
getResult()
Exception
getError()
static void
setUnobservedExceptionHandler(Task.UnobservedExceptionHandler eh)
Set the handler invoked when a task has an unobserved exception. -
-
Method Detail
-
create
static <TResult> TaskCompletionSource create(Call<TResult> call)
-
isCompleted
boolean isCompleted()
-
isCancelled
boolean isCancelled()
-
isFaulted
boolean isFaulted()
-
waitForCompletion
void waitForCompletion()
Blocks until the task is complete.
-
waitForCompletion
boolean waitForCompletion(long duration, TimeUnit timeUnit)
Blocks until the task is complete or times out.
-
delay
static 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
static 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 tocompleting the returned task.
-
delay
static Task<Void> delay(long delay, ScheduledExecutorService executor, CancellationToken cancellationToken)
-
cast
<TOut> Task<TOut> cast()
Makes a fluent cast of a Task's result possible, avoiding an extra continuation just to castthe type of the result.
-
callInBackground
static <TResult> 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 java.util.concurrent.CancellationException from the callable.
-
callInBackground
static <TResult> Task<TResult> callInBackground(Callable<TResult> callable, CancellationToken ct)
Invokes the callable on a background thread, returning a Task to represent the operation.
-
call
static <TResult> 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 java.util.concurrent.CancellationException from the callable.
-
call
static <TResult> 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
static <TResult> 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 java.util.concurrent.CancellationException from the callable.
-
call
static <TResult> Task<TResult> call(Callable<TResult> callable, CancellationToken ct)
Invokes the callable on the current thread, producing a Task.
-
whenAnyResult
static <TResult> Task<Task<TResult>> whenAnyResult(Collection<out 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 taskwill always end in the completed state with its result set to the first task to complete. Thisis 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
static Task<Task<out Object>> whenAny(Collection<out Task<out Object>> 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 taskwill always end in the completed state with its result set to the first task to complete. Thisis 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
static <TResult> Task<List<TResult>> whenAllResult(Collection<out 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 completein a faulted state, where its exception will resolve to that java.lang.Exception if asingle task fails or an AggregateException of all the java.lang.Exceptionsif multiple tasks fail.
If none of the supplied tasks faulted but at least one of them was cancelled, the returnedtask will end as cancelled.
If none of the tasks faulted and none of the tasks were cancelled, the resulting task will endcompleted. The result of the returned task will be set to a list containing all of the resultsof the supplied tasks in the same order as they were provided (e.g. if the input tasks collectioncontained t1, t2, t3, the output task's result will return an
{@code List<TResult>}
where{@code 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 toa completed state before it's returned to the caller.The returned
{@code List<TResult>}
will contain 0 elements.- Parameters:
tasks
- The tasks that the return value will wait for before completing.
-
whenAll
static Task<Void> whenAll(Collection<out Task<out Object>> 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 completein a faulted state, where its exception will resolve to that java.lang.Exception if asingle task fails or an AggregateException of all the java.lang.Exceptionsif multiple tasks fail.
If none of the supplied tasks faulted but at least one of them was cancelled, the returnedtask will end as cancelled.
If none of the tasks faulted and none of the tasks were canceled, the resulting task willend in the completed state.
If the supplied collection contains no tasks, the returned task will immediately transitionto a completed state before it's returned to the caller.
- Parameters:
tasks
- The tasks that the return value will wait for before completing.
-
continueWhile
Task<Void> continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation.
-
continueWhile
Task<Void> continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, CancellationToken ct)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation.
-
continueWhile
Task<Void> continueWhile(Callable<Boolean> predicate, Continuation<Void, Task<Void>> continuation, Executor executor)
Continues a task with the equivalent of a Task-based while loop, where the body of the loop isa task continuation.
-
continueWith
<TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult, TContinuationResult> continuation, Executor executor)
Adds a continuation that will be scheduled using the executor, returning a new task thatcompletes after the continuation has finished running. This allows the continuation to bescheduled on different thread.
-
continueWith
<TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult, TContinuationResult> continuation)
Adds a synchronous continuation to this task, returning a new task that completes after thecontinuation has finished running.
-
continueWith
<TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult, TContinuationResult> continuation, Executor executor, CancellationToken ct)
Adds a continuation that will be scheduled using the executor, returning a new task thatcompletes after the continuation has finished running. This allows the continuation to bescheduled on different thread.
-
continueWith
<TContinuationResult> Task<TContinuationResult> continueWith(Continuation<TResult, TContinuationResult> continuation, CancellationToken ct)
Adds a synchronous continuation to this task, returning a new task that completes after thecontinuation has finished running.
-
continueWithTask
<TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, 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.
-
continueWithTask
<TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor, CancellationToken ct)
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.
-
continueWithTask
<TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation)
Adds an asynchronous continuation to this task, returning a new task that completes after thetask returned by the continuation has completed.
-
continueWithTask
<TContinuationResult> Task<TContinuationResult> continueWithTask(Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct)
Adds an asynchronous continuation to this task, returning a new task that completes after thetask returned by the continuation has completed.
-
onSuccess
<TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult, TContinuationResult> continuation, Executor executor)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exception or cancellation.
-
onSuccess
<TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult, TContinuationResult> continuation)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exceptions or cancellation.
-
onSuccess
<TContinuationResult> Task<TContinuationResult> onSuccess(Continuation<TResult, TContinuationResult> continuation, CancellationToken ct)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exceptions or cancellation.
-
onSuccessTask
<TContinuationResult> Task<TContinuationResult> onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exceptions or cancellation.
-
onSuccessTask
<TContinuationResult> Task<TContinuationResult> onSuccessTask(Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct)
Runs a continuation when a task completes successfully, forwarding along java.lang.Exceptions or cancellation.
-
trySetCancelled
boolean trySetCancelled()
Sets the cancelled flag on the Task if the Task hasn't already been completed.
-
trySetResult
boolean trySetResult(TResult result)
Sets the result on the Task if the Task hasn't already been completed.
-
trySetError
boolean trySetError(Exception error)
Sets the error on the Task if the Task hasn't already been completed.
-
getUnobservedExceptionHandler
static Task.UnobservedExceptionHandler getUnobservedExceptionHandler()
Returns the handler invoked when a task has an unobservedexception or
{@code null}
.
-
setUnobservedExceptionHandler
static void setUnobservedExceptionHandler(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.
-
-
-
-