Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment