Android中协程如何命名?
How to name coroutine in Android?
根据the documentation,我们可以使用CoroutineName("theName")
命名协程
如果我 运行 它没有名字(在单元测试中)
runBlocking {
launch {
println("main runBlocking pre : ${Thread.currentThread().name}:${coroutineContext[Job]}")
delay(500)
println("main runBlocking post : ${Thread.currentThread().name}:${coroutineContext[Job]}")
}
Thread.currentThread().name
和coroutineContext[Job]
都会给协程名分配一个ID,因此打印如下(注意coroutine#2
)
main runBlocking pre : main @coroutine#2:"coroutine#2":StandaloneCoroutine{Active}@39529185
main runBlocking post : main @coroutine#2:"coroutine#2":StandaloneCoroutine{Active}@39529185
如果我运行它的名字(在单元测试中)
runBlocking {
launch(CoroutineName("CustomName")) {
println("main runBlocking pre : ${Thread.currentThread().name}:${coroutineContext[Job]}")
delay(500)
println("main runBlocking post : ${Thread.currentThread().name}:${coroutineContext[Job]}")
}
它将打印(注意 @CustomName#2
)
main runBlocking pre : main @CustomName#2:"CustomName#2":StandaloneCoroutine{Active}@78e117e3
main runBlocking post : main @CustomName#2:"CustomName#2":StandaloneCoroutine{Active}@78e117e3
但是,如果我运行它在Android如下
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runBlocking {
launch(CoroutineName("CustomName")) { // context of the parent, main runBlocking coroutine
Log.d("Track", "main runBlocking pre : ${Thread.currentThread().name}:${coroutineContext[Job]}")
delay(500)
Log.d("Track", "main runBlocking post : ${Thread.currentThread().name}:${coroutineContext[Job]}")
}
}
}
它打印
Track: main runBlocking pre : main:StandaloneCoroutine{Active}@5e322c2
Track: main runBlocking post : main:StandaloneCoroutine{Active}@5e322c2
未给出协程名称,也未指定协程ID。我只能从地址 ID 识别,即 @5e322c2
,不能保证始终相同。
如何在Android中分配协程名称?
协程名称仅在调试模式下使用。但是,您可以在您的应用程序中启用:
System.setProperty("kotlinx.coroutines.debug", "on" )
根据the documentation,我们可以使用CoroutineName("theName")
如果我 运行 它没有名字(在单元测试中)
runBlocking {
launch {
println("main runBlocking pre : ${Thread.currentThread().name}:${coroutineContext[Job]}")
delay(500)
println("main runBlocking post : ${Thread.currentThread().name}:${coroutineContext[Job]}")
}
Thread.currentThread().name
和coroutineContext[Job]
都会给协程名分配一个ID,因此打印如下(注意coroutine#2
)
main runBlocking pre : main @coroutine#2:"coroutine#2":StandaloneCoroutine{Active}@39529185
main runBlocking post : main @coroutine#2:"coroutine#2":StandaloneCoroutine{Active}@39529185
如果我运行它的名字(在单元测试中)
runBlocking {
launch(CoroutineName("CustomName")) {
println("main runBlocking pre : ${Thread.currentThread().name}:${coroutineContext[Job]}")
delay(500)
println("main runBlocking post : ${Thread.currentThread().name}:${coroutineContext[Job]}")
}
它将打印(注意 @CustomName#2
)
main runBlocking pre : main @CustomName#2:"CustomName#2":StandaloneCoroutine{Active}@78e117e3
main runBlocking post : main @CustomName#2:"CustomName#2":StandaloneCoroutine{Active}@78e117e3
但是,如果我运行它在Android如下
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runBlocking {
launch(CoroutineName("CustomName")) { // context of the parent, main runBlocking coroutine
Log.d("Track", "main runBlocking pre : ${Thread.currentThread().name}:${coroutineContext[Job]}")
delay(500)
Log.d("Track", "main runBlocking post : ${Thread.currentThread().name}:${coroutineContext[Job]}")
}
}
}
它打印
Track: main runBlocking pre : main:StandaloneCoroutine{Active}@5e322c2
Track: main runBlocking post : main:StandaloneCoroutine{Active}@5e322c2
未给出协程名称,也未指定协程ID。我只能从地址 ID 识别,即 @5e322c2
,不能保证始终相同。
如何在Android中分配协程名称?
协程名称仅在调试模式下使用。但是,您可以在您的应用程序中启用:
System.setProperty("kotlinx.coroutines.debug", "on" )