abstract fun dispatch(context: CoroutineContext, block: Runnable)

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.

This method should guarantee that the given block will be eventually invoked, otherwise the system may reach a deadlock state and never leave it. The cancellation mechanism is transparent for CoroutineDispatcher and is managed by block internals.

This method should generally be exception-safe. An exception thrown from this method may leave the coroutines that use this dispatcher in an inconsistent and hard-to-debug state.

This method must not immediately call block. Doing so may result in StackOverflowError when dispatch is invoked repeatedly, for example when yield is called in a loop. In order to execute a block in place, it is required to return false from isDispatchNeeded and delegate the dispatch implementation to Dispatchers.Unconfined.dispatch in such cases. To support this, the coroutines machinery ensures in-place execution and forms an event-loop to avoid unbound recursion.

See also


Link copied to clipboard