Coroutine Context and Threads

The call to the launch() and runBlocking() functions resulted in the coroutines executing in the same thread as the caller’s coroutine scope. That’s the default behavior of these functions, since they carry a coroutine context from their scope. You may, however, vary the context and the thread of execution of the coroutines where you like.

Explicitly Setting a Context

You may pass a CoroutineContext to the launch() and runBlocking() functions to set the execution context of the coroutines these functions start.

The value of Dispatchers.Default for the argument of type CoroutineContext instructs the coroutine that is started to execute in a thread from a DefaultDispatcher pool. The number of threads in this pool ...

Get Programming Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.