forked from alibaba/transmittable-thread-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgentDemo.kt
65 lines (50 loc) · 2.35 KB
/
AgentDemo.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@file:JvmName("AgentDemo")
package com.alibaba.demo.ttl.agent
import com.alibaba.ttl.TransmittableThreadLocal
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
fun main() {
val executorService = Executors.newFixedThreadPool(3)
expandThreadPool(executorService)
stringTransmittableThreadLocal.set("foo - main")
personReferenceTransmittableThreadLocal.set(Person("jerry - reference", 1))
personCopyTransmittableThreadLocal.set(Person("Tom - value", 2))
printTtlInstancesInfo("Main - Before execution of thread pool")
val submit = executorService.submit {
printTtlInstancesInfo("Thread Pool - enter")
stringTransmittableThreadLocal.set("foo - modified in thread pool")
personReferenceTransmittableThreadLocal.get().name = "jerry - reference - modified in thread pool"
personCopyTransmittableThreadLocal.get().name = "Tom - value - modified in thread pool"
printTtlInstancesInfo("Thread Pool - leave")
}
submit.get()
printTtlInstancesInfo("Main - After execution of thread pool")
executorService.shutdown()
}
private data class Person(var name: String = "unnamed", var age: Int = -1)
private val stringTransmittableThreadLocal = TransmittableThreadLocal<String>()
private val personReferenceTransmittableThreadLocal = object : TransmittableThreadLocal<Person>() {
override fun initialValue(): Person = Person()
}
private val personCopyTransmittableThreadLocal = object : TransmittableThreadLocal<Person>() {
override fun initialValue(): Person = Person()
override fun copy(parentValue: Person): Person = parentValue.copy() // copy value to child thread
}
private fun expandThreadPool(executor: ExecutorService) {
(0 until 3).map {
executor.submit { Thread.sleep(100) }
}.forEach {
it.get()
}
}
private fun printTtlInstancesInfo(msg: String) {
println("====================================================")
println(msg)
println("====================================================")
println("stringTransmittableThreadLocal: ${stringTransmittableThreadLocal.get()}")
println("personReferenceTransmittableThreadLocal: ${personReferenceTransmittableThreadLocal.get()}")
println("personCopyTransmittableThreadLocal: ${personCopyTransmittableThreadLocal.get()}")
}