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
Unconfineddispatcher 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.
Requests execution of a runnable block. The dispatcher guarantees that block will eventually execute, typically by dispatching it to a thread pool, using a dedicated thread, or just executing the block in place. The context parameter represents the context of the coroutine that is being dispatched, or EmptyCoroutineContext if a non-coroutine-specific Runnable is dispatched instead. Implementations may use context for additional context-specific information, such as priority, whether the dispatched coroutine can be invoked in place, coroutine name, and additional diagnostic elements.