June 2018
Intermediate to advanced
310 pages
6h 32m
English
To understand different contexts, let's look at the following code:
val r1 = async(Unconfined) { for (i in 1..1000) { println(Thread.currentThread().name) delay(1) }}r1.await()
Instead of yield(), we're using the delay() function, which also suspends the current coroutine.
But the output compared to yield() is different:
mainkotlinx.coroutines.DefaultExecutor...
After calling delay() for the first time, the coroutine has switched context, and as a result, threads.
For that reason, using Unconfined is not recommended for CPU-intensive tasks or tasks that need to run on a particular thread, such as UI rendering.
You can also create your own thread pool for coroutines to run on:
val pool = newFixedThreadPoolContext(2, ...Read now
Unlock full access