Skip to content

Instantly share code, notes, and snippets.

@mayojava
Created October 17, 2018 19:01
Show Gist options
  • Save mayojava/6cce33d2fa011aa6a40e09562b8172ba to your computer and use it in GitHub Desktop.
Save mayojava/6cce33d2fa011aa6a40e09562b8172ba to your computer and use it in GitHub Desktop.
fun main(args: Array<String>) = runBlocking {
val time = measureTimeMillis {
val first = async { firstNumber() }
val second = async { secondNumber() }
val third = async { thirdNumber() }
val result = first.await() + second.await() + third.await()
}
println(time) //prints 7 seconds
}
suspend fun firstNumber(): Int {
delay(3_000) // 3 seconds delay
return 5
}
suspend fun secondNumber(): Int {
delay(5_000) // 5 seconds delay
return 8
}
suspend fun thirdNumber(): Int {
delay(7_000) // 7 seconds delay
return 10
}
//the above code prints out 7 seconds, which is the time it took the longest running function to return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment