Dispatchers
Main, IO, Default dispatchers and withContext for thread switching.
1
coroutine dispatchers
2Dispatcher Main in Kotlin
Dispatchers.Main runs coroutines on UI thread in Android/UI apps. A Kotlin example with source code and sample output.
coroutine dispatchers
3Dispatcher IO in Kotlin
Dispatchers.IO is optimized for blocking I/O operations. A Kotlin example with source code and sample output.
coroutine dispatchers
4Dispatcher Default in Kotlin
Dispatchers.Default is for CPU-intensive work. A Kotlin example with source code and sample output.
coroutine dispatchers
5withContext Dispatcher in Kotlin
withContext switches coroutine to another dispatcher and returns result. A Kotlin example with source code and sample output.
coroutine dispatchers
Custom Dispatcher in Kotlin
Custom dispatchers can be created using Executors and asCoroutineDispatcher(). A Kotlin example with source code and sample output.