Skip to content

Commit bd3a4c2

Browse files
committed
improve CoroutineThreadContextElementDemo
1 parent ec524b3 commit bd3a4c2

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/test/java/com/alibaba/demo/coroutine/CoroutineThreadContextElementDemo.kt

+14-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ fun main() = runBlocking {
99
threadLocal.set("main")
1010
println("Pre-main, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
1111

12-
val job = launch(Dispatchers.Default + threadLocal.asContextElement(value = "launch")) {
12+
val block: suspend CoroutineScope.() -> Unit = {
1313
println("Launch start, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
1414
yield()
1515
println("After yield, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
1616
}
17-
job.join()
1817

18+
println()
19+
launch(block = block).join()
20+
21+
println()
22+
launch(threadLocal.asContextElement(value = "launch"), block = block).join()
23+
24+
println()
25+
launch(Dispatchers.Default, block = block).join()
26+
27+
println()
28+
launch(Dispatchers.Default + threadLocal.asContextElement(value = "launch"), block = block).join()
29+
30+
println()
1931
println("Post-main, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
2032
}

0 commit comments

Comments
 (0)