CoRoutine Highlighted

  • CoRoutine is like a light weight thread, that is running in application layer (not in operating system layer).
  • A coroutine can suspend on one thread and resume on a different thread
    • At any given moment, a coroutine runs on exactly one thread.
    • The dispatcher determines which thread(s) a coroutine uses.
      • Multiple coroutines can run on the same thread (one at a time though).
  • By default one co-routine throwing (non CancellationException) uncaught exception will cancel job hierarchy, meaning:
    • Cancel parent (recursively). Parent is determined through job hierarchy.
    • Cancel sibling co-routine (parent will cancel all it's children recursively).
    • Rethrow original exception to the parent.
  • Uncaught exception on async will auto cancel siblings right away. (example)
  • When you catch exceptions see if they are CancellationException if they are DON'T keep going.
  • Cooperative Cancellation - coroutines must check for cancellation at suspension points, and respect it.

Backlinks