Advertisement
tukangbegal

Coroutine

Apr 5th, 2020
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.52 KB | None | 0 0
  1. import kotlinx.coroutines.*
  2.  
  3. // TODO 1
  4. suspend fun sum(valueA: Int, valueB: Int): Int {
  5.     delay(3000L)
  6.    return valueA+valueB
  7. }
  8.  
  9. // TODO 2
  10. suspend fun multiple(valueA: Int, valueB: Int): Int {
  11.     delay(2000L)
  12.     return valueA*valueB
  13. }
  14.  
  15. fun main() = runBlocking {
  16.     println("Counting...")
  17.  
  18.     val resultSum = async { sum(10, 10) }
  19.     val resultMultiple = async { multiple(20, 20) }
  20.  
  21.     // TODO 3
  22.     println("Result sum: ${resultSum.await()}")
  23.     println("Result multiple: ${resultMultiple.await()}")
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement