Dispatchers
In this page:
What a Dispatcher Does
A dispatcher controls which thread (or thread pool) the coroutine's code runs on; different dispatchers are tuned for different kinds of work, like CPU computation versus blocking I/O.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: What a Dispatcher Does
import kotlinx.coroutines.*
fun main() {
runBlocking {
launch(Dispatchers.Default) {
println("Running on Dispatchers.Default: ${Thread.currentThread().name}")
}
}
}
Login to try C/C++/Java/PHP code in the editor
Dispatchers.IO for Blocking Operations
Dispatchers.IO is backed by a larger thread pool designed to handle many concurrent blocking operations, such as file or network access, without exhausting the CPU-focused thread pool.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Dispatchers.IO for Blocking Operations
import kotlinx.coroutines.*
fun main() = runBlocking {
withContext(Dispatchers.IO) {
println("Simulating a blocking IO operation")
}
println("Back on the original context")
}
Login to try C/C++/Java/PHP code in the editor
Switching Context with withContext
withContext(dispatcher) { } suspends the current coroutine, runs the given block on the specified dispatcher, and then resumes back with the block's result once it finishes.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Switching Context with withContext
import kotlinx.coroutines.*
suspend fun computeSum(): Int = withContext(Dispatchers.Default) {
(1..1000).sum()
}
fun main() = runBlocking {
println("Sum: ${computeSum()}")
}
Login to try C/C++/Java/PHP code in the editor
Dispatchers.Default for CPU Work
Dispatchers.Default is sized based on the number of CPU cores and is meant for computationally intensive work, keeping it separate from dispatchers meant for blocking I/O.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Dispatchers.Default for CPU Work
import kotlinx.coroutines.*
fun main() = runBlocking {
val result = withContext(Dispatchers.Default) {
(1..100).map { it * it }.sum()
}
println("Sum of squares: $result")
}
Login to try C/C++/Java/PHP code in the editor
- Using
Dispatchers.Mainoutside an environment (like Android) that actually provides a main/UI thread dispatcher implementation. - Doing heavy CPU or I/O work on
Dispatchers.DefaultwhenDispatchers.IOis designed and sized specifically for blocking I/O operations. - Forgetting
withContextsuspends until the block finishes, and assuming it behaves likelaunchby returning immediately.
- A dispatcher determines which thread or thread pool a coroutine's code actually executes on.
Dispatchers.Defaultis optimized for CPU-intensive work;Dispatchers.IOis optimized for blocking I/O operations.withContext(dispatcher) { }switches the current coroutine to a different dispatcher for the duration of that block, then switches back.- Choosing the right dispatcher keeps CPU-heavy and I/O-heavy work from starving each other's thread pools.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: