📖 Scenario: You are building a simple Kotlin program that simulates tasks running on different threads using Dispatchers.Main, Dispatchers.IO, and Dispatchers.Default. This helps you understand how Kotlin manages work on the main thread, background IO tasks, and CPU-intensive tasks.
🎯 Goal: Create a Kotlin program that launches three coroutines, each using a different dispatcher: Main, IO, and Default. Each coroutine prints a message showing which dispatcher it is running on.
📋 What You'll Learn
Create a CoroutineScope using Dispatchers.Main.
Create a variable ioScope using CoroutineScope(Dispatchers.IO).
Create a variable defaultScope using CoroutineScope(Dispatchers.Default).
Launch one coroutine in each scope that prints a message with the dispatcher name.
Print all messages so you can see which dispatcher runs which coroutine.
💡 Why This Matters
🌍 Real World
Understanding dispatchers helps you write Kotlin programs that run tasks on the right threads, improving app responsiveness and performance.
💼 Career
Many Kotlin developers use coroutines and dispatchers daily to manage background work, UI updates, and CPU-heavy tasks efficiently.
Progress0 / 4 steps
1
Set up CoroutineScopes for Main, IO, and Default
Create three variables: mainScope as CoroutineScope(Dispatchers.Main), ioScope as CoroutineScope(Dispatchers.IO), and defaultScope as CoroutineScope(Dispatchers.Default).
Kotlin
Hint
Use CoroutineScope(Dispatchers.Main) to create mainScope. Do the same for ioScope and defaultScope with Dispatchers.IO and Dispatchers.Default.
2
Launch coroutine in mainScope
Use mainScope.launch to start a coroutine that prints "Running on Main dispatcher".
Kotlin
Hint
Use mainScope.launch { println("Running on Main dispatcher") } to start the coroutine.
3
Launch coroutines in ioScope and defaultScope
Use ioScope.launch to print "Running on IO dispatcher" and defaultScope.launch to print "Running on Default dispatcher".
Kotlin
Hint
Use ioScope.launch { println("Running on IO dispatcher") } and defaultScope.launch { println("Running on Default dispatcher") }.
4
Wait for coroutines to finish and print all messages
Add Thread.sleep(500) at the end to allow all coroutines to complete and print their messages.
Kotlin
Hint
Use Thread.sleep(500) to pause the main thread so all coroutines can print their messages.
Practice
(1/5)
1. Which Dispatcher should you use in Kotlin when updating the user interface (UI)?
easy
A. Dispatchers.Main
B. Dispatchers.IO
C. Dispatchers.Default
D. Dispatchers.Unconfined
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.Main is designed for UI tasks on the main thread.
Final Answer:
Dispatchers.Main -> Option A
Quick Check:
UI work = Dispatchers.Main [OK]
Hint: UI updates always use Dispatchers.Main [OK]
Common Mistakes:
Using Dispatchers.IO for UI updates
Using Dispatchers.Default for UI work
Confusing Dispatchers.Unconfined with Main
2. Which of the following is the correct way to launch a coroutine on the IO dispatcher in Kotlin?
easy
A. CoroutineScope.launch(Dispatchers.IO) { /* IO work */ }
B. CoroutineScope(Dispatchers.Main).launch { /* IO work */ }
C. CoroutineScope(Dispatchers.Default).launch { /* IO work */ }
D. CoroutineScope(Dispatchers.IO).launch { /* IO work */ }
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 D