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

cancel

fun CoroutineContext.cancel(): Unit

Cancels Job of this context. See Job.cancel for details.

cancelChildren

fun CoroutineContext.cancelChildren(): Unit

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

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

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.