← Playground
Kotlin · Android
2 minutes

Which thread does this run on?

A coroutine can pause and resume — but it still runs on a real thread. The dispatcher picks which one. Route each job to the right lane. Pick wrong and you'll freeze the UI (ANR) or crash it.
Score 0 / 8
Task 1 of 8
launch(/* which dispatcher? */) {

Download JSON from a REST API

}
The one-line takeaway

A coroutine is just work that can pause and resume. A dispatcher decides which thread that work runs on. Match the job to the pool: Main for UI, IO for waiting on network/disk, Default for CPU-heavy number crunching — and use withContext(...) to hop between them.


One coroutine, three lanes

withContext(...) lets a single coroutine hop between threads — do the network on IO, the number-crunching on Default, and come back to Main to update the UI, all without callbacks.

viewModelScope.launch {
    // Main by default — safe to touch UI
    showLoading(true)

    val user = withContext(Dispatchers.IO) {
        api.fetchUser()          // network → IO
    }

    val score = withContext(Dispatchers.Default) {
        heavyRankingCompute(user)  // CPU → Default
    }

    scoreText.text = score.toString()   // back on Main
    showLoading(false)
}
Enjoyed this?

I build Android & cross-platform apps freelance and love making hard ideas simple.