forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT17.java
More file actions
40 lines (32 loc) · 880 Bytes
/
T17.java
File metadata and controls
40 lines (32 loc) · 880 Bytes
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
package com.basic;
import java.util.concurrent.TimeUnit;
/**
* @program JavaBooks
* @description: ThreadLocal
* @author: mf
* @create: 2019/12/30 23:55
*/
public class T17 {
private static ThreadLocal<person> tl = new ThreadLocal<>();
public static void main(String[] args) {
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(tl.get()); // 无法得到线程二的new的person
}).start();
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
tl.set(new person());
}).start();
}
}
class person {
String name = "zhangsan";
}