Last active
February 18, 2022 22:05
-
-
Save chenzhang2006/f6bbeb0fe965151555149f8dda2d6ed7 to your computer and use it in GitHub Desktop.
Prove CoroutineContext is inherited and overwritten by Coroutine child
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun CoroutineScope.logWithName(msg: String) { // log message with CoroutineName | |
val coroutineName = coroutineContext[CoroutineName]?.name | |
println("[$coroutineName] $msg") | |
} | |
fun main() = runBlocking(CoroutineName("main")) { | |
logWithName("Started") // [main] Started | |
launch { | |
delay(300) | |
logWithName("finished") // [main] finished | |
} | |
launch(CoroutineName("launch-101")) { | |
delay(500) | |
logWithName("finished") // [launch-101] finished | |
} | |
val value = async(CoroutineName("async-202")) { | |
delay(1000) | |
logWithName("finished") // [async-202] finished | |
888 | |
} | |
logWithName("Got ${value.await()}") // [main] Got 888 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment