一个协程作业的延迟会影响另一个吗?
Does a delay in one coroutine job affect another?
根据文档,延迟会暂停协程但不会阻塞线程:
Delay is a special suspending function. It suspends the coroutine for a specific time. Suspending a coroutine does not block the underlying thread, but allows other coroutines to run and use the underlying thread for their code.
但是当运行这段代码时:
fun main() {
println("1")
GlobalScope.launch {
doSomething()
delay(1000L)
println("3")
}
GlobalScope.launch {
delay(1000L)
println("4")
}
println("5")
}
suspend fun doSomething() {
delay(500L)
println("2")
delay(1000L)
}
给出的输出是:
1
5
2
4
3
按照我的理解应该是1 5 4 2 3
。
这是因为 doSomething()
在第一个协程中被调用,因此与第二个协程相比延迟更多,并且第二个协程被异步调用。我的误会在哪里?
还有这段代码,两个值在 2 秒后打印在一起:
fun main() {
GlobalScope.launch {
val value1 = doSomething1()
val value2 = doSomething2()
println(value1)
println(value2)
}
}
suspend fun doSomething1(): String {
delay(1000L)
return "This is the answer1"
}
suspend fun doSomething2(): String {
delay(1000L)
return "This is the answer2"
}
这是为什么?
so the delay is more as compared to the second coroutine
不是,第一个延迟是:500L,
第二个延迟是:1000L
TL;DR
不会,不同工作的延迟不会相互影响。
对于你的第一个问题,显然 1 和 5 是第一个和最后一个,所以我们可以忽略它们。所以对于其余的我们有:
Print
Delays
Total delay
2
500
500
3
500, 1000, 1000
2500
4
1000
1000
所以顺序是 2、4、3,您已经用输出验证了这一点。
如果您想更多地了解协程是如何 运行ning 的,我们可以大致考虑每个作业在每个时间步发生的情况:
Time
Job 1
Job 2
0
delay(500L)
delay(1000L)
500
print("2"); delay(1000L)
-
1000
-
print("4")
1500
delay(1000L)
-
2000
-
-
2500
print("3")
-
要解决你的第二个问题,你可以把它写成一个代码块:
GlobalScope.launch {
delay(1000L)
val value1 = "This is the answer1"
delay(1000L)
val value2 = "This is the answer2"
println(value1)
println(value2)
}
很明显,打印语句会 运行 一个接一个。仅仅因为获取值有延迟,并不意味着打印它们之间有延迟。如果 print 语句在两个 doSomething
方法中,我认为您期望的行为将会发生。
根据文档,延迟会暂停协程但不会阻塞线程:
Delay is a special suspending function. It suspends the coroutine for a specific time. Suspending a coroutine does not block the underlying thread, but allows other coroutines to run and use the underlying thread for their code.
但是当运行这段代码时:
fun main() {
println("1")
GlobalScope.launch {
doSomething()
delay(1000L)
println("3")
}
GlobalScope.launch {
delay(1000L)
println("4")
}
println("5")
}
suspend fun doSomething() {
delay(500L)
println("2")
delay(1000L)
}
给出的输出是:
1
5
2
4
3
按照我的理解应该是1 5 4 2 3
。
这是因为 doSomething()
在第一个协程中被调用,因此与第二个协程相比延迟更多,并且第二个协程被异步调用。我的误会在哪里?
还有这段代码,两个值在 2 秒后打印在一起:
fun main() {
GlobalScope.launch {
val value1 = doSomething1()
val value2 = doSomething2()
println(value1)
println(value2)
}
}
suspend fun doSomething1(): String {
delay(1000L)
return "This is the answer1"
}
suspend fun doSomething2(): String {
delay(1000L)
return "This is the answer2"
}
这是为什么?
so the delay is more as compared to the second coroutine
不是,第一个延迟是:500L, 第二个延迟是:1000L
TL;DR
不会,不同工作的延迟不会相互影响。
对于你的第一个问题,显然 1 和 5 是第一个和最后一个,所以我们可以忽略它们。所以对于其余的我们有:
Delays | Total delay | |
---|---|---|
2 | 500 | 500 |
3 | 500, 1000, 1000 | 2500 |
4 | 1000 | 1000 |
所以顺序是 2、4、3,您已经用输出验证了这一点。
如果您想更多地了解协程是如何 运行ning 的,我们可以大致考虑每个作业在每个时间步发生的情况:
Time | Job 1 | Job 2 |
---|---|---|
0 | delay(500L) |
delay(1000L) |
500 | print("2"); delay(1000L) |
- |
1000 | - | print("4") |
1500 | delay(1000L) |
- |
2000 | - | - |
2500 | print("3") |
- |
要解决你的第二个问题,你可以把它写成一个代码块:
GlobalScope.launch {
delay(1000L)
val value1 = "This is the answer1"
delay(1000L)
val value2 = "This is the answer2"
println(value1)
println(value2)
}
很明显,打印语句会 运行 一个接一个。仅仅因为获取值有延迟,并不意味着打印它们之间有延迟。如果 print 语句在两个 doSomething
方法中,我认为您期望的行为将会发生。