Mechanism and Performance of Dispatchers
Explore how Kotlin coroutine dispatchers operate using continuation interception and how they determine thread execution for coroutines. Understand the performance of different dispatchers with various task types such as suspending, blocking, CPU-, and memory-intensive operations. Learn which dispatcher to choose for efficient coroutine performance and how to handle dispatcher usage in testing.
We'll cover the following...
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.
The capability to wrap a continuation gives a lot of control. Dispatchers use interceptContinuation to wrap a continuation with DispatchedContinuation, which runs on a specific pool of threads. This is how dispatchers work.
The ...