测试代码时,为什么会出现错误“未解析的引用:启动”?

时间:2020-03-18 00:56:47

标签: kotlin kotlin-coroutines

我正在学习Kotlin的协程,我是https://try.kotlinlang.org/上在线运行代码的初学者

我尝试在网站上测试代码A,但是像图像A一样,我遇到很多错误,该如何解决?

代码A

:buffers!

图片A enter image description here

1 个答案:

答案 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