...

/

Mechanism and Performance of Dispatchers

Mechanism and Performance of Dispatchers

Learn which mechanism dispatchers work on and the performance of dispatchers against different tasks.

Continuation interceptor

Dispatching works based on the mechanism of continuation interception, built into the Kotlin language. There is a coroutine context named ContinuationInterceptor whose interceptContinuation method is used to modify a continuation when a coroutine is suspended. Thanks to the caching mechanism, wrapping needs to happen only once per continuation. It also has a releaseInterceptedContinuation method called when a continuation ends.

Press + to interact
public interface ContinuationInterceptor :
CoroutineContext.Element {
companion object Key :
CoroutineContext.Key<ContinuationInterceptor>
fun <T> interceptContinuation(
continuation: Continuation<T>
): Continuation<T>
fun releaseInterceptedContinuation(
continuation: Continuation<*>
) {
}
//...
}

The capability to wrap a continuation gives a lot of control. Dispatchers use interceptContinuation to wrap a continuation with DispatchedContinuation, which runs ...