Kotlin协程(翻译-未完成)
引言 Kotlin,作为一个语言,标准库中通过提供最小的底层API来给其他库更好的利用协程。不像其他语言,async 和 await 并不是Kotlin的关键字,甚至都不是标准库的一部分。此外,Kotlin的中断函数(suspending function)概念比 future 和 promise 提供了更安全,更不容易出错的异步操作。 协程基础 第一个协程 fun main(args: Array<String>) { launch(CommonPool) { // create new coroutine in common thread pool delay(1000L) // non-blocking delay for 1 second (default time unit is ms) println("World!") // print after delay } println("Hello,") // main function continues while coroutine is delayed Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive } 运行结果...