【问题标题】:How to show "@coroutine#2","@coroutine#3","@coroutine#1" in kotlin coroutine?如何在 kotlin 协程中显示“@coroutine#2”、“@coroutine#3”、“@coroutine#1”?
【发布时间】:2020-09-05 08:51:07
【问题描述】:
import kotlinx.coroutines.*

fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")

fun main() = runBlocking<Unit> {
    val a = async {
        log("I'm computing a piece of the answer")
        6
    }
    val b = async {
        log("I'm computing another piece of the answer")
        7
    }
    log("The answer is ${a.await() * b.await()}")    
}

kotlin 的文档

[main @coroutine#2] 我正在计算一个答案 [main @coroutine#3] 我正在计算另一个答案 [main @coroutine#1] 答案是 42

我的 Intellij IDEA 和 Android IDE

[main] 我正在计算一部分答案 [main] 我正在计算 另一个答案 [main] 答案是 42

如何显示"@coroutine#2","@coroutine#3","@coroutine#1"????

【问题讨论】:

    标签: android-studio kotlin intellij-idea kotlin-coroutines


    【解决方案1】:

    打开运行/调试配置窗口并将-Dkotlinx.coroutines.debug添加到VM选项

    完成之后你会得到这个输出:

    [main @coroutine#2] I'm computing a piece of the answer
    [main @coroutine#3] I'm computing another piece of the answer
    [main @coroutine#1] : The answer is 42
    

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 2020-05-06
      • 2020-03-08
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 2021-03-28
      • 1970-01-01
      相关资源
      最近更新 更多