CoroutineDispatcher

abstract class CoroutineDispatcher : ContinuationInterceptor (source)

Base class that shall be extended by all coroutine dispatcher implementations.

The following standard implementations are provided by kotlinx.coroutines as properties on Dispatchers objects:

This class ensures that debugging facilities in newCoroutineContext function work properly.

Constructors

<init>

CoroutineDispatcher()

Base class that shall be extended by all coroutine dispatcher implementations.

Functions

dispatch

abstract fun dispatch(
    context: CoroutineContext,
    block: Runnable
): Unit

Dispatches execution of a runnable block onto another thread in the given context.

interceptContinuation

fun <T> interceptContinuation(
    continuation: Continuation<T>
): Continuation<T>

Returns continuation that wraps the original continuation, thus intercepting all resumptions.

isDispatchNeeded

open fun isDispatchNeeded(context: CoroutineContext): Boolean

Returns true if execution shall be dispatched onto another thread. The default behaviour for most dispatchers is to return true.

Extension Properties

isActive

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

Extension Functions

asExecutor

fun CoroutineDispatcher.asExecutor(): Executor

Converts an instance of CoroutineDispatcher to an implementation of Executor.

cancel

fun CoroutineContext.cancel(
    cause: CancellationException? = null
): Unit

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

cancelChildren

fun CoroutineContext.cancelChildren(
    cause: CancellationException? = null
): Unit

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.

ensureActive

fun CoroutineContext.ensureActive(): Unit

Ensures that job in the current context is active. Throws IllegalStateException if the context does not have a job in it.

invoke

operator suspend fun <T> CoroutineDispatcher.invoke(
    block: suspend CoroutineScope.() -> T
): T

Calls the specified suspending block with the given CoroutineDispatcher, suspends until it completes, and returns the result.

Inheritors

ExecutorCoroutineDispatcher

abstract class ExecutorCoroutineDispatcher : 
    CoroutineDispatcher,
    Closeable

CoroutineDispatcher that has underlying Executor for dispatching tasks. Instances of ExecutorCoroutineDispatcher should be closed by the owner of the dispatcher.

MainCoroutineDispatcher

abstract class MainCoroutineDispatcher : CoroutineDispatcher

Base class for special CoroutineDispatcher which is confined to application “Main” or “UI” thread and used for any UI-based activities. Instance of MainDispatcher can be obtained by Dispatchers.Main.