提交 bd3a4c2d 编写于 作者: oldratlee's avatar oldratlee 🔥

improve CoroutineThreadContextElementDemo

上级 ec524b34
......@@ -9,12 +9,24 @@ fun main() = runBlocking {
threadLocal.set("main")
println("Pre-main, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
val job = launch(Dispatchers.Default + threadLocal.asContextElement(value = "launch")) {
val block: suspend CoroutineScope.() -> Unit = {
println("Launch start, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
yield()
println("After yield, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
}
job.join()
println()
launch(block = block).join()
println()
launch(threadLocal.asContextElement(value = "launch"), block = block).join()
println()
launch(Dispatchers.Default, block = block).join()
println()
launch(Dispatchers.Default + threadLocal.asContextElement(value = "launch"), block = block).join()
println()
println("Post-main, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册