-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo9.java
More file actions
73 lines (64 loc) · 1.83 KB
/
Copy pathThreadDemo9.java
File metadata and controls
73 lines (64 loc) · 1.83 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package thirdPart;
/**
* @author yangxp
* @date 2017年8月9日 上午11:30:11 ThreadLocal 是线程的共享对象。每个线程都是独立。
*/
public class ThreadDemo9 {
public static void main(String[] args) {
System.out.println("Main get Value ==== " + Tools.t1.get());
ThreadDemo9TestA threadDemo9TestA = new ThreadDemo9TestA();
ThreadDemo9TestB threadDemo9TestB = new ThreadDemo9TestB();
threadDemo9TestA.start();
threadDemo9TestB.start();
for (int i = 0; i < 10; i++) {
Tools.t1.set("Main ====" + (i + 1));
System.out.println("Main get Value ==== " + Tools.t1.get());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Tools {
public static ThreadLocalExt t1 = new ThreadLocalExt();
// public static ThreadLocal<String> t2 = new ThreadLocal<String>();
}
class ThreadDemo9TestA extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
Tools.t1.set("ThreadA ====" + (i + 1));
System.out.println("ThreadA get Value ==== " + Tools.t1.get());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ThreadDemo9TestB extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
Tools.t1.set("ThreadB ====" + (i + 1));
System.out.println("ThreadB get Value ==== " + Tools.t1.get());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ThreadLocalExt extends ThreadLocal<Object> {
@Override
public Object initialValue() {
return "这个是初始化的默认值,第一次get 不再为 null";
}
}