Deferred
Deferred value is a non-blocking cancellable future.
It is created with async coroutine builder or via constructor of CompletableDeferred class. It is in active state while the value is being computed.
Deferred value has the following states:
State | isActive | isCompleted | isCompletedExceptionally | isCancelled |
---|---|---|---|---|
New (optional initial state) | false |
false |
false |
false |
Active (default initial state) | true |
false |
false |
false |
Completing (optional transient state) | true |
false |
false |
false |
Cancelling (optional transient state) | false |
false |
false |
true |
Cancelled (final state) | false |
true |
true |
true |
Resolved (final state) | false |
true |
false |
false |
Failed (final state) | false |
true |
true |
false |
Usually, a deferred value is created in active state (it is created and started).
However, async coroutine builder has an optional start
parameter that creates a deferred value in new state
when this parameter is set to CoroutineStart.LAZY.
Such a deferred can be be made active by invoking start, join, or await.
A deferred can be cancelled at any time with cancel function that forces it to transition to cancelling state immediately. Deferred that is not backed by a coroutine (see CompletableDeferred) and does not have children becomes cancelled on cancel immediately. Otherwise, deferred becomes cancelled when it finishes executing its code and when all its children complete.
wait children +-----+ start +--------+ complete +-------------+ finish +-----------+ | New | ---------------> | Active | ----------> | Completing | ---+-> | Resolved | +-----+ +--------+ +-------------+ | |(completed)| | | | | +-----------+ | cancel | cancel | cancel | V V | | +-----------+ +-----------+ finish +------------+ | +-> | Failed | | Cancelled | <--------- | Cancelling | <---------------+ |(completed)| |(completed)| +------------+ +-----------+ +-----------+
A deferred value is a Job. A job in the coroutine context of async builder represents the coroutine itself. A deferred value is active while the coroutine is working and cancellation aborts the coroutine when the coroutine is suspended on a cancellable suspension point by throwing CancellationException or the cancellation cause inside the coroutine.
A deferred value can have a parent job. A deferred value with a parent is cancelled when its parent is cancelled or completes. Parent waits for all its children to complete in completing or cancelling state. Completing state is purely internal. For an outside observer a completing deferred is still active, while internally it is waiting for its children.
All functions on this interface and on all interfaces derived from it are thread-safe and can be safely invoked from concurrent coroutines without external synchronization.
Properties
abstract val isCompletedExceptionally: Boolean Returns |
|
abstract val onAwait: SelectClause1<T> Clause for select expression of await suspending function that selects with the deferred value when it is resolved. The select invocation fails if the deferred value completes exceptionally (either fails or it cancelled). |
Inherited Properties
Returns a sequence of this job’s children. |
|
abstract val isActive: Boolean Returns |
|
abstract val isCancelled: Boolean Returns |
|
abstract val isCompleted: Boolean Returns |
|
abstract val onJoin: SelectClause0 Clause for select expression of join suspending function that selects when the job is complete. This clause never fails, even if the job completes exceptionally. |
Functions
abstract suspend fun await(): T Awaits for completion of this value without blocking a thread and resumes when deferred computation is complete, returning the resulting value or throwing the corresponding exception if the deferred had completed exceptionally. |
|
abstract fun getCompleted(): T Returns completed result or throws IllegalStateException if this deferred value has not completed yet. It throws the corresponding exception if this deferred has completed exceptionally. |
|
abstract fun getCompletionExceptionOrNull(): Throwable? Returns completion exception result if this deferred completed exceptionally,
|
Inherited Functions
abstract fun |
|
Cancels this job with an optional cancellation cause. The result is |
|
abstract fun getCancellationException(): CancellationException Returns CancellationException that signals the completion of this job. This function is used by cancellable suspending functions. They throw exception returned by this function when they suspend in the context of this job and this job becomes complete. |
|
abstract fun invokeOnCompletion( Registers handler that is synchronously invoked once on completion of this job.
When job is already complete, then the handler is immediately invoked
with a job’s exception or cancellation cause or abstract fun invokeOnCompletion( Registers handler that is synchronously invoked once on cancellation or completion of this job.
When job is already cancelling or complete, then the handler is immediately invoked
with a job’s cancellation cause or |
|
abstract suspend fun join(): Unit Suspends coroutine until this job is complete. This invocation resumes normally (without exception) when the job is complete for any reason and the Job of the invoking coroutine is still active. This function also starts the corresponding coroutine if the Job was still in new state. |
|
abstract fun start(): Boolean Starts coroutine related to this job (if any) if it was not started yet.
The result |
Extension Functions
Converts this deferred value to the instance of Promise. |
|
fun CoroutineContext.cancel( Cancels Job of this context with an optional cancellation cause. The result is |
|
Cancels the job and suspends invoking coroutine until the cancelled job is complete. |
|
fun Job.cancelFutureOnCompletion( Cancels a specified future when this job is complete. |
|
fun Job.disposeOnCompletion( Disposes a specified handle when this job is complete. |
|
Suspends coroutine until all children of this job are complete using Job.join for all of them. Unlike Job.join on this job as a whole, it does not wait until this job is complete. |
Inheritors
A Deferred that can be completed via public functions complete, completeExceptionally, and cancel. |