Recap
This is a good time to recap how thread switching works. Now that we have all the parts explained, we can just put it in a few sentences.
The initial Continuation gets wrapped into a DispatchedContinuation; this is still a Continuation, but can dispatch using a CoroutineDispatcher if needed—which should be the case, except for Unconfined. The CoroutineDispatcher will use whichever executor fits its requirements, sending it a DispatchedTask, which is a Runnable that sets the correct context using withCoroutineContext() and invokes the resume() and resumeWithException() functions from the DispatchedContinuation.
So, the actual work of changing the thread happens in the CoroutineDispatcher, but is only possible thanks to the whole pipeline ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access