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
}

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

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188
