kotlin文档经常有用到Thread.currentThread().name,打印当前的线程和协程,但是自己测试只能看到线程信息。
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
fun simple(): Flow<Int> = flow {
log("Started simple flow")
for (i in 1..3) {
emit(i)
}
}
fun main() = runBlocking {
simple().collect { value -> log("Collected $value") }
}
输出:
[main] Started simple flow
[main] Collected 1
[main] Collected 2
[main] Collected 3
需要在as的VM options中配置-Dkotlinx.coroutines.debug才能看到
image.png image.png
配置后输出:
[main @coroutine#1] Started simple flow
[main @coroutine#1] Collected 1
[main @coroutine#1] Collected 2
[main @coroutine#1] Collected 3
网友评论