Skip to content

Instantly share code, notes, and snippets.

@denis-zhdanov
Created February 21, 2021 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denis-zhdanov/faf19c16ba295247d72e5993d6d1ac00 to your computer and use it in GitHub Desktop.
Save denis-zhdanov/faf19c16ba295247d72e5993d6d1ac00 to your computer and use it in GitHub Desktop.
Coroutines sample 1
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