June 2018
Intermediate to advanced
310 pages
6h 32m
English
This is another concept we discussed briefly in Chapter 8, Threads and Coroutines, in the Starting a coroutine section.
Remember how our launch() or async() could receive CommonPool?
Here's an example to remind you that you could specify it explicitly:
// Same as launch {}launch(CommonPool) {...}// Same as async {}val result = async(CommonPool) {...}
This CommonPool is a Scheduler design pattern in a bad disguise. Many async tasks may be mapped to the same Scheduler.
Run the following code:
val r1 = async(CommonPool) { for (i in 1..1000) { println(Thread.currentThread().name) yield() }}r1.await()
What is interesting is the fact that the same coroutine is picked up by different threads:
ForkJoinPool.commonPool-worker-2ForkJoinPool.commonPool-worker-3 ...
Read now
Unlock full access