-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadLocalTest.java
More file actions
67 lines (59 loc) · 1.62 KB
/
ThreadLocalTest.java
File metadata and controls
67 lines (59 loc) · 1.62 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
package Thread.Basic;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/*
* 线程本地变量:对一个共享变量,为每一个线程开辟不同的存储
* 例如5个线程访问a变量,那线程本地存储就会生成5个用于a的存储块
*
* */
public class ThreadLocalTest {
public static void main(String[] args) {
ExecutorService eService=Executors.newCachedThreadPool();
for(int i=0;i<5;i++){
eService.execute(new ThreadLocalRunnable(i));
}
eService.shutdown();
try {
TimeUnit.MICROSECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
eService.shutdownNow();
}
}
class ThreadLocalRunnable implements Runnable{
private final int id;
public ThreadLocalRunnable(int id) {
this.id=id;
}
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
ThreadLocalVariableHolder.increment();
System.out.println(this);
Thread.yield();
}
}
public String toString(){
return "#"+id+":"+ThreadLocalVariableHolder.get();
}
}
//类中ThreadLocal类型的value存放产生的值
//increment和get方法都不是同步的,但是ThreadLocal保证了其不会出现竞争条件
class ThreadLocalVariableHolder{
private static ThreadLocal<Integer> value=new ThreadLocal<Integer>(){
private Random random=new Random(47);
protected Integer initialValue() {
return random.nextInt(1000);
};
};
public static void increment(){
value.set(value.get()+1);
}
public static int get(){
return value.get();
}
}