如果顺序执行,调起两个挂起函数,执行这两个挂起函数的总是时间等于分别执行这个两个函数的总和,如示例(1)
示例(1):
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = doSomethingUsefulOne()
val two = doSomethingUsefulTwo()
println("The result is ${one + two}")
}
println("Completed in $time ms")
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L)
return 10
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L)
return 33
}
示例(1)执行结果:
The result is 43
Completed in 2046 ms
async函数类似于 launch函数.它启动了一个单独的协程,这是一个轻量级的线程并与其它所有的协程一起并发的工作.不同之处在于 launch 返回一个 Job 并且不附带任何结果值,而 async 返回一个 Deferred —— 一个轻量级的非阻塞 future,这代表了一个将会在稍后提供结果的 promise.你可以使用 .await() 在一个延期的值上得到它的最终结果, 但是 Deferred 也是一个 Job,所以如果需要的话,你可以取消它.如示例(2)
示例(2):
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = async { doSomethingUsefulOne() }
val two = async { doSomethingUsefulTwo() }
val result1 = one.await()
val result2 = two.await()
println("The result is ${result1 + result2}")
}
println("Completed in $time ms")
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L)
return 10
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L)
return 33
}
示例(2)的执行结果:
The result is 43
Completed in 1032 ms
通过示例(1)和示例(2)的执行结果,我们可以看出,示例(2)的两个挂起函数是并发执行的,即:async函数是新起一个协程,并发执行的
async 可以通过将 start 参数设置为 CoroutineStart.LAZY 而变为惰性的.在这种情况下,只有结果通过 await 获取的时候协程才会启动,或者在 Job 的 start 函数调用的时候,如示例(3)
示例(3):
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
one.start()
two.start()
val result1 = one.await()
val result2 = two.await()
println("The result is ${result1 + result2}")
}
println("Completed in $time ms")
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L)
return 9
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L)
return 33
}
示例(3)的执行结果:
The result is 42
Completed in 1026 ms
在示例(3)中,如果没有执行start,默认执行await方法,则one 和 two是顺序执行的,one执行完之后才执行two,如示例(4)
示例(4):
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
val result1 = one.await()
val result2 = two.await()
println("The result is ${result1 + result2}")
}
println("Completed in $time ms")
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L)
return 9
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L)
return 33
}
示例(4)的执行结果:
The result is 42
Completed in 2026 ms
网友评论