forked from alibaba/transmittable-thread-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgentDemo.java
135 lines (112 loc) · 4.47 KB
/
AgentDemo.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.alibaba.demo.agent;
import com.alibaba.ttl.TransmittableThreadLocal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
public final class AgentDemo {
private static TransmittableThreadLocal<String> stringTransmittableThreadLocal = new TransmittableThreadLocal<String>();
private static TransmittableThreadLocal<Person> personReferenceTransmittableThreadLocal = new TransmittableThreadLocal<Person>() {
@Override
protected Person initialValue() {
return new Person("unnamed", -1);
}
};
private static TransmittableThreadLocal<Person> personCopyTransmittableThreadLocal = new TransmittableThreadLocal<Person>() {
@Override
protected Person copy(Person parentValue) {
// copy value to child thread
return new Person(parentValue.getName(), parentValue.getAge());
}
@Override
protected Person initialValue() {
return new Person("unnamed", -1);
}
};
private AgentDemo() {
throw new InstantiationError("Must not instantiate this class");
}
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(3);
expandThreadPool(executorService);
stringTransmittableThreadLocal.set("foo - main");
personReferenceTransmittableThreadLocal.set(new Person("jerry - reference", 1));
personCopyTransmittableThreadLocal.set(new Person("Tom - value", 2));
printTtlInstancesInfo("Main - Before execution of thread pool");
Future<?> submit = executorService.submit(new Runnable() {
@Override
public void run() {
printTtlInstancesInfo("Thread Pool - enter");
stringTransmittableThreadLocal.set("foo - modified in thread pool");
personReferenceTransmittableThreadLocal.get().setName("jerry - reference - modified in thread pool");
personCopyTransmittableThreadLocal.get().setName("Tom - value - modified in thread pool");
printTtlInstancesInfo("Thread Pool - leave");
}
});
submit.get();
printTtlInstancesInfo("Main - After execution of thread pool");
executorService.shutdown();
if (!executorService.awaitTermination(3, TimeUnit.SECONDS)) {
System.exit(1);
}
}
private static void expandThreadPool(ExecutorService executor) throws Exception {
List<Future<?>> ret = new ArrayList<Future<?>>();
for (int i = 0; i < 3; ++i) {
Future<?> submit = executor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
ret.add(submit);
}
for (Future<?> future : ret) {
future.get();
}
}
private static void printTtlInstancesInfo(String msg) {
System.out.println("====================================================");
System.out.println(msg);
System.out.println("====================================================");
System.out.println("stringTransmittableThreadLocal: " + stringTransmittableThreadLocal.get());
System.out.println("personReferenceTransmittableThreadLocal: " + personReferenceTransmittableThreadLocal.get());
System.out.println("personCopyTransmittableThreadLocal: " + personCopyTransmittableThreadLocal.get());
}
public static class Person {
String name;
int age;
public String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}