Dispatchers help your program decide where to run tasks. This keeps your app smooth and fast by running work in the right place.
Dispatchers.Main, IO, Default behavior in Kotlin
launch(Dispatchers.Main) { /* UI work here */ }
launch(Dispatchers.IO) { /* Disk or network work here */ }
launch(Dispatchers.Default) { /* CPU heavy work here */ }Dispatchers.Main runs on the main thread, good for UI updates.
Dispatchers.IO is for input/output tasks like files or network.
CoroutineScope(Dispatchers.Main).launch {
println("Running on Main thread")
}CoroutineScope(Dispatchers.IO).launch {
println("Running on IO thread")
}CoroutineScope(Dispatchers.Default).launch {
println("Running on Default thread")
}This program launches three coroutines on different dispatchers and prints the thread names to show where each runs.
import kotlinx.coroutines.* fun main() = runBlocking { launch(Dispatchers.Main) { println("Main: Thread = ${Thread.currentThread().name}") } launch(Dispatchers.IO) { println("IO: Thread = ${Thread.currentThread().name}") } launch(Dispatchers.Default) { println("Default: Thread = ${Thread.currentThread().name}") } }
Dispatchers.Main requires a platform that supports a main thread, like Android or JavaFX.
Dispatchers.IO uses a shared pool of threads optimized for blocking IO tasks.
Dispatchers.Default is optimized for CPU-intensive work and uses a shared pool of threads.
Dispatchers.Main is for UI work on the main thread.
Dispatchers.IO is for input/output tasks like files and network.
Dispatchers.Default is for CPU-heavy tasks to keep the app responsive.
Practice
Dispatcher should you use in Kotlin when updating the user interface (UI)?Solution
Step 1: Understand UI thread requirements
UI updates must happen on the main thread to avoid crashes or freezes.Step 2: Match Dispatcher to UI work
Dispatchers.Mainis designed for UI tasks on the main thread.Final Answer:
Dispatchers.Main -> Option AQuick Check:
UI work = Dispatchers.Main [OK]
- Using Dispatchers.IO for UI updates
- Using Dispatchers.Default for UI work
- Confusing Dispatchers.Unconfined with Main
Solution
Step 1: Identify correct CoroutineScope usage
To launch a coroutine on a specific dispatcher, create CoroutineScope with that dispatcher.Step 2: Check syntax correctness
CoroutineScope(Dispatchers.IO).launch { ... }is correct syntax for IO dispatcher.Final Answer:
CoroutineScope(Dispatchers.IO).launch { /* IO work */ } -> Option DQuick Check:
IO dispatcher launch = CoroutineScope(Dispatchers.IO).launch { /* IO work */ } [OK]
- Using Dispatchers.Main for IO tasks
- Incorrect CoroutineScope.launch syntax
- Using Dispatchers.Default for IO tasks
import kotlinx.coroutines.*
fun main() = runBlocking {
launch(Dispatchers.Default) {
println("Running on: " + Thread.currentThread().name)
}.join()
}Solution
Step 1: Identify the dispatcher used
The coroutine is launched withDispatchers.Default, which uses a shared background thread pool.Step 2: Understand thread naming
Threads in Default dispatcher are named like "DefaultDispatcher-worker-1".Final Answer:
Running on: DefaultDispatcher-worker-1 -> Option AQuick Check:
Dispatchers.Default runs on DefaultDispatcher-worker threads [OK]
- Assuming it runs on main thread
- Confusing IO thread names with Default
- Expecting Unconfined dispatcher output
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO) {
// Perform file read
}
println("File read complete")
}
What is the main issue if this code is run in an Android app?Solution
Step 1: Analyze dispatcher switching
The code launches on Main dispatcher, then switches to IO for file reading.Step 2: Confirm correct usage
Switching to Dispatchers.IO for file I/O and back to Main for UI is correct practice.Final Answer:
No issue; this is the correct way to switch dispatchers -> Option CQuick Check:
Dispatcher switching Main ↔ IO is correct [OK]
- Thinking Dispatchers.Default is for I/O
- Believing println runs on IO dispatcher
- Assuming dispatcher switch causes crash
Select the best option.
Solution
Step 1: Identify task type
CPU-intensive tasks need background threads to avoid freezing UI.Step 2: Match dispatcher to CPU work
Dispatchers.Defaultis designed for CPU-heavy tasks using a shared thread pool.Final Answer:
Dispatchers.Default, because it is optimized for CPU-heavy work -> Option BQuick Check:
CPU tasks = Dispatchers.Default [OK]
- Using Main dispatcher for CPU work
- Confusing IO dispatcher with CPU dispatcher
- Using Unconfined dispatcher for heavy tasks
Ready to prove it?
Pass the test and this pattern moves unknown
