Describe coroutine cancellation mechanics and cooperative suspend functions

Tests cooperative cancellation mechanics. cancel() sets a Job to cancelling; suspend functions check this at suspension points and throw CancellationException, yet a tight non-suspending loop never checks and keeps running.
This tests Kotlin's cooperative cancellation model. cancel() transitions the Job to cancelling; standard suspend functions like delay check this at suspension points and throw CancellationException. A tight non-suspending loop ignores cancellation because it never reaches a suspension point, so you must check the Job active state or call a suspending function that does. CancellationException unwinds the stack for cleanup and must be rethrown if caught so structured concurrency propagates to children.
Read the original → kotlinlang.org
- #kotlin
- #coroutines
- #cancellation
- #structured-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.