Coroutine context and Dispatchers
Coroutines always execute in some context represented by a value of the CoroutineContext type.
Dispatchers and threads
The coroutine context includes a coroutine dispatcher that determines what thread or threads the corresponding coroutine uses for its execution. The coroutine dispatcher can confine coroutine execution to a specific thread, dispatch it to a thread pool, or let it run unconfined.
All coroutine builders like launch and async accept an optional CoroutineContext parameter that can be used to explicitly specify the dispatcher for the new coroutine and other context elements.
It produces the following output:
Whenlaunch { ... } is used without parameters, it inherits the context (and thus dispatcher) from the CoroutineScope it is being launched from. In this case, it inherits the context of the main runBlocking coroutine which runs in the main thread.
Dispatchers.Unconfined is a special dispatcher that also appears to run in the main thread, but it is, in fact, a different mechanism that is explained later.
The default dispatcher is used when no other dispatcher is explicitly specified in the scope. It is represented by Dispatchers.Default and uses a shared background pool of threads.
newSingleThreadContext creates a thread for the coroutine to run. A dedicated thread is a very expensive resource. In a real application it must be either released, when no longer needed, using the close function, or stored in a top-level variable and reused throughout the application.
Unconfined vs confined dispatcher
The Dispatchers.Unconfined coroutine dispatcher starts a coroutine in the caller thread, but only until the first suspension point. After suspension it resumes the coroutine in the thread that is fully determined by the suspending function that was invoked.
The unconfined dispatcher is appropriate for coroutines which neither consume CPU time nor update any shared data (like UI) confined to a specific thread.
On the other side, the dispatcher is inherited from the outer CoroutineScope by default. The default dispatcher for the runBlocking coroutine is confined to the invoker thread, so inheriting it has the effect of confining execution to this thread with predictable FIFO scheduling.
It produces the following output:
So, the coroutine with the context inherited from runBlocking {...} continues to execute in the main thread, while the unconfined one resumes in the default executor thread that the delay function is using.
Jumping between threads
It demonstrates several new techniques. One is using runBlocking with an explicitly specified context, and the other one is using the withContext function to change the context of a coroutine while still staying in the same coroutine, as you can see in the output below:
It produces the following output:
Note that this example also uses the use function from the Kotlin standard library to release threads created with newSingleThreadContext when they are no longer needed.
Job in the context
The coroutine's Job is part of its context, and can be retrieved from it using the coroutineContext[Job] expression:
Note that isActive in CoroutineScope is just a convenient shortcut for coroutineContext[Job]?.isActive == true.
Children of a coroutine
When a coroutine is launched in the CoroutineScope of another coroutine, it inherits its context viaCoroutineScope.coroutineContext and the Job of the new coroutine becomes a child of the parent coroutine's job.
When the parent coroutine is cancelled, all its children are recursively cancelled, too.
However, this parent-child relation can be explicitly overriden in one of two ways:
When a different scope is explicitly specified when launching a coroutine (for example,
GlobalScope.launch), then it does not inherit aJobfrom the parent scope.When a different
Jobobject is passed as the context for the new coroutine (as shown in the example below), then it overrides theJobof the parent scope.
In both cases, the launched coroutine is not tied to the scope it was launched from and operates independently.
It produces the following output:
Parental responsibilities
A parent coroutine always waits for completion of all its children.
A parent does not have to explicitly track all the children it launches, and it does not have to use Job.join to wait for them at the end:
It produces the following output:
Other Scenario 1
It produces the following output:
Naming coroutines for debugging
When multiple coroutines are launched in the same context, it is useful to name them for debugging purposes.
The CoroutineName context element serves the same purpose as the thread name. It is included in the thread name that is executing this coroutine when the debugging mode is turned on.
Combining context elements
Sometimes we need to define multiple elements for a coroutine context. We can use the + operator for that.
For example, we can launch a coroutine with an explicitly specified dispatcher and an explicitly specified name at the same time:
Coroutine scope
Assume that our application has an object with a lifecycle, but that object is not a coroutine.
For example, we are writing an Android application and launch various coroutines in the context of an Android activity to perform asynchronous operations to fetch and update data, do animations, etc. All of these coroutines must be cancelled when the activity is destroyed to avoid memory leaks.
We, of course, can manipulate contexts and jobs manually to tie the lifecycles of the activity and its coroutines, but kotlinx.coroutines provides an abstraction encapsulating that: CoroutineScope.
We manage the lifecycles of our coroutines by creating an instance of CoroutineScope tied to the lifecycle of our activity.
CoroutineScope(): This creates a general-purpose scopeMainScope(): This creates a scope for UI applications and uses Dispatchers.Main as the default dispatcher
Now, we can launch coroutines in the scope of this Activity using the defined mainScope. For the demo, we launch ten coroutines that delay for a different time:
In our main function we create the activity, call our test doSomething function, and destroy the activity after 500ms. This cancels all the coroutines that were launched from doSomething. We can see that because after the destruction of the activity no more messages are printed, even if we wait a little longer.
It produces the following output:
As you can see, only the first two coroutines print a message and the others are cancelled by a single invocation of job.cancel() in Activity.destroy().