-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo10.java
More file actions
52 lines (44 loc) · 1.16 KB
/
Copy pathThreadDemo10.java
File metadata and controls
52 lines (44 loc) · 1.16 KB
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
package thirdPart;
import java.util.Date;
public class ThreadDemo10 {
public static void main(String[] args) {
try {
System.out.println("初始化值: === " + Tools10.t1.get());
for (int i = 0; i < 10; i++) {
System.out.println("main 线程中取值 ==== " + Tools10.t1.get());
Thread.sleep(100);
}
Thread.sleep(5000);
ThreadDemo10TestA a = new ThreadDemo10TestA();
a.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Tools10 {
public static InheritableThreadLocalExt t1 = new InheritableThreadLocalExt();
}
class ThreadDemo10TestA extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("Thread A 线程中取值 ==== " + Tools10.t1.get());
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class InheritableThreadLocalExt extends InheritableThreadLocal<Object> {
@Override
public Object initialValue() {
return new Date().getTime();
}
@Override
public Object childValue(Object parentValue) {
return parentValue + " 我是在子线程加的 ";
}
}