-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReentrantLockTest.java
More file actions
170 lines (149 loc) · 4.06 KB
/
ReentrantLockTest.java
File metadata and controls
170 lines (149 loc) · 4.06 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package thread;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* 内置锁和显示锁
* Created by Administrator on 2017/5/10 0010.
*/
public class ReentrantLockTest{
public static void main(String[] args) {
for(int i = 0;i<5;i++){
rwTest();
}
for(int i = 0;i<5;i++){
synTest();
}
}
/**
* 显示锁
*/
private static void synTest() {
LinkedList list = new LinkedList();
for(int i=0;i<10000;i++){
list.add(i);
}
final SynList synList = new SynList(list);
Thread writer = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
synList.put(i);
}
}
});
Thread reader1 = new Thread(new Runnable(){
@Override
public void run() {
for(int i = 0;i<10000;i++){
synList.get(i);
}
}
});
Thread reader2 = new Thread(new Runnable(){
@Override
public void run() {
for(int i = 0;i<10000;i++){
synList.get(i);
}
}
});
long beginTime = System.currentTimeMillis();
writer.start();
reader1.start();
reader2.start();
try {
writer.join();
reader1.join();
reader2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("SynList need "+(System.currentTimeMillis()-beginTime));
}
/**
* 读写锁测试
*/
private static void rwTest() {
List list = new LinkedList();
for(int i = 0;i<10000;i++){
list.add(i);
}
final RWLockList rwLockList = new RWLockList(list);//初始化数据
Thread writer = new Thread(new Runnable(){
@Override
public void run() {
for(int i = 0;i<10000;i++){
rwLockList.put(i);
}
}
});
Thread reader1 = new Thread(new Runnable(){
@Override
public void run() {
for(int i = 0;i<10000;i++){
rwLockList.get(i);
}
}
});
Thread reader2 = new Thread(new Runnable(){
@Override
public void run() {
for(int i = 0;i<10000;i++){
rwLockList.get(i);
}
}
});
long beginTime = System.currentTimeMillis();
writer.start();
reader1.start();
reader2.start();
try {
writer.join();
reader1.join();
reader2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("RWLockList need "+(System.currentTimeMillis()-beginTime));
}
}
class RWLockList{//读写锁
private List list;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Lock readLock = lock.readLock();//读锁
private final Lock writeLock = lock.writeLock();//写锁
public RWLockList(List list) {
this.list = list;
}
public int get(int index){
readLock.lock();
try {
return (int)list.get(index);
}finally {
readLock.unlock();
}
}
public void put(int value){
writeLock.lock();
try {
list.add(value);
}finally {
writeLock.unlock();
}
}
}
class SynList{//同步锁
private List list;
public SynList(List list) {
this.list = list;
}
public synchronized int get(int index){
return (int)list.get(index);
}
public synchronized void put(int value){
list.add(value);
}
}