Dispatchers and coroutine scope
Dispatchers and Coroutine Scope Deep Dive¶
Overview¶
Dispatchers and scopes determine where coroutines run and who owns their lifetime.
Core Concepts¶
Dispatchers¶
Mainโ UI threadIOโ blocking I/O workDefaultโ CPU-heavy workUnconfinedโ special-case behavior, rarely appropriate in app code
Scopes¶
A CoroutineScope defines the lifecycle boundary for its coroutines.
Internal Implementation¶
Dispatchers are elements of coroutine context. Scopes combine context plus job hierarchy rules.
The important concept is that execution context and ownership travel together through coroutine context composition.
JVM / Compiler Behavior¶
Dispatchers do not change what coroutines are. They change scheduling and thread execution behavior.
withContext(...) is commonly used to switch execution context safely.
Code Examples¶
viewModelScope.launch {
val result = withContext(Dispatchers.IO) {
repository.fetch()
}
render(result)
}
Common Interview Questions¶
- Q: What is the difference between scope and dispatcher? A: Lead with correctness then throughput: choose dispatcher by workload type, keep critical sections small, cap parallelism, and monitor tail latency and queue depth.
- Q: Why is
Dispatchers.IOdifferent fromDefault? A: Lead with correctness then throughput: choose dispatcher by workload type, keep critical sections small, cap parallelism, and monitor tail latency and queue depth. - Q: Why is
GlobalScopediscouraged? A: Tie Kotlin language features to production outcomes: safety, readability, testability, and runtime or allocation tradeoffs when relevant.
Production Considerations¶
Use lifecycle-aware scopes on Android:
viewModelScopelifecycleScope
Avoid unowned coroutine launches unless there is a very explicit reason.
Performance Insights¶
Wrong dispatcher selection can cause:
- blocked UI
- thread starvation
- wasted parallelism
- poor responsiveness
Senior-Level Insights¶
Senior-level answers connect dispatcher choice to workload type and connect scope choice to ownership and cancellation boundaries.