簡體   English   中英

如何在 kotlin 協程中顯示“@coroutine#2”、“@coroutine#3”、“@coroutine#1”?

[英]How to show “@coroutine#2”,“@coroutine#3”,“@coroutine#1” in kotlin coroutine?

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”????

打開運行/調試配置窗口並將-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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM