Kotlin & Coroutines (50 Qs)•Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)
Structured Concurrency & Exception Handling: `Job` vs `SupervisorJob` and cancellation propagation.
senior LevelPerformance
In Simple Words:
- Standard
Job: Follows strict cascading cancellation. If child coroutine A throws an unhandled exception, it immediately cancels its parent, which in turn cancels all sibling coroutines! SupervisorJob: Isolates child failures. If child A crashes, child B and the parent continue executing unaffected.- Essential for Android ViewModels (
viewModelScopeusesSupervisorJob) so one failed network request does not crash the entire screen.
- Essential for Android ViewModels (
- Cooperative Cancellation: Coroutines must cooperate with cancellation by periodically checking
isActiveor calling cancellable functions (delay,yield).