Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prove CoroutineContext is inherited and overwritten by Coroutine child
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