-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
65 lines (53 loc) · 1.58 KB
/
Test.java
File metadata and controls
65 lines (53 loc) · 1.58 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
package Thread.threadLocal;
/*
* ThreadLocal实例
*
* 两个线程产生的序号共享一个Test实例,但没有相互干扰,因为通过ThreadLocal为每一个该变量的线程提供独立的变量副本,而不会影响其他其他线程的所对应的副本
*
* 同同步机制(锁)一样,为了解决多线程访问相同变量冲突的问题
* 同步:以时间换空间
* ThreadLocal:以空间换时间
*
* */
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Test {
private static final ThreadLocal<DateFormat> threadLocal=new ThreadLocal<DateFormat>(){
//返回线程局部变量的初始值
protected DateFormat initialValue() {
// TODO Auto-generated method stub
return new SimpleDateFormat("yyyy-MM-dd");
}
};
private static ThreadLocal<Integer> seqNum=new ThreadLocal<Integer>(){
protected Integer initialValue() {
return 0;
}
};
//获取下一个序列值
public int nextSqlNum(){
seqNum.set(seqNum.get()+1);
return seqNum.get();
}
public static void main(String[] args) {
//System.out.println(threadLocal.get().format(new Date()));
Test test=new Test();
TestClient testClient=new TestClient(test);
TestClient testClient1=new TestClient(test);
testClient.start();
testClient1.start();
}
}
class TestClient extends Thread{
private Test test;
public TestClient(Test test) {
this.test=test;
}
@Override
public void run() {
//打印出三个序列号
for(int i=0;i<3;i++){
System.out.println(Thread.currentThread().getName()+"序列号:"+test.nextSqlNum());
}
}
}