Skip to content

Commit ec524b3

Browse files
committed
rename class name of CoroutineThreadLocalAsContextElement Test/Demo
1 parent 1755021 commit ec524b3

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/test/java/com/alibaba/demo/coroutine/CoroutineThreadLocalAsContextElementDemo.kt renamed to src/test/java/com/alibaba/demo/coroutine/CoroutineThreadContextElementDemo.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ private val threadLocal = ThreadLocal<String?>() // declare thread-local variabl
55
/**
66
* [Thread-local data - Coroutine Context and Dispatchers - Kotlin Programming Language](https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html#thread-local-data)
77
*/
8-
fun main() = runBlocking<Unit> {
8+
fun main() = runBlocking {
99
threadLocal.set("main")
1010
println("Pre-main, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
1111

src/test/java/com/alibaba/demo/coroutine/CoroutineThreadLocalAsContextElementTest.kt renamed to src/test/java/com/alibaba/demo/coroutine/CoroutineThreadContextElementTest.kt

+43-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import kotlinx.coroutines.*
44
import org.junit.Assert.*
55
import org.junit.Test
66

7-
class CoroutineThreadLocalAsContextElementTest {
7+
class CoroutineThreadContextElementTest {
88
@Test
9-
fun oneThreadContextElement(): Unit = runBlocking {
9+
fun threadContextElement_passByValue(): Unit = runBlocking {
1010
val mainValue = "main-${System.currentTimeMillis()}"
1111
val launchValue = "launch-${System.currentTimeMillis()}"
1212
val testThread = Thread.currentThread()
1313

14-
val threadLocal = ThreadLocal<String?>() // declare thread-local variable
14+
// String ThreadLocal, String is immutable value, can only be passed by value
15+
val threadLocal = ThreadLocal<String?>()
1516
threadLocal.set(mainValue)
1617
println("test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
1718

@@ -44,6 +45,45 @@ class CoroutineThreadLocalAsContextElementTest {
4445
assertEquals(mainValue, threadLocal.get())
4546
}
4647

48+
@Test
49+
fun threadContextElement_passByReference(): Unit = runBlocking {
50+
data class Reference(var data: Int = 42)
51+
52+
val mainValue = Reference()
53+
val launchValue = Reference(4242)
54+
val testThread = Thread.currentThread()
55+
56+
// Reference ThreadLocal, mutable value, pass by reference
57+
val threadLocal = ThreadLocal<Reference>() // declare thread-local variable
58+
threadLocal.set(mainValue)
59+
println("test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
60+
61+
val job = launch(Dispatchers.Default + threadLocal.asContextElement(value = launchValue)) {
62+
println("Launch start, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
63+
assertEquals(launchValue, threadLocal.get())
64+
assertNotEquals(testThread, Thread.currentThread())
65+
66+
delay(5)
67+
68+
println("After delay, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
69+
assertEquals(launchValue, threadLocal.get())
70+
assertNotEquals(testThread, Thread.currentThread())
71+
72+
val reset = -42
73+
threadLocal.get().data = reset
74+
75+
delay(5)
76+
77+
println("After delay set reset, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
78+
assertEquals(Reference(reset), threadLocal.get())
79+
assertNotEquals(testThread, Thread.currentThread())
80+
}
81+
job.join()
82+
83+
println("after launch, test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}")
84+
assertEquals(mainValue, threadLocal.get())
85+
}
86+
4787
@Test
4888
fun twoThreadContextElement(): Unit = runBlocking {
4989
val mainValue = "main-a-${System.currentTimeMillis()}"

src/test/java/com/alibaba/demo/coroutine/CoroutineContextDemo.kt renamed to src/test/java/com/alibaba/demo/coroutine/CoroutineThreadLocalContextContinuationInterceptorDemo.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fun main(): Unit = runBlocking {
2424
println("Hello ${await()}!")
2525
}
2626

27-
async(MyContext(myThreadLocal.get(), Dispatchers.IO)) {
27+
async(MyThreadLocalContextContinuationInterceptor(myThreadLocal.get(), Dispatchers.IO)) {
2828
"world(${myThreadLocal.get().data})"
2929
}.run {
3030
println("Hello ${await()}!")
@@ -37,7 +37,7 @@ private val myThreadLocal = object : ThreadLocal<MyData>() {
3737
}
3838
}
3939

40-
private class MyContext(
40+
private class MyThreadLocalContextContinuationInterceptor(
4141
private var myData: MyData,
4242
private val dispatcher: ContinuationInterceptor
4343
) : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {

0 commit comments

Comments
 (0)