0
0
Kotlinprogramming~20 mins

Dispatchers.Main, IO, Default behavior in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Dispatcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this coroutine dispatcher code?

Consider this Kotlin coroutine code snippet. What will it print?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Main) {
        println("Running on Main: ${Thread.currentThread().name}")
    }
    launch(Dispatchers.IO) {
        println("Running on IO: ${Thread.currentThread().name}")
    }
    launch(Dispatchers.Default) {
        println("Running on Default: ${Thread.currentThread().name}")
    }
}
A
Running on Main: DefaultDispatcher-worker-1
Running on IO: main
Running on Default: main
B
Running on Main: DefaultDispatcher-worker-1
Running on IO: DefaultDispatcher-worker-2
Running on Default: DefaultDispatcher-worker-3
C
Running on Main: main
Running on IO: DefaultDispatcher-worker-1
Running on Default: DefaultDispatcher-worker-2
D
Running on Main: main
Running on IO: main
Running on Default: main
Attempts:
2 left
💡 Hint

Remember that Dispatchers.Main runs on the main thread, while Dispatchers.IO and Dispatchers.Default use background threads.

Predict Output
intermediate
2:00remaining
What happens if you launch a coroutine with Dispatchers.Main in a non-Android JVM?

What will happen when running this Kotlin coroutine code on a plain JVM (no Android)?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Main) {
        println("Hello from Main")
    }
}
AThrows an IllegalStateException because Dispatchers.Main is not available.
BRuns on a background thread named DefaultDispatcher-worker-1.
CPrints "Hello from Main" successfully on the main thread.
DDeadlocks and never prints anything.
Attempts:
2 left
💡 Hint

Think about whether Dispatchers.Main is supported outside Android or JavaFX environments.

Predict Output
advanced
2:00remaining
What is the output of this mixed dispatcher coroutine code?

Analyze the output of this Kotlin coroutine code snippet:

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch(Dispatchers.Default) {
        println("Default start: ${Thread.currentThread().name}")
        withContext(Dispatchers.IO) {
            println("Inside IO: ${Thread.currentThread().name}")
        }
        println("Back to Default: ${Thread.currentThread().name}")
    }
    job.join()
}
A
Default start: DefaultDispatcher-worker-1
Inside IO: DefaultDispatcher-worker-1
Back to Default: DefaultDispatcher-worker-1
B
Default start: DefaultDispatcher-worker-1
Inside IO: main
Back to Default: DefaultDispatcher-worker-1
C
Default start: main
Inside IO: DefaultDispatcher-worker-1
Back to Default: main
D
Default start: DefaultDispatcher-worker-1
Inside IO: DefaultDispatcher-worker-2
Back to Default: DefaultDispatcher-worker-1
Attempts:
2 left
💡 Hint

Remember that withContext switches the coroutine context temporarily.

Predict Output
advanced
2:00remaining
What error does this code raise when using Dispatchers.Main improperly?

What error will this Kotlin code produce?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Main.immediate) {
        println("Running immediate on Main")
    }
}
AIllegalStateException: Module with the Main dispatcher is missing.
BNo error, prints "Running immediate on Main" on main thread.
CRuntimeException: Immediate dispatcher not supported on this platform.
DDeadlock because immediate dispatcher tries to run on main thread synchronously.
Attempts:
2 left
💡 Hint

Consider if the Main dispatcher module is included in your project.

🧠 Conceptual
expert
2:00remaining
Which dispatcher is best suited for CPU-intensive tasks and why?

Choose the best dispatcher for CPU-heavy computations and explain why.

ADispatchers.IO because it uses a large thread pool optimized for blocking IO tasks.
BDispatchers.Default because it uses a shared pool of threads optimized for CPU-intensive work.
CDispatchers.Main because it runs on the main thread and ensures UI responsiveness.
DA new single-threaded dispatcher to avoid thread switching overhead.
Attempts:
2 left
💡 Hint

Think about thread pool size and task type for CPU-bound work.