简体   繁体   中英

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()}")    
}

Document of kotlin

[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

My IDE of Intellij IDEA and Android

[main] I'm computing a piece of the answer [main] I'm computing another piece of the answer [main] The answer is 42

How to show "@coroutine#2","@coroutine#3","@coroutine#1"????

Open Run/Debug Configurations window and add -Dkotlinx.coroutines.debug to the VM options :

在此处输入图片说明

After doing that you'll get this output:

[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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM