我不确定yield
函数的目的是什么.你能看看我举的这个例子吗?
我正在遵循一个here的例子.
以下是代码:
val job = launch {
val child = launch {
try {
delay(Long.MAX_VALUE)
} finally {
println("Child is cancelled")
}
}
yield() //why do i need this ???????
println("取消子项")
child.cancel()
child.join()
yield()
println("家长没有被取消")
}
job.join()
When I comment out the first yield I get the following results:
-
取消子项
家长没有被取消
但如果我保持yield 率不变:
-
取消子项
Child is cancelled
家长没有被取消
What does it mean to use yield
here?