Coordinate cancellation in Kotlin coroutines

8 steps · about 4 h 50 min · Intermediate

  1. Launch work inside a structured scope (reference, kotlinx.coroutines API, 35 min)

    A scope makes child lifetime part of the surrounding operation.

  2. Choose execution context deliberately (reference, kotlinx.coroutines API, 35 min)

    Dispatchers separate CPU work, blocking I/O and inherited context.

  3. Make cancellation cooperative (guide, Kotlin documentation, 35 min)

    Suspension points and checks let unwanted work stop promptly.

  4. Predict exception propagation (reference, kotlinx.coroutines API, 35 min)

    Builders and parent-child relationships determine which failure reaches which owner.

  5. Compose sequential and concurrent operations (reference, kotlinx.coroutines API, 30 min)

    Async should represent intentional concurrency rather than become a default wrapper.

  6. Stream changing values with Flow (guide, Kotlin documentation, 40 min)

    Cold flows combine cancellable production with declarative transformations.

  7. Protect shared mutable state (reference, kotlinx.coroutines API, 40 min)

    Confinement, atomics and mutexes solve different contention patterns.

  8. Test virtual time and cancellation (reference, kotlinx.coroutines API, 40 min)

    A test scheduler makes delays, completion and cleanup deterministic.