🐦 Kotlin
Intermediate
What is structured concurrency in Kotlin?
Answer
Structured concurrency is the principle that coroutines are scoped to a specific lifetime. Every coroutine belongs to a CoroutineScope, and a parent scope waits for all its child coroutines to complete before it itself completes. If a parent scope is cancelled, all child coroutines are automatically cancelled. If a child fails with an uncaught exception, it propagates to the parent and cancels siblings. This prevents coroutine leaks (coroutines that keep running after their associated UI is destroyed) and makes async code behave predictably. Android's viewModelScope and lifecycleScope implement this principle.