Created
February 21, 2021 18:22
-
-
Save denis-zhdanov/faf19c16ba295247d72e5993d6d1ac00 to your computer and use it in GitHub Desktop.
Coroutines sample 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val durationMs = measureTimeMillis { | |
val result = serve() | |
println("Got result $result") | |
} | |
println("The processing is done in $durationMs ms") | |
} | |
fun serve(): Collection<Int> { | |
return runBlocking { | |
listOf(async { getItemDouble(1) }, async { getItemTriple(2) }).map { it.await() } | |
} | |
} | |
suspend fun getItemDouble(i: Int): Int { | |
delay(1000) | |
return i * 2 | |
} | |
suspend fun getItemTriple(i: Int): Int { | |
delay(1000) | |
return i * 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment