当主线程退出时协程会发生什么?

What happen with coroutines when main thread exits?

在 Java 中,当主线程退出时,所有用户线程(非守护线程)将保持 运行ning 直到完成他们的工作。

我有一个简单的程序,可以将计数器从 1 到 5 打印到控制台。

Java版本:

fun main(args: Array<String>) {
    println("Main start")

    countWithThread()

    println("Main end")
}

fun countWithThread() {
    Thread(Runnable {
        for (i in 1..5) {
            println("${Thread.currentThread().name} count $i")
            Thread.sleep(10)
        }
    }).start()
}

输出:

Main start
Main end
Thread-0 count 1
Thread-0 count 2
Thread-0 count 3
Thread-0 count 4
Thread-0 count 5

Process finished with exit code 0

Kotlin 版本:

fun main(args: Array<String>) {
    println("Main start")

    countWithCoroutine()

    println("Main end")
}

fun countWithCoroutine() {
    launch(CommonPool) {
        for (i in 1..5) {
            println("${Thread.currentThread().name} count $i")
            delay(10)
        }
    }
}

输出:

Main start
Main end

Process finished with exit code 0

如你所见,当主线程退出时,协程中的代码不再运行。似乎 Kotlin 终止了引擎盖下的所有协程。

谁能告诉我当主线程退出时协程到底发生了什么?

协程在主线程完成执行并且 process/JVM 实例死亡时终止,它们就像守护线程。参考官方协程指南this section

协程本身并不是 "running" JVM 知道的方式。它们只不过是堆上的对象。

但是,协程上下文 确实对何时允许 JVM 终止有发言权。创建您自己的:

val threadPool = Executors.newFixedThreadPool(4)
val dispatcher = threadPool.asCoroutineDispatcher()

现在如果你用它代替 CommonPool:

launch(dispatcher) { ... }

您会发现 JVM 根本不会死,即使所有任务都已完成。只有当你明确地说

时它才会退出
threadPool.shutdown()

但是请注意,executor.shutdown() 对协同程序的行为与对您提交给它的 "classic" 任务的行为不同。执行器将确保所有提交的任务在关闭前完成,但它没有考虑挂起的协程。