Kotlin & Coroutines (50 Qs)•Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)
Coroutine Dispatchers and Context: `Dispatchers.Main`, `Dispatchers.IO`, and `Dispatchers.Default`.
senior LevelPerformance
In Simple Words:
Dispatchers.Main: Confined to the UI thread (Android Main thread or JavaFX). Essential for UI mutations.Dispatchers.IO: Designed for blocking I/O (files, networking, database queries). Backed by an elastic thread pool that scales up to 64 threads (or core count).Dispatchers.Default: Designed for CPU-intensive computational work (sorting massive lists, JSON parsing). Thread pool size is strictly capped at the number of available CPU cores.withContext(Dispatchers.IO) { ... }: Safely switches execution context and returns result to caller without callback nesting.