Roman Elizarov | b65dc7f | 2018-12-22 18:40:39 +0300 | [diff] [blame] | 1 | import kotlinx.coroutines.* |
| 2 | import kotlinx.coroutines.debug.* |
| 3 | |
| 4 | suspend fun computeValue(): String = coroutineScope { |
| 5 | val one = async { computeOne() } |
| 6 | val two = async { computeTwo() } |
| 7 | combineResults(one, two) |
| 8 | } |
| 9 | |
| 10 | suspend fun combineResults(one: Deferred<String>, two: Deferred<String>): String = |
| 11 | one.await() + two.await() |
| 12 | |
| 13 | suspend fun computeOne(): String { |
| 14 | delay(5000) |
| 15 | return "4" |
| 16 | } |
| 17 | |
| 18 | suspend fun computeTwo(): String { |
| 19 | delay(5000) |
| 20 | return "2" |
| 21 | } |
| 22 | |
| 23 | fun main() = runBlocking { |
| 24 | DebugProbes.install() |
| 25 | val deferred = async { computeValue() } |
| 26 | // Delay for some time |
| 27 | delay(1000) |
| 28 | // Dump running coroutines |
| 29 | DebugProbes.dumpCoroutines() |
| 30 | println("\nDumping only deferred") |
| 31 | DebugProbes.printJob(deferred) |
| 32 | } |