JDK 5.0 Concurrency Utilities 并发处理(3)ReadWriteLock 读写锁
2011-10-25 14:21
495 查看
1
package com.vinko.test.concurrent;
2
3
import java.util.Calendar;
4
import java.util.Map;
5
import java.util.TreeMap;
6
//import java.util.concurrent.locks.Condition;
7
import java.util.concurrent.locks.Lock;
8
import java.util.concurrent.locks.ReentrantReadWriteLock;
9
10
public class TestReadWriteLock {
11
12
private ReentrantReadWriteLock lock = null;
13
14
private Lock readLock = null;
15
private Lock writeLock = null;
16
17
// private Condition condition = null;
18
19
public int key = 100;
20
public int index = 100;
21
22
public Map<Integer, String> dataMap = null;
23
24
public TestReadWriteLock() {
25
lock = new ReentrantReadWriteLock(true);
26
27
readLock = lock.readLock();
28
writeLock = lock.writeLock();
29
30
// condition = writeLock.newCondition();
31
32
dataMap = new TreeMap<Integer, String>();
33
}
34
35
/**
36
* @param args
37
*/
38
public static void main(String[] args) {
39
40
TestReadWriteLock tester = new TestReadWriteLock();
41
42
// test lock downgrading
43
44
// tester.readLock.lock();
45
// System.out.println(Thread.currentThread() + " get readLock");
46
47
tester.writeLock.lock();
48
System.out.println(Thread.currentThread() + " get writeLock.");
49
50
tester.writeLock.lock();
51
System.out.println(Thread.currentThread() + " get writeLock.");
52
53
tester.readLock.lock();
54
System.out.println(Thread.currentThread() + " get readLock");
55
56
tester.readLock.lock();
57
System.out.println(Thread.currentThread() + " get readLock");
58
59
// tester.writeLock.lock();
60
// System.out.println(Thread.currentThread() + " get writeLock.");
61
62
tester.readLock.unlock();
63
tester.readLock.unlock();
64
tester.writeLock.unlock();
65
tester.writeLock.unlock();
66
67
tester.test();
68
}
69
70
public void test() {
71
72
for (int i = 0; i < 10; i++) {
73
new Thread(new reader(this)).start();
74
}
75
76
for (int i = 0; i <3; i++) {
77
new Thread(new writer(this)).start();
78
}
79
80
}
81
82
public void read() {
83
/*
84
writeLock.lock();
85
86
try {
87
condition.await();
88
} catch (InterruptedException e1) {
89
e1.printStackTrace();
90
}
91
92
writeLock.unlock();
93
*/
94
readLock.lock();
95
96
try {
97
if (dataMap.isEmpty()) {
98
Calendar now = Calendar.getInstance();
99
System.out.println(now.getTime() + " R " + Thread.currentThread() + " get key, but map is empty.");
100
}
101
102
String value = dataMap.get(index);
103
104
Calendar now = Calendar.getInstance();
105
System.out.println(now.getTime() + " R " + Thread.currentThread() + " get key = " + index + " value = " + value + " map size = " + dataMap.size());
106
107
// get next value
108
if (value != null) {
109
index ++;
110
}
111
} finally {
112
readLock.unlock();
113
}
114
115
try {
116
Thread.sleep(3000);
117
} catch (InterruptedException e) {
118
e.printStackTrace();
119
}
120
}
121
122
public void write() {
123
124
writeLock.lock();
125
126
127
try {
128
String value = "value" + key;
129
130
dataMap.put(new Integer(key), value);
131
132
Calendar now = Calendar.getInstance();
133
System.out.println(now.getTime() + " W " + Thread.currentThread() + " put key = " + key + " value = " + value + " map size = " + dataMap.size());
134
135
key ++;
136
137
// condition.signal();
138
139
try {
140
Thread.sleep(500);
141
} catch (InterruptedException e) {
142
e.printStackTrace();
143
}
144
145
} finally {
146
writeLock.unlock();
147
}
148
149
}
150
}
151
152
class reader implements Runnable {
153
154
private TestReadWriteLock tester = null;
155
156
public reader(TestReadWriteLock tester) {
157
this.tester = tester;
158
}
159
160
public void run() {
161
Calendar now = Calendar.getInstance();
162
163
System.out.println(now.getTime() + " R " + Thread.currentThread() + " started");
164
165
while (true) {
166
tester.read();
167
}
168
}
169
}
170
171
class writer implements Runnable {
172
173
private TestReadWriteLock tester = null;
174
175
public writer(TestReadWriteLock tester) {
176
this.tester = tester;
177
}
178
179
public void run() {
180
Calendar now = Calendar.getInstance();
181
182
System.out.println(now.getTime() + " W " + Thread.currentThread() + " started");
183
184
while (true) {
185
tester.write();
186
}
187
}
188
}
package com.vinko.test.concurrent;
2
3
import java.util.Calendar;
4
import java.util.Map;
5
import java.util.TreeMap;
6
//import java.util.concurrent.locks.Condition;
7
import java.util.concurrent.locks.Lock;
8
import java.util.concurrent.locks.ReentrantReadWriteLock;
9
10
public class TestReadWriteLock {
11
12
private ReentrantReadWriteLock lock = null;
13
14
private Lock readLock = null;
15
private Lock writeLock = null;
16
17
// private Condition condition = null;
18
19
public int key = 100;
20
public int index = 100;
21
22
public Map<Integer, String> dataMap = null;
23
24
public TestReadWriteLock() {
25
lock = new ReentrantReadWriteLock(true);
26
27
readLock = lock.readLock();
28
writeLock = lock.writeLock();
29
30
// condition = writeLock.newCondition();
31
32
dataMap = new TreeMap<Integer, String>();
33
}
34
35
/**
36
* @param args
37
*/
38
public static void main(String[] args) {
39
40
TestReadWriteLock tester = new TestReadWriteLock();
41
42
// test lock downgrading
43
44
// tester.readLock.lock();
45
// System.out.println(Thread.currentThread() + " get readLock");
46
47
tester.writeLock.lock();
48
System.out.println(Thread.currentThread() + " get writeLock.");
49
50
tester.writeLock.lock();
51
System.out.println(Thread.currentThread() + " get writeLock.");
52
53
tester.readLock.lock();
54
System.out.println(Thread.currentThread() + " get readLock");
55
56
tester.readLock.lock();
57
System.out.println(Thread.currentThread() + " get readLock");
58
59
// tester.writeLock.lock();
60
// System.out.println(Thread.currentThread() + " get writeLock.");
61
62
tester.readLock.unlock();
63
tester.readLock.unlock();
64
tester.writeLock.unlock();
65
tester.writeLock.unlock();
66
67
tester.test();
68
}
69
70
public void test() {
71
72
for (int i = 0; i < 10; i++) {
73
new Thread(new reader(this)).start();
74
}
75
76
for (int i = 0; i <3; i++) {
77
new Thread(new writer(this)).start();
78
}
79
80
}
81
82
public void read() {
83
/*
84
writeLock.lock();
85
86
try {
87
condition.await();
88
} catch (InterruptedException e1) {
89
e1.printStackTrace();
90
}
91
92
writeLock.unlock();
93
*/
94
readLock.lock();
95
96
try {
97
if (dataMap.isEmpty()) {
98
Calendar now = Calendar.getInstance();
99
System.out.println(now.getTime() + " R " + Thread.currentThread() + " get key, but map is empty.");
100
}
101
102
String value = dataMap.get(index);
103
104
Calendar now = Calendar.getInstance();
105
System.out.println(now.getTime() + " R " + Thread.currentThread() + " get key = " + index + " value = " + value + " map size = " + dataMap.size());
106
107
// get next value
108
if (value != null) {
109
index ++;
110
}
111
} finally {
112
readLock.unlock();
113
}
114
115
try {
116
Thread.sleep(3000);
117
} catch (InterruptedException e) {
118
e.printStackTrace();
119
}
120
}
121
122
public void write() {
123
124
writeLock.lock();
125
126
127
try {
128
String value = "value" + key;
129
130
dataMap.put(new Integer(key), value);
131
132
Calendar now = Calendar.getInstance();
133
System.out.println(now.getTime() + " W " + Thread.currentThread() + " put key = " + key + " value = " + value + " map size = " + dataMap.size());
134
135
key ++;
136
137
// condition.signal();
138
139
try {
140
Thread.sleep(500);
141
} catch (InterruptedException e) {
142
e.printStackTrace();
143
}
144
145
} finally {
146
writeLock.unlock();
147
}
148
149
}
150
}
151
152
class reader implements Runnable {
153
154
private TestReadWriteLock tester = null;
155
156
public reader(TestReadWriteLock tester) {
157
this.tester = tester;
158
}
159
160
public void run() {
161
Calendar now = Calendar.getInstance();
162
163
System.out.println(now.getTime() + " R " + Thread.currentThread() + " started");
164
165
while (true) {
166
tester.read();
167
}
168
}
169
}
170
171
class writer implements Runnable {
172
173
private TestReadWriteLock tester = null;
174
175
public writer(TestReadWriteLock tester) {
176
this.tester = tester;
177
}
178
179
public void run() {
180
Calendar now = Calendar.getInstance();
181
182
System.out.println(now.getTime() + " W " + Thread.currentThread() + " started");
183
184
while (true) {
185
tester.write();
186
}
187
}
188
}
相关文章推荐
- JDK 5.0 Concurrency Utilities 并发处理(3)ReadWriteLock 读写锁
- Java高并发程序-Chapter3 JDK并发包(第十二讲)同步控制之 ReadWriteLock
- JDK 5.0 Concurrency Utilities 并发处理(1)ReentrantLock 可重入锁
- Java 并发问题的处理神器:ReentrantReadWriteLock
- java多线程:并发包中ReentrantReadWriteLock读写锁的锁降级模板 写锁降级为读锁
- java多线程:并发包中ReentrantReadWriteLock读写锁的锁降级模板
- JDK 5.0 Concurrency Utilities 并发处理 ReentrantLock 可重入锁
- jdk源码解读-并发包-Lock-ReentrantReadWriteLock(1)-整体介绍以及读锁的lock 和 unlock 解析
- java多线程:并发包中ReentrantReadWriteLock读写锁的原理
- java高并发程序设计总结五:jdk并发包其他同步控制工具类:ReadWriteLock/CountDownLatch/CyclicBarrier/LockSupport
- 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock
- ReentrantReadWriteLock读写锁
- 多线程读写文件利器-ReentrantReadWriteLock
- 【Java并发编程实战】—–“J.U.C”:ReentrantReadWriteLock
- 【死磕Java并发】-----J.U.C之读写锁:ReentrantReadWriteLock
- 高并发第十一弹:J.U.C -AQS(AbstractQueuedSynchronizer) 组件:Lock,ReentrantLock,ReentrantReadWriteLock,StampedLock
- 【Java并发编程实战】—–“J.U.C”:ReentrantReadWriteLock
- Java并发编程:重入锁(ReentranceLock )、读写锁(ReadWriteLock)代码实现
- 并发编程中Lock, synchronized和 ReadWriteLock的异同
- ReentrantReadWriteLock读写锁