I'm learning Coroutines of Kotlin. The Code A will get the Result A.
I know "async is used to start a coroutine that computes some result. The result is represented by an instance of Deferred.".
So val one will return Deferred, but what return will one.await()? It seem that one.await() will return Int, right?
Code A
import kotlinx.coroutines.*
import kotlin.system.*
fun main() = runBlocking {
val time = measureTimeMillis {
val one = async { doJob1() }
val two = async { doJob2() }
val a1=one.await()
val a2=two.await()
println("[testSequential] Result ${a1+a2}")
}
println("[testSequential] Completed in $time ms")
}
suspend fun doJob1(): Int {
println("Job1 doing")
delay(2000L)
println("Job1 done")
return 10
}
suspend fun doJob2(): Int {
println("Job2 doing")
delay(1000L)
println("Job2 done")
return 20
}
Result A
Job1 doing
Job2 doing
Job2 done
Job1 done
[testSequential] Result 30
[testSequential] Completed in 2016 ms