如何获取使用 CoroutineName 字段启动的协程的名称

How to get the name of the Coroutine which was launched with CoroutineName field

我正在启动一个 Coroutine 字段 CoroutineName 像这样

fun main() {
    runBlocking {
        launch(CoroutineName("newName")) {
            println(Thread.currentThread().name)
        }
    }
}

当我打印当前线程名称时,我没有得到 main newName 作为名称,相反我只会得到 main 作为名称

只有开启调试模式才能看到协程名称。否则只显示线程名称。

您可以使用 JVM 选项打开调试 -Dkotlinx.coroutines.debug

如果您想了解更多详情,可以在 this link

上找到