CoroutineDispatcher

Base class to be extended by all coroutine dispatcher implementations.

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

  • Dispatchers.Default — is used by all standard builders if no dispatcher or any other ContinuationInterceptor is specified in their context. It uses a common pool of shared background threads. This is an appropriate choice for compute-intensive coroutines that consume CPU resources.

  • Dispatchers.IO — uses a shared pool of on-demand created threads and is designed for offloading of IO-intensive blocking operations (like file I/O and blocking socket I/O).

  • Dispatchers.Unconfined — starts coroutine execution in the current call-frame until the first suspension, whereupon the coroutine builder function returns. The coroutine will later resume in whatever thread used by the corresponding suspending function, without confining it to any specific thread or pool. The Unconfined dispatcher should not normally be used in code.

  • Private thread pools can be created with newSingleThreadContext and newFixedThreadPoolContext.

  • An arbitrary java.util.concurrent.Executor can be converted to a dispatcher with the asCoroutineDispatcher extension function.

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

Constructors

CoroutineDispatcher
Link copied to clipboard
fun CoroutineDispatcher()

Functions

dispatch
Link copied to clipboard
abstract fun dispatch(context: CoroutineContext, block: Runnable)

Dispatches execution of a runnable block onto another thread in the given context. This method should guarantee that the given block will be eventually invoked, otherwise the system may reach a deadlock state and never leave it. Cancellation mechanism is transparent for CoroutineDispatcher and is managed by block internals.

interceptContinuation
Link copied to clipboard
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T>

Returns a continuation that wraps the provided continuation, thus intercepting all resumptions.

isDispatchNeeded
Link copied to clipboard
open fun isDispatchNeeded(context: CoroutineContext): Boolean

Returns true if the execution of the coroutine should be performed with dispatch method. The default behavior for most dispatchers is to return true.

limitedParallelism
Link copied to clipboard
open fun limitedParallelism(parallelism: Int): CoroutineDispatcher

Creates a view of the current dispatcher that limits the parallelism to the given value. The resulting view uses the original dispatcher for execution, but with the guarantee that no more than parallelism coroutines are executed at the same time.

releaseInterceptedContinuation
Link copied to clipboard
override fun releaseInterceptedContinuation(continuation: Continuation<*>)

Inheritors

MainCoroutineDispatcher
Link copied to clipboard
CloseableCoroutineDispatcher
Link copied to clipboard
ExecutorCoroutineDispatcher
Link copied to clipboard

Extensions

asExecutor
Link copied to clipboard
fun CoroutineDispatcher.asExecutor(): Executor

Converts an instance of CoroutineDispatcher to an implementation of Executor.

invoke
Link copied to clipboard
inline suspend operator fun <T> CoroutineDispatcher.invoke(noinline block: suspend CoroutineScope.() -> T): T

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

Sources

common source
Link copied to clipboard