CoroutineDispatcher

abstract class CoroutineDispatcher : 
    AbstractCoroutineContextElement,
    ContinuationInterceptor
(source)

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

The following standard implementations are provided by kotlinx.coroutines:

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
abstract fun dispatch(
    context: CoroutineContext,
    block: Runnable
): Unit

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

interceptContinuation

open 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.

toString

open fun toString(): String

Extension Functions

cancel

fun CoroutineContext.cancel(
    cause: Throwable? = null
): Boolean

Cancels Job of this context with an optional cancellation cause. The result is true if the job was cancelled as a result of this invocation and false if there is no job in the context or if it was already cancelled or completed. See Job.cancel for details.

cancelChildren

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

Cancels all children of the Job in this context with an optional cancellation cause. It does not do anything if there is no job in the context or it has no children. See Job.cancelChildren for details.

Inheritors

CommonPool

object CommonPool : CoroutineDispatcher

Represents common pool of shared threads as coroutine dispatcher for compute-intensive tasks.

Unconfined

object Unconfined : CoroutineDispatcher

A coroutine dispatcher that is not confined to any specific thread. It executes initial continuation of the coroutine right here in the current call-frame and let the coroutine resume in whatever thread that is used by the corresponding suspending function, without mandating any specific threading policy.