How does coroutine cancellation work internally?

This tests your grasp of cooperative cancellation. Explain that `cancel()` sets a flag, causing a `CancellationException` at the next suspension point. A non-suspending loop must explicitly check `isActive` to stop.
This tests your deep understanding of cooperative cancellation. A great answer explains that `job.cancel()` sets a 'cancelling' flag and propagates to children. The coroutine only stops when it hits a suspension point (like `delay`) which throws `CancellationException`. A long, non-suspending loop will ignore cancellation unless it manually checks the `isActive` property. A major red flag is stating that `cancel()` immediately stops the underlying thread.
Read the original → kotlinlang.org
- #kotlin
- #coroutines
- #concurrency
- #android
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.