CompletableJob

A job that can be completed using complete() function. It is returned by Job() and SupervisorJob() constructor functions.

All functions on this interface are thread-safe and can be safely invoked from concurrent coroutines without external synchronization.

The CompletableJob interface is not stable for inheritance in 3rd party libraries, as new methods might be added to this interface in the future, but is stable for use.

Functions

Link copied to clipboard
abstract fun complete(): Boolean

Completes this job. The result is true if this job was completed as a result of this invocation and false otherwise (if it was already completed).

Link copied to clipboard
abstract fun completeExceptionally(exception: Throwable): Boolean

Completes this job exceptionally with a given exception. The result is true if this job was completed as a result of this invocation and false otherwise (if it was already completed). exception parameter is used as an additional debug information that is not handled by any exception handlers.

Extensions

Link copied to clipboard
fun Job.cancel(message: String, cause: Throwable? = null)

Cancels current job, including all its children with a specified diagnostic error message. A cause can be specified to provide additional details on a cancellation reason for debugging purposes.

Cancels Job of this context with an optional cancellation cause. See Job.cancel for details.

Link copied to clipboard
suspend fun Job.cancelAndJoin()

Cancels the job and suspends the invoking coroutine until the cancelled job is complete.

Link copied to clipboard

Cancels all children jobs of this coroutine using Job.cancel for all of them with an optional cancellation cause. Unlike Job.cancel on this job as a whole, the state of this job itself is not affected.

Cancels all children of the Job in this context, without touching the state of this job itself with an optional cancellation cause. See Job.cancel. It does not do anything if there is no job in the context or it has no children.

Link copied to clipboard

Ensures that current job is active. If the job is no longer active, throws CancellationException. If the job was cancelled, thrown exception contains the original cancellation cause.

Ensures that job in the current context is active.

Link copied to clipboard

Returns true when the Job of the coroutine in this context is still active (has not completed and was not cancelled yet).

Link copied to clipboard

Retrieves the current Job instance from the given CoroutineContext or throws IllegalStateException if no job is present in the context.