Guest User

Untitled

a guest
Oct 17th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. fun main(args: Array<String>) = runBlocking {
  2. val time = measureTimeMillis {
  3. val first = async { firstNumber() }
  4. val second = async { secondNumber() }
  5. val third = async { thirdNumber() }
  6.  
  7. val result = first.await() + second.await() + third.await()
  8. }
  9.  
  10. println(time) //prints 7 seconds
  11. }
  12. suspend fun firstNumber(): Int {
  13. delay(3_000) // 3 seconds delay
  14. return 5
  15. }
  16. suspend fun secondNumber(): Int {
  17. delay(5_000) // 5 seconds delay
  18. return 8
  19. }
  20. suspend fun thirdNumber(): Int {
  21. delay(7_000) // 7 seconds delay
  22. return 10
  23. }
  24. //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