Consider the following Kotlin coroutine code snippet. What will be printed to the console?
import kotlinx.coroutines.* fun main() = runBlocking { println("Start: ${Thread.currentThread().name}") withContext(Dispatchers.IO) { println("Inside IO: ${Thread.currentThread().name}") } println("End: ${Thread.currentThread().name}") }
Remember that withContext(Dispatchers.IO) switches the coroutine context to a background thread pool.
The runBlocking runs on the main thread. The withContext(Dispatchers.IO) block switches to a background thread from the IO dispatcher thread pool, so the thread name changes inside that block. After it finishes, execution returns to the main thread.
What will be the output of this Kotlin coroutine code?
import kotlinx.coroutines.* fun main() = runBlocking { println("Before withContext: ${Thread.currentThread().name}") withContext(Dispatchers.Default) { println("Inside withContext: ${Thread.currentThread().name}") } println("After withContext: ${Thread.currentThread().name}") }
Think about how runBlocking and withContext interact with dispatchers.
runBlocking runs on the main thread by default. When withContext(Dispatchers.Default) is called, it switches to a background thread from the Default dispatcher thread pool. After the block, execution returns to the main thread.
What error will this Kotlin code produce when run?
import kotlinx.coroutines.* fun main() { withContext(Dispatchers.IO) { println("Running in IO dispatcher") } }
Check if withContext is called inside a coroutine or suspend function.
withContext is a suspend function and must be called from within a coroutine or another suspend function. Calling it directly in main() without runBlocking or a coroutine scope causes a compiler error.
Analyze the output of this Kotlin coroutine code with nested withContext calls:
import kotlinx.coroutines.* fun main() = runBlocking { println("Start: ${Thread.currentThread().name}") withContext(Dispatchers.Default) { println("Default: ${Thread.currentThread().name}") withContext(Dispatchers.IO) { println("IO: ${Thread.currentThread().name}") } println("Back to Default: ${Thread.currentThread().name}") } println("End: ${Thread.currentThread().name}") }
Each withContext switches to the specified dispatcher thread pool.
The outer withContext(Dispatchers.Default) runs on a Default dispatcher thread. The inner withContext(Dispatchers.IO) switches to a different thread from the IO dispatcher pool. After the inner block, execution returns to the Default dispatcher thread, then finally back to the main thread.
Which statement best explains why withContext is preferred over launch when switching dispatchers for a block of code?
Think about blocking vs non-blocking behavior and sequential code flow.
withContext is a suspend function that switches the coroutine context and suspends until the block finishes, keeping code sequential. launch starts a new coroutine that runs concurrently and does not wait for completion, so it is not suitable for sequential dispatcher switching.