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
:
- Unconfined – starts coroutine execution in the current call-frame until the first suspension. On first suspension the coroutine builder function returns. The coroutine will resume in whatever thread that is used by the corresponding suspending function, without confining it to any specific thread or pool. This in an appropriate choice for IO-intensive coroutines that do not consume CPU resources.
- DefaultDispatcher – is used by all standard builder if no dispatcher nor any other ContinuationInterceptor is specified in their context. It is currently equal to CommonPool (subject to change).
- CommonPool – immediately returns from the coroutine builder and schedules coroutine execution to a common pool of shared background threads. This is an appropriate choice for compute-intensive coroutines that consume a lot of CPU resources.
- Private thread pools can be created with newSingleThreadContext and newFixedThreadPoolContext.
- An arbitrary Executor can be converted to dispatcher with asCoroutineDispatcher extension function.
This class ensures that debugging facilities in newCoroutineContext function work properly.
Constructors
CoroutineDispatcher() Base class that shall be extended by all coroutine dispatcher implementations. |
Functions
abstract fun dispatch( abstract fun dispatch( Dispatches execution of a runnable block onto another thread in the given context. |
|
open fun <T> interceptContinuation( Returns continuation that wraps the original continuation, thus intercepting all resumptions. |
|
open fun isDispatchNeeded(context: CoroutineContext): Boolean Returns |
|
open fun toString(): String |
Extension Functions
fun CoroutineContext.cancel( Cancels Job of this context with an optional cancellation cause. The result is |
|
fun CoroutineContext.cancelChildren( 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
object CommonPool : CoroutineDispatcher Represents common pool of shared threads as coroutine dispatcher for compute-intensive tasks. |
|
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. |