我正在学习Kotlin的协程,我是https://try.kotlinlang.org/上在线运行代码的初学者
我尝试在网站上测试代码A,但是像图像A一样,我遇到很多错误,该如何解决?
代码A
:buffers!
答案 0 :(得分:2)
尝试运行以下代码:
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
fun main() = runBlocking<Unit> { //here i made change
val job = launch {
val child = launch {
try {
delay(Long.MAX_VALUE)
} finally {
println("Child is cancelled")
}
}
yield()
println("Cancelling child")
child.cancel()
child.join()
yield()
println("Parent is not cancelled")
}
job.join()
}
输出为:)
Cancelling child
Child is cancelled
Parent is not cancelled