-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadLocalTest1.java
More file actions
68 lines (54 loc) · 1.56 KB
/
Copy pathThreadLocalTest1.java
File metadata and controls
68 lines (54 loc) · 1.56 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
package thirdPart;
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadLocalTest1 {
private static ThreadLocal<User> local = new ThreadLocal<User>();
private static ThreadLocal<User> local2 = new ThreadLocal<User>();
private int test = nextHashCode();
private static AtomicInteger nextHashCode = new AtomicInteger(0);
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
User user1 = new User("zhangz", 99);
User user2 = new User("zhasfdaangz", 99);
System.out.println(Thread.currentThread().getName());
local.set(user1);
local2.set(user1);
// new Thread(new Runnable() {
//
// @Override
// public void run() {
// local2.set(user2);
// User user1T2 = local2.get();
// System.out.println(user1T2.getName());
// }
//
// }).start();
local.get().setName("ff1");;
local2.get().setName("ff2");;
User user1T1 = local.get();
User user1T2 = local2.get();
System.out.println(user1T1.getName());
System.out.println(user1T2.getName());
}
private static int nextHashCode() {
return nextHashCode.getAndAdd(0x61c88647);
}
static class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}