June 2018
Intermediate to advanced
310 pages
6h 32m
English
We'll call both the greedyLongCoroutine() and shortCoroutine() methods 10 times each and wait until they finish:
val latch = CountDownLatch(10 * 2)fun main(args: Array<String>) { for (i in 1..10) { CoroutineFactory.greedyLongCoroutine(i) } for (i in 1..10) { CoroutineFactory.shortCoroutine(i) } latch.await(10, TimeUnit.SECONDS)}
It's obvious that since coroutines are asynchronous, we'll see first 10 lines of the short coroutine then 10 lines of the long coroutine:
Done greedyLongCoroutine 2Done greedyLongCoroutine 4Done greedyLongCoroutine 3Done greedyLongCoroutine 5Done shortCoroutine 1! <= You should have finished long ago!Done shortCoroutine 2!Done shortCoroutine 3!Done shortCoroutine 4!Done shortCoroutine 5!Done shortCoroutine ...
Read now
Unlock full access