Created
October 17, 2018 19:01
-
-
Save mayojava/6cce33d2fa011aa6a40e09562b8172ba to your computer and use it in GitHub Desktop.
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(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