1 |
dl |
1.1 |
/* |
2 |
dl |
1.12 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
jsr166 |
1.28 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.concurrent.locks.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
dl |
1.2 |
import java.io.*; |
13 |
dl |
1.6 |
import java.util.*; |
14 |
dl |
1.1 |
|
15 |
dl |
1.3 |
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
16 |
dl |
1.1 |
public static void main(String[] args) { |
17 |
jsr166 |
1.35 |
junit.textui.TestRunner.run (suite()); |
18 |
dl |
1.1 |
} |
19 |
|
|
public static Test suite() { |
20 |
jsr166 |
1.35 |
return new TestSuite(ReentrantReadWriteLockTest.class); |
21 |
dl |
1.1 |
} |
22 |
|
|
|
23 |
dl |
1.6 |
/** |
24 |
|
|
* A runnable calling lockInterruptibly |
25 |
|
|
*/ |
26 |
jsr166 |
1.32 |
class InterruptibleLockRunnable extends CheckedRunnable { |
27 |
dl |
1.6 |
final ReentrantReadWriteLock lock; |
28 |
|
|
InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
29 |
jsr166 |
1.32 |
public void realRun() throws InterruptedException { |
30 |
|
|
lock.writeLock().lockInterruptibly(); |
31 |
dl |
1.6 |
} |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* A runnable calling lockInterruptibly that expects to be |
37 |
|
|
* interrupted |
38 |
|
|
*/ |
39 |
jsr166 |
1.32 |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
40 |
dl |
1.6 |
final ReentrantReadWriteLock lock; |
41 |
|
|
InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
42 |
jsr166 |
1.32 |
public void realRun() throws InterruptedException { |
43 |
|
|
lock.writeLock().lockInterruptibly(); |
44 |
dl |
1.6 |
} |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
/** |
48 |
|
|
* Subclass to expose protected methods |
49 |
|
|
*/ |
50 |
|
|
static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { |
51 |
|
|
PublicReentrantReadWriteLock() { super(); } |
52 |
jsr166 |
1.28 |
public Collection<Thread> getQueuedThreads() { |
53 |
|
|
return super.getQueuedThreads(); |
54 |
dl |
1.6 |
} |
55 |
jsr166 |
1.28 |
public Collection<Thread> getWaitingThreads(Condition c) { |
56 |
|
|
return super.getWaitingThreads(c); |
57 |
dl |
1.13 |
} |
58 |
dl |
1.6 |
} |
59 |
|
|
|
60 |
|
|
/** |
61 |
|
|
* Constructor sets given fairness, and is in unlocked state |
62 |
|
|
*/ |
63 |
jsr166 |
1.28 |
public void testConstructor() { |
64 |
jsr166 |
1.35 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
65 |
dl |
1.6 |
assertFalse(rl.isFair()); |
66 |
|
|
assertFalse(rl.isWriteLocked()); |
67 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
68 |
jsr166 |
1.35 |
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
69 |
dl |
1.6 |
assertTrue(r2.isFair()); |
70 |
|
|
assertFalse(r2.isWriteLocked()); |
71 |
dl |
1.9 |
assertEquals(0, r2.getReadLockCount()); |
72 |
jsr166 |
1.35 |
ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false); |
73 |
jsr166 |
1.32 |
assertFalse(r3.isFair()); |
74 |
|
|
assertFalse(r3.isWriteLocked()); |
75 |
|
|
assertEquals(0, r3.getReadLockCount()); |
76 |
dl |
1.6 |
} |
77 |
dl |
1.1 |
|
78 |
dl |
1.5 |
/** |
79 |
dl |
1.4 |
* write-locking and read-locking an unlocked lock succeed |
80 |
dl |
1.1 |
*/ |
81 |
jsr166 |
1.28 |
public void testLock() { |
82 |
jsr166 |
1.35 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
83 |
dl |
1.4 |
rl.writeLock().lock(); |
84 |
dl |
1.6 |
assertTrue(rl.isWriteLocked()); |
85 |
|
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
86 |
dl |
1.25 |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
87 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
88 |
dl |
1.4 |
rl.writeLock().unlock(); |
89 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
90 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
91 |
dl |
1.25 |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
92 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
93 |
dl |
1.4 |
rl.readLock().lock(); |
94 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
95 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
96 |
dl |
1.9 |
assertEquals(1, rl.getReadLockCount()); |
97 |
dl |
1.4 |
rl.readLock().unlock(); |
98 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
99 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
100 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
101 |
dl |
1.4 |
} |
102 |
|
|
|
103 |
dl |
1.1 |
|
104 |
dl |
1.5 |
/** |
105 |
dl |
1.4 |
* locking an unlocked fair lock succeeds |
106 |
|
|
*/ |
107 |
jsr166 |
1.28 |
public void testFairLock() { |
108 |
jsr166 |
1.35 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
109 |
dl |
1.4 |
rl.writeLock().lock(); |
110 |
dl |
1.6 |
assertTrue(rl.isWriteLocked()); |
111 |
|
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
112 |
dl |
1.25 |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
113 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
114 |
dl |
1.4 |
rl.writeLock().unlock(); |
115 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
116 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
117 |
dl |
1.25 |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
118 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
119 |
dl |
1.4 |
rl.readLock().lock(); |
120 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
121 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
122 |
dl |
1.9 |
assertEquals(1, rl.getReadLockCount()); |
123 |
dl |
1.4 |
rl.readLock().unlock(); |
124 |
dl |
1.6 |
assertFalse(rl.isWriteLocked()); |
125 |
|
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
126 |
dl |
1.9 |
assertEquals(0, rl.getReadLockCount()); |
127 |
dl |
1.2 |
} |
128 |
dl |
1.1 |
|
129 |
dl |
1.4 |
/** |
130 |
dl |
1.6 |
* getWriteHoldCount returns number of recursive holds |
131 |
|
|
*/ |
132 |
dl |
1.23 |
public void testGetWriteHoldCount() { |
133 |
jsr166 |
1.35 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
134 |
|
|
for (int i = 1; i <= SIZE; i++) { |
135 |
|
|
lock.writeLock().lock(); |
136 |
|
|
assertEquals(i,lock.getWriteHoldCount()); |
137 |
|
|
} |
138 |
|
|
for (int i = SIZE; i > 0; i--) { |
139 |
|
|
lock.writeLock().unlock(); |
140 |
|
|
assertEquals(i-1,lock.getWriteHoldCount()); |
141 |
|
|
} |
142 |
dl |
1.6 |
} |
143 |
dl |
1.23 |
|
144 |
|
|
/** |
145 |
dl |
1.25 |
* WriteLock.getHoldCount returns number of recursive holds |
146 |
|
|
*/ |
147 |
|
|
public void testGetHoldCount() { |
148 |
jsr166 |
1.35 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
149 |
|
|
for (int i = 1; i <= SIZE; i++) { |
150 |
|
|
lock.writeLock().lock(); |
151 |
|
|
assertEquals(i,lock.writeLock().getHoldCount()); |
152 |
|
|
} |
153 |
|
|
for (int i = SIZE; i > 0; i--) { |
154 |
|
|
lock.writeLock().unlock(); |
155 |
|
|
assertEquals(i-1,lock.writeLock().getHoldCount()); |
156 |
|
|
} |
157 |
dl |
1.25 |
} |
158 |
|
|
|
159 |
|
|
/** |
160 |
dl |
1.23 |
* getReadHoldCount returns number of recursive holds |
161 |
|
|
*/ |
162 |
|
|
public void testGetReadHoldCount() { |
163 |
jsr166 |
1.35 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
164 |
|
|
for (int i = 1; i <= SIZE; i++) { |
165 |
|
|
lock.readLock().lock(); |
166 |
|
|
assertEquals(i,lock.getReadHoldCount()); |
167 |
|
|
} |
168 |
|
|
for (int i = SIZE; i > 0; i--) { |
169 |
|
|
lock.readLock().unlock(); |
170 |
|
|
assertEquals(i-1,lock.getReadHoldCount()); |
171 |
|
|
} |
172 |
dl |
1.23 |
} |
173 |
jsr166 |
1.28 |
|
174 |
dl |
1.6 |
|
175 |
|
|
/** |
176 |
dl |
1.4 |
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
177 |
|
|
*/ |
178 |
jsr166 |
1.28 |
public void testUnlock_IllegalMonitorStateException() { |
179 |
jsr166 |
1.35 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
180 |
|
|
try { |
181 |
|
|
rl.writeLock().unlock(); |
182 |
|
|
shouldThrow(); |
183 |
|
|
} catch (IllegalMonitorStateException success) {} |
184 |
dl |
1.4 |
} |
185 |
dl |
1.1 |
|
186 |
|
|
|
187 |
dl |
1.4 |
/** |
188 |
|
|
* write-lockInterruptibly is interruptible |
189 |
|
|
*/ |
190 |
jsr166 |
1.32 |
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
191 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
192 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
193 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
194 |
|
|
lock.writeLock().lockInterruptibly(); |
195 |
|
|
lock.writeLock().unlock(); |
196 |
|
|
lock.writeLock().lockInterruptibly(); |
197 |
|
|
lock.writeLock().unlock(); |
198 |
|
|
}}); |
199 |
jsr166 |
1.32 |
|
200 |
|
|
lock.writeLock().lock(); |
201 |
|
|
t.start(); |
202 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
203 |
|
|
t.interrupt(); |
204 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
205 |
|
|
lock.writeLock().unlock(); |
206 |
|
|
t.join(); |
207 |
jsr166 |
1.28 |
} |
208 |
dl |
1.1 |
|
209 |
dl |
1.4 |
/** |
210 |
dl |
1.15 |
* timed write-tryLock is interruptible |
211 |
dl |
1.4 |
*/ |
212 |
jsr166 |
1.32 |
public void testWriteTryLock_Interrupted() throws InterruptedException { |
213 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
214 |
|
|
lock.writeLock().lock(); |
215 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
216 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
217 |
|
|
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
218 |
|
|
}}); |
219 |
jsr166 |
1.32 |
|
220 |
|
|
t.start(); |
221 |
|
|
t.interrupt(); |
222 |
|
|
lock.writeLock().unlock(); |
223 |
|
|
t.join(); |
224 |
dl |
1.2 |
} |
225 |
|
|
|
226 |
dl |
1.4 |
/** |
227 |
|
|
* read-lockInterruptibly is interruptible |
228 |
|
|
*/ |
229 |
jsr166 |
1.32 |
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
230 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
231 |
|
|
lock.writeLock().lock(); |
232 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
233 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
234 |
|
|
lock.readLock().lockInterruptibly(); |
235 |
|
|
}}); |
236 |
jsr166 |
1.32 |
|
237 |
|
|
t.start(); |
238 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
239 |
|
|
t.interrupt(); |
240 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
241 |
|
|
lock.writeLock().unlock(); |
242 |
|
|
t.join(); |
243 |
jsr166 |
1.28 |
} |
244 |
dl |
1.2 |
|
245 |
dl |
1.4 |
/** |
246 |
dl |
1.15 |
* timed read-tryLock is interruptible |
247 |
dl |
1.4 |
*/ |
248 |
jsr166 |
1.32 |
public void testReadTryLock_Interrupted() throws InterruptedException { |
249 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
250 |
|
|
lock.writeLock().lock(); |
251 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
252 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
253 |
|
|
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
254 |
|
|
}}); |
255 |
jsr166 |
1.32 |
|
256 |
|
|
t.start(); |
257 |
|
|
t.interrupt(); |
258 |
|
|
t.join(); |
259 |
dl |
1.1 |
} |
260 |
|
|
|
261 |
jsr166 |
1.28 |
|
262 |
dl |
1.4 |
/** |
263 |
dl |
1.15 |
* write-tryLock fails if locked |
264 |
dl |
1.4 |
*/ |
265 |
jsr166 |
1.32 |
public void testWriteTryLockWhenLocked() throws InterruptedException { |
266 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
267 |
|
|
lock.writeLock().lock(); |
268 |
|
|
Thread t = new Thread(new Runnable() { |
269 |
dl |
1.4 |
public void run() { |
270 |
dl |
1.3 |
threadAssertFalse(lock.writeLock().tryLock()); |
271 |
jsr166 |
1.35 |
} |
272 |
|
|
}); |
273 |
jsr166 |
1.32 |
|
274 |
|
|
t.start(); |
275 |
|
|
t.join(); |
276 |
|
|
lock.writeLock().unlock(); |
277 |
jsr166 |
1.28 |
} |
278 |
dl |
1.2 |
|
279 |
dl |
1.4 |
/** |
280 |
dl |
1.15 |
* read-tryLock fails if locked |
281 |
dl |
1.4 |
*/ |
282 |
jsr166 |
1.32 |
public void testReadTryLockWhenLocked() throws InterruptedException { |
283 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
284 |
|
|
lock.writeLock().lock(); |
285 |
|
|
Thread t = new Thread(new Runnable() { |
286 |
dl |
1.4 |
public void run() { |
287 |
dl |
1.3 |
threadAssertFalse(lock.readLock().tryLock()); |
288 |
jsr166 |
1.35 |
} |
289 |
|
|
}); |
290 |
jsr166 |
1.32 |
|
291 |
|
|
t.start(); |
292 |
|
|
t.join(); |
293 |
|
|
lock.writeLock().unlock(); |
294 |
jsr166 |
1.28 |
} |
295 |
dl |
1.2 |
|
296 |
dl |
1.4 |
/** |
297 |
|
|
* Multiple threads can hold a read lock when not write-locked |
298 |
|
|
*/ |
299 |
jsr166 |
1.32 |
public void testMultipleReadLocks() throws InterruptedException { |
300 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
301 |
|
|
lock.readLock().lock(); |
302 |
|
|
Thread t = new Thread(new Runnable() { |
303 |
dl |
1.4 |
public void run() { |
304 |
dl |
1.3 |
threadAssertTrue(lock.readLock().tryLock()); |
305 |
dl |
1.2 |
lock.readLock().unlock(); |
306 |
jsr166 |
1.35 |
} |
307 |
|
|
}); |
308 |
jsr166 |
1.32 |
|
309 |
|
|
t.start(); |
310 |
|
|
t.join(); |
311 |
|
|
lock.readLock().unlock(); |
312 |
jsr166 |
1.28 |
} |
313 |
dl |
1.2 |
|
314 |
dl |
1.4 |
/** |
315 |
|
|
* A writelock succeeds after reading threads unlock |
316 |
|
|
*/ |
317 |
jsr166 |
1.32 |
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
318 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
319 |
|
|
lock.readLock().lock(); |
320 |
|
|
Thread t1 = new Thread(new Runnable() { |
321 |
dl |
1.4 |
public void run() { |
322 |
dl |
1.2 |
lock.readLock().lock(); |
323 |
|
|
lock.readLock().unlock(); |
324 |
jsr166 |
1.35 |
} |
325 |
|
|
}); |
326 |
|
|
Thread t2 = new Thread(new Runnable() { |
327 |
dl |
1.4 |
public void run() { |
328 |
dl |
1.2 |
lock.writeLock().lock(); |
329 |
|
|
lock.writeLock().unlock(); |
330 |
jsr166 |
1.35 |
} |
331 |
|
|
}); |
332 |
dl |
1.2 |
|
333 |
jsr166 |
1.32 |
t1.start(); |
334 |
|
|
t2.start(); |
335 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
336 |
|
|
lock.readLock().unlock(); |
337 |
|
|
t1.join(MEDIUM_DELAY_MS); |
338 |
|
|
t2.join(MEDIUM_DELAY_MS); |
339 |
|
|
assertTrue(!t1.isAlive()); |
340 |
|
|
assertTrue(!t2.isAlive()); |
341 |
jsr166 |
1.28 |
} |
342 |
dl |
1.2 |
|
343 |
dl |
1.4 |
/** |
344 |
|
|
* Readlocks succeed after a writing thread unlocks |
345 |
|
|
*/ |
346 |
jsr166 |
1.32 |
public void testReadAfterWriteLock() throws InterruptedException { |
347 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
348 |
|
|
lock.writeLock().lock(); |
349 |
|
|
Thread t1 = new Thread(new Runnable() { |
350 |
dl |
1.4 |
public void run() { |
351 |
dl |
1.2 |
lock.readLock().lock(); |
352 |
|
|
lock.readLock().unlock(); |
353 |
jsr166 |
1.35 |
} |
354 |
|
|
}); |
355 |
|
|
Thread t2 = new Thread(new Runnable() { |
356 |
dl |
1.4 |
public void run() { |
357 |
dl |
1.2 |
lock.readLock().lock(); |
358 |
|
|
lock.readLock().unlock(); |
359 |
jsr166 |
1.35 |
} |
360 |
|
|
}); |
361 |
dl |
1.2 |
|
362 |
jsr166 |
1.32 |
t1.start(); |
363 |
|
|
t2.start(); |
364 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
365 |
|
|
lock.writeLock().unlock(); |
366 |
|
|
t1.join(MEDIUM_DELAY_MS); |
367 |
|
|
t2.join(MEDIUM_DELAY_MS); |
368 |
|
|
assertTrue(!t1.isAlive()); |
369 |
|
|
assertTrue(!t2.isAlive()); |
370 |
jsr166 |
1.28 |
} |
371 |
dl |
1.2 |
|
372 |
dl |
1.20 |
/** |
373 |
|
|
* Read trylock succeeds if write locked by current thread |
374 |
|
|
*/ |
375 |
jsr166 |
1.28 |
public void testReadHoldingWriteLock() { |
376 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
377 |
|
|
lock.writeLock().lock(); |
378 |
dl |
1.20 |
assertTrue(lock.readLock().tryLock()); |
379 |
|
|
lock.readLock().unlock(); |
380 |
|
|
lock.writeLock().unlock(); |
381 |
jsr166 |
1.28 |
} |
382 |
dl |
1.20 |
|
383 |
|
|
/** |
384 |
|
|
* Read lock succeeds if write locked by current thread even if |
385 |
dl |
1.23 |
* other threads are waiting for readlock |
386 |
dl |
1.20 |
*/ |
387 |
jsr166 |
1.32 |
public void testReadHoldingWriteLock2() throws InterruptedException { |
388 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
389 |
|
|
lock.writeLock().lock(); |
390 |
|
|
Thread t1 = new Thread(new Runnable() { |
391 |
dl |
1.20 |
public void run() { |
392 |
|
|
lock.readLock().lock(); |
393 |
|
|
lock.readLock().unlock(); |
394 |
jsr166 |
1.35 |
} |
395 |
|
|
}); |
396 |
|
|
Thread t2 = new Thread(new Runnable() { |
397 |
dl |
1.20 |
public void run() { |
398 |
|
|
lock.readLock().lock(); |
399 |
|
|
lock.readLock().unlock(); |
400 |
jsr166 |
1.35 |
} |
401 |
|
|
}); |
402 |
dl |
1.20 |
|
403 |
jsr166 |
1.32 |
t1.start(); |
404 |
|
|
t2.start(); |
405 |
|
|
lock.readLock().lock(); |
406 |
|
|
lock.readLock().unlock(); |
407 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
408 |
|
|
lock.readLock().lock(); |
409 |
|
|
lock.readLock().unlock(); |
410 |
|
|
lock.writeLock().unlock(); |
411 |
|
|
t1.join(MEDIUM_DELAY_MS); |
412 |
|
|
t2.join(MEDIUM_DELAY_MS); |
413 |
|
|
assertTrue(!t1.isAlive()); |
414 |
|
|
assertTrue(!t2.isAlive()); |
415 |
jsr166 |
1.28 |
} |
416 |
dl |
1.20 |
|
417 |
|
|
/** |
418 |
dl |
1.23 |
* Read lock succeeds if write locked by current thread even if |
419 |
|
|
* other threads are waiting for writelock |
420 |
|
|
*/ |
421 |
jsr166 |
1.32 |
public void testReadHoldingWriteLock3() throws InterruptedException { |
422 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
423 |
|
|
lock.writeLock().lock(); |
424 |
|
|
Thread t1 = new Thread(new Runnable() { |
425 |
dl |
1.23 |
public void run() { |
426 |
|
|
lock.writeLock().lock(); |
427 |
|
|
lock.writeLock().unlock(); |
428 |
jsr166 |
1.35 |
} |
429 |
|
|
}); |
430 |
|
|
Thread t2 = new Thread(new Runnable() { |
431 |
dl |
1.23 |
public void run() { |
432 |
|
|
lock.writeLock().lock(); |
433 |
|
|
lock.writeLock().unlock(); |
434 |
jsr166 |
1.35 |
} |
435 |
|
|
}); |
436 |
dl |
1.23 |
|
437 |
jsr166 |
1.32 |
t1.start(); |
438 |
|
|
t2.start(); |
439 |
|
|
lock.readLock().lock(); |
440 |
|
|
lock.readLock().unlock(); |
441 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
442 |
|
|
lock.readLock().lock(); |
443 |
|
|
lock.readLock().unlock(); |
444 |
|
|
lock.writeLock().unlock(); |
445 |
|
|
t1.join(MEDIUM_DELAY_MS); |
446 |
|
|
t2.join(MEDIUM_DELAY_MS); |
447 |
|
|
assertTrue(!t1.isAlive()); |
448 |
|
|
assertTrue(!t2.isAlive()); |
449 |
jsr166 |
1.28 |
} |
450 |
dl |
1.23 |
|
451 |
|
|
|
452 |
|
|
/** |
453 |
|
|
* Write lock succeeds if write locked by current thread even if |
454 |
|
|
* other threads are waiting for writelock |
455 |
|
|
*/ |
456 |
jsr166 |
1.32 |
public void testWriteHoldingWriteLock4() throws InterruptedException { |
457 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
458 |
|
|
lock.writeLock().lock(); |
459 |
|
|
Thread t1 = new Thread(new Runnable() { |
460 |
dl |
1.23 |
public void run() { |
461 |
|
|
lock.writeLock().lock(); |
462 |
|
|
lock.writeLock().unlock(); |
463 |
jsr166 |
1.35 |
} |
464 |
|
|
}); |
465 |
|
|
Thread t2 = new Thread(new Runnable() { |
466 |
dl |
1.23 |
public void run() { |
467 |
|
|
lock.writeLock().lock(); |
468 |
|
|
lock.writeLock().unlock(); |
469 |
jsr166 |
1.35 |
} |
470 |
|
|
}); |
471 |
dl |
1.23 |
|
472 |
jsr166 |
1.32 |
t1.start(); |
473 |
|
|
t2.start(); |
474 |
|
|
lock.writeLock().lock(); |
475 |
|
|
lock.writeLock().unlock(); |
476 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
477 |
|
|
lock.writeLock().lock(); |
478 |
|
|
lock.writeLock().unlock(); |
479 |
|
|
lock.writeLock().unlock(); |
480 |
|
|
t1.join(MEDIUM_DELAY_MS); |
481 |
|
|
t2.join(MEDIUM_DELAY_MS); |
482 |
|
|
assertTrue(!t1.isAlive()); |
483 |
|
|
assertTrue(!t2.isAlive()); |
484 |
jsr166 |
1.28 |
} |
485 |
dl |
1.23 |
|
486 |
|
|
|
487 |
|
|
/** |
488 |
dl |
1.20 |
* Fair Read trylock succeeds if write locked by current thread |
489 |
|
|
*/ |
490 |
jsr166 |
1.28 |
public void testReadHoldingWriteLockFair() { |
491 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
492 |
|
|
lock.writeLock().lock(); |
493 |
dl |
1.20 |
assertTrue(lock.readLock().tryLock()); |
494 |
|
|
lock.readLock().unlock(); |
495 |
|
|
lock.writeLock().unlock(); |
496 |
jsr166 |
1.28 |
} |
497 |
dl |
1.20 |
|
498 |
|
|
/** |
499 |
|
|
* Fair Read lock succeeds if write locked by current thread even if |
500 |
dl |
1.23 |
* other threads are waiting for readlock |
501 |
dl |
1.20 |
*/ |
502 |
jsr166 |
1.32 |
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
503 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
504 |
|
|
lock.writeLock().lock(); |
505 |
|
|
Thread t1 = new Thread(new Runnable() { |
506 |
dl |
1.20 |
public void run() { |
507 |
|
|
lock.readLock().lock(); |
508 |
|
|
lock.readLock().unlock(); |
509 |
jsr166 |
1.35 |
} |
510 |
|
|
}); |
511 |
|
|
Thread t2 = new Thread(new Runnable() { |
512 |
dl |
1.20 |
public void run() { |
513 |
|
|
lock.readLock().lock(); |
514 |
|
|
lock.readLock().unlock(); |
515 |
jsr166 |
1.35 |
} |
516 |
|
|
}); |
517 |
dl |
1.20 |
|
518 |
jsr166 |
1.32 |
t1.start(); |
519 |
|
|
t2.start(); |
520 |
|
|
lock.readLock().lock(); |
521 |
|
|
lock.readLock().unlock(); |
522 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
523 |
|
|
lock.readLock().lock(); |
524 |
|
|
lock.readLock().unlock(); |
525 |
|
|
lock.writeLock().unlock(); |
526 |
|
|
t1.join(MEDIUM_DELAY_MS); |
527 |
|
|
t2.join(MEDIUM_DELAY_MS); |
528 |
|
|
assertTrue(!t1.isAlive()); |
529 |
|
|
assertTrue(!t2.isAlive()); |
530 |
jsr166 |
1.28 |
} |
531 |
dl |
1.20 |
|
532 |
dl |
1.2 |
|
533 |
dl |
1.4 |
/** |
534 |
dl |
1.23 |
* Fair Read lock succeeds if write locked by current thread even if |
535 |
|
|
* other threads are waiting for writelock |
536 |
|
|
*/ |
537 |
jsr166 |
1.32 |
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
538 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
539 |
|
|
lock.writeLock().lock(); |
540 |
|
|
Thread t1 = new Thread(new Runnable() { |
541 |
dl |
1.23 |
public void run() { |
542 |
|
|
lock.writeLock().lock(); |
543 |
|
|
lock.writeLock().unlock(); |
544 |
jsr166 |
1.35 |
} |
545 |
|
|
}); |
546 |
|
|
Thread t2 = new Thread(new Runnable() { |
547 |
dl |
1.23 |
public void run() { |
548 |
|
|
lock.writeLock().lock(); |
549 |
|
|
lock.writeLock().unlock(); |
550 |
jsr166 |
1.35 |
} |
551 |
|
|
}); |
552 |
dl |
1.23 |
|
553 |
jsr166 |
1.32 |
t1.start(); |
554 |
|
|
t2.start(); |
555 |
|
|
lock.readLock().lock(); |
556 |
|
|
lock.readLock().unlock(); |
557 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
558 |
|
|
lock.readLock().lock(); |
559 |
|
|
lock.readLock().unlock(); |
560 |
|
|
lock.writeLock().unlock(); |
561 |
|
|
t1.join(MEDIUM_DELAY_MS); |
562 |
|
|
t2.join(MEDIUM_DELAY_MS); |
563 |
|
|
assertTrue(!t1.isAlive()); |
564 |
|
|
assertTrue(!t2.isAlive()); |
565 |
jsr166 |
1.28 |
} |
566 |
dl |
1.23 |
|
567 |
|
|
|
568 |
|
|
/** |
569 |
|
|
* Fair Write lock succeeds if write locked by current thread even if |
570 |
|
|
* other threads are waiting for writelock |
571 |
|
|
*/ |
572 |
jsr166 |
1.32 |
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
573 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
574 |
|
|
lock.writeLock().lock(); |
575 |
|
|
Thread t1 = new Thread(new Runnable() { |
576 |
dl |
1.23 |
public void run() { |
577 |
|
|
lock.writeLock().lock(); |
578 |
|
|
lock.writeLock().unlock(); |
579 |
jsr166 |
1.35 |
} |
580 |
|
|
}); |
581 |
|
|
Thread t2 = new Thread(new Runnable() { |
582 |
dl |
1.23 |
public void run() { |
583 |
|
|
lock.writeLock().lock(); |
584 |
|
|
lock.writeLock().unlock(); |
585 |
jsr166 |
1.35 |
} |
586 |
|
|
}); |
587 |
dl |
1.23 |
|
588 |
jsr166 |
1.32 |
t1.start(); |
589 |
|
|
t2.start(); |
590 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
591 |
|
|
assertTrue(lock.isWriteLockedByCurrentThread()); |
592 |
|
|
assertTrue(lock.getWriteHoldCount() == 1); |
593 |
|
|
lock.writeLock().lock(); |
594 |
|
|
assertTrue(lock.getWriteHoldCount() == 2); |
595 |
|
|
lock.writeLock().unlock(); |
596 |
|
|
lock.writeLock().lock(); |
597 |
|
|
lock.writeLock().unlock(); |
598 |
|
|
lock.writeLock().unlock(); |
599 |
|
|
t1.join(MEDIUM_DELAY_MS); |
600 |
|
|
t2.join(MEDIUM_DELAY_MS); |
601 |
|
|
assertTrue(!t1.isAlive()); |
602 |
|
|
assertTrue(!t2.isAlive()); |
603 |
jsr166 |
1.28 |
} |
604 |
dl |
1.23 |
|
605 |
|
|
|
606 |
|
|
/** |
607 |
dl |
1.15 |
* Read tryLock succeeds if readlocked but not writelocked |
608 |
dl |
1.4 |
*/ |
609 |
jsr166 |
1.32 |
public void testTryLockWhenReadLocked() throws InterruptedException { |
610 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
611 |
|
|
lock.readLock().lock(); |
612 |
|
|
Thread t = new Thread(new Runnable() { |
613 |
dl |
1.4 |
public void run() { |
614 |
dl |
1.3 |
threadAssertTrue(lock.readLock().tryLock()); |
615 |
dl |
1.2 |
lock.readLock().unlock(); |
616 |
jsr166 |
1.35 |
} |
617 |
|
|
}); |
618 |
jsr166 |
1.32 |
|
619 |
|
|
t.start(); |
620 |
|
|
t.join(); |
621 |
|
|
lock.readLock().unlock(); |
622 |
jsr166 |
1.28 |
} |
623 |
|
|
|
624 |
dl |
1.2 |
|
625 |
|
|
|
626 |
dl |
1.4 |
/** |
627 |
dl |
1.15 |
* write tryLock fails when readlocked |
628 |
dl |
1.4 |
*/ |
629 |
jsr166 |
1.32 |
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
630 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
631 |
|
|
lock.readLock().lock(); |
632 |
|
|
Thread t = new Thread(new Runnable() { |
633 |
dl |
1.4 |
public void run() { |
634 |
dl |
1.3 |
threadAssertFalse(lock.writeLock().tryLock()); |
635 |
jsr166 |
1.35 |
} |
636 |
|
|
}); |
637 |
jsr166 |
1.32 |
|
638 |
|
|
t.start(); |
639 |
|
|
t.join(); |
640 |
|
|
lock.readLock().unlock(); |
641 |
jsr166 |
1.28 |
} |
642 |
dl |
1.2 |
|
643 |
dl |
1.24 |
|
644 |
|
|
/** |
645 |
|
|
* Fair Read tryLock succeeds if readlocked but not writelocked |
646 |
|
|
*/ |
647 |
jsr166 |
1.32 |
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
648 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
649 |
|
|
lock.readLock().lock(); |
650 |
|
|
Thread t = new Thread(new Runnable() { |
651 |
dl |
1.24 |
public void run() { |
652 |
|
|
threadAssertTrue(lock.readLock().tryLock()); |
653 |
|
|
lock.readLock().unlock(); |
654 |
jsr166 |
1.35 |
} |
655 |
|
|
}); |
656 |
jsr166 |
1.32 |
|
657 |
|
|
t.start(); |
658 |
|
|
t.join(); |
659 |
|
|
lock.readLock().unlock(); |
660 |
jsr166 |
1.28 |
} |
661 |
|
|
|
662 |
dl |
1.24 |
|
663 |
|
|
|
664 |
|
|
/** |
665 |
|
|
* Fair write tryLock fails when readlocked |
666 |
|
|
*/ |
667 |
jsr166 |
1.32 |
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
668 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
669 |
|
|
lock.readLock().lock(); |
670 |
|
|
Thread t = new Thread(new Runnable() { |
671 |
dl |
1.24 |
public void run() { |
672 |
|
|
threadAssertFalse(lock.writeLock().tryLock()); |
673 |
jsr166 |
1.35 |
} |
674 |
|
|
}); |
675 |
jsr166 |
1.32 |
|
676 |
|
|
t.start(); |
677 |
|
|
t.join(); |
678 |
|
|
lock.readLock().unlock(); |
679 |
jsr166 |
1.28 |
} |
680 |
|
|
|
681 |
dl |
1.24 |
|
682 |
dl |
1.2 |
|
683 |
dl |
1.4 |
/** |
684 |
dl |
1.15 |
* write timed tryLock times out if locked |
685 |
dl |
1.4 |
*/ |
686 |
jsr166 |
1.32 |
public void testWriteTryLock_Timeout() throws InterruptedException { |
687 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
688 |
|
|
lock.writeLock().lock(); |
689 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
690 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
691 |
|
|
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
692 |
|
|
}}); |
693 |
jsr166 |
1.32 |
|
694 |
|
|
t.start(); |
695 |
|
|
t.join(); |
696 |
jsr166 |
1.34 |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
697 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
698 |
jsr166 |
1.28 |
} |
699 |
dl |
1.2 |
|
700 |
dl |
1.4 |
/** |
701 |
dl |
1.15 |
* read timed tryLock times out if write-locked |
702 |
dl |
1.4 |
*/ |
703 |
jsr166 |
1.32 |
public void testReadTryLock_Timeout() throws InterruptedException { |
704 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
705 |
|
|
lock.writeLock().lock(); |
706 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
707 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
708 |
|
|
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
709 |
|
|
}}); |
710 |
jsr166 |
1.32 |
|
711 |
|
|
t.start(); |
712 |
|
|
t.join(); |
713 |
jsr166 |
1.34 |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
714 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
715 |
jsr166 |
1.28 |
} |
716 |
dl |
1.2 |
|
717 |
|
|
|
718 |
dl |
1.4 |
/** |
719 |
|
|
* write lockInterruptibly succeeds if lock free else is interruptible |
720 |
|
|
*/ |
721 |
jsr166 |
1.32 |
public void testWriteLockInterruptibly() throws InterruptedException { |
722 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
723 |
jsr166 |
1.32 |
lock.writeLock().lockInterruptibly(); |
724 |
jsr166 |
1.35 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
725 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
726 |
|
|
lock.writeLock().lockInterruptibly(); |
727 |
|
|
}}); |
728 |
jsr166 |
1.32 |
|
729 |
|
|
t.start(); |
730 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
731 |
|
|
t.interrupt(); |
732 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
733 |
|
|
t.join(); |
734 |
|
|
lock.writeLock().unlock(); |
735 |
dl |
1.2 |
} |
736 |
|
|
|
737 |
dl |
1.4 |
/** |
738 |
|
|
* read lockInterruptibly succeeds if lock free else is interruptible |
739 |
|
|
*/ |
740 |
jsr166 |
1.32 |
public void testReadLockInterruptibly() throws InterruptedException { |
741 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
742 |
jsr166 |
1.32 |
lock.writeLock().lockInterruptibly(); |
743 |
jsr166 |
1.35 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
744 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
745 |
|
|
lock.readLock().lockInterruptibly(); |
746 |
|
|
}}); |
747 |
jsr166 |
1.32 |
|
748 |
|
|
t.start(); |
749 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
750 |
|
|
t.interrupt(); |
751 |
|
|
t.join(); |
752 |
|
|
lock.writeLock().unlock(); |
753 |
dl |
1.2 |
} |
754 |
|
|
|
755 |
dl |
1.4 |
/** |
756 |
|
|
* Calling await without holding lock throws IllegalMonitorStateException |
757 |
|
|
*/ |
758 |
jsr166 |
1.32 |
public void testAwait_IllegalMonitor() throws InterruptedException { |
759 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
760 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
761 |
|
|
try { |
762 |
|
|
c.await(); |
763 |
dl |
1.4 |
shouldThrow(); |
764 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
765 |
dl |
1.2 |
} |
766 |
|
|
|
767 |
dl |
1.4 |
/** |
768 |
|
|
* Calling signal without holding lock throws IllegalMonitorStateException |
769 |
|
|
*/ |
770 |
dl |
1.2 |
public void testSignal_IllegalMonitor() { |
771 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
772 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
773 |
|
|
try { |
774 |
|
|
c.signal(); |
775 |
dl |
1.4 |
shouldThrow(); |
776 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
777 |
dl |
1.2 |
} |
778 |
|
|
|
779 |
dl |
1.4 |
/** |
780 |
|
|
* awaitNanos without a signal times out |
781 |
|
|
*/ |
782 |
jsr166 |
1.32 |
public void testAwaitNanos_Timeout() throws InterruptedException { |
783 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
784 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
785 |
jsr166 |
1.32 |
|
786 |
|
|
lock.writeLock().lock(); |
787 |
|
|
long t = c.awaitNanos(100); |
788 |
|
|
assertTrue(t <= 0); |
789 |
|
|
lock.writeLock().unlock(); |
790 |
dl |
1.2 |
} |
791 |
|
|
|
792 |
dl |
1.4 |
|
793 |
|
|
/** |
794 |
|
|
* timed await without a signal times out |
795 |
|
|
*/ |
796 |
jsr166 |
1.34 |
public void testAwait_Timeout() throws InterruptedException { |
797 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
798 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
799 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
800 |
jsr166 |
1.34 |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
801 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
802 |
dl |
1.2 |
} |
803 |
|
|
|
804 |
dl |
1.4 |
/** |
805 |
|
|
* awaitUntil without a signal times out |
806 |
|
|
*/ |
807 |
jsr166 |
1.34 |
public void testAwaitUntil_Timeout() throws InterruptedException { |
808 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
809 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
810 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
811 |
|
|
java.util.Date d = new java.util.Date(); |
812 |
jsr166 |
1.34 |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
813 |
jsr166 |
1.32 |
lock.writeLock().unlock(); |
814 |
dl |
1.2 |
} |
815 |
dl |
1.1 |
|
816 |
dl |
1.4 |
/** |
817 |
|
|
* await returns when signalled |
818 |
|
|
*/ |
819 |
jsr166 |
1.32 |
public void testAwait() throws InterruptedException { |
820 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
821 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
822 |
jsr166 |
1.35 |
Thread t = new Thread(new CheckedRunnable() { |
823 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
824 |
|
|
lock.writeLock().lock(); |
825 |
|
|
c.await(); |
826 |
|
|
lock.writeLock().unlock(); |
827 |
|
|
}}); |
828 |
dl |
1.2 |
|
829 |
jsr166 |
1.32 |
t.start(); |
830 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
831 |
|
|
lock.writeLock().lock(); |
832 |
|
|
c.signal(); |
833 |
|
|
lock.writeLock().unlock(); |
834 |
|
|
t.join(SHORT_DELAY_MS); |
835 |
|
|
assertFalse(t.isAlive()); |
836 |
dl |
1.2 |
} |
837 |
|
|
|
838 |
dl |
1.22 |
/** A helper class for uninterruptible wait tests */ |
839 |
|
|
class UninterruptableThread extends Thread { |
840 |
|
|
private Lock lock; |
841 |
|
|
private Condition c; |
842 |
jsr166 |
1.28 |
|
843 |
dl |
1.22 |
public volatile boolean canAwake = false; |
844 |
|
|
public volatile boolean interrupted = false; |
845 |
|
|
public volatile boolean lockStarted = false; |
846 |
jsr166 |
1.28 |
|
847 |
dl |
1.22 |
public UninterruptableThread(Lock lock, Condition c) { |
848 |
|
|
this.lock = lock; |
849 |
|
|
this.c = c; |
850 |
|
|
} |
851 |
jsr166 |
1.28 |
|
852 |
dl |
1.22 |
public synchronized void run() { |
853 |
|
|
lock.lock(); |
854 |
|
|
lockStarted = true; |
855 |
jsr166 |
1.28 |
|
856 |
dl |
1.22 |
while (!canAwake) { |
857 |
|
|
c.awaitUninterruptibly(); |
858 |
|
|
} |
859 |
jsr166 |
1.28 |
|
860 |
dl |
1.22 |
interrupted = isInterrupted(); |
861 |
|
|
lock.unlock(); |
862 |
|
|
} |
863 |
|
|
} |
864 |
|
|
|
865 |
dl |
1.4 |
/** |
866 |
|
|
* awaitUninterruptibly doesn't abort on interrupt |
867 |
|
|
*/ |
868 |
jsr166 |
1.32 |
public void testAwaitUninterruptibly() throws InterruptedException { |
869 |
dl |
1.22 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
870 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
871 |
dl |
1.22 |
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
872 |
dl |
1.2 |
|
873 |
jsr166 |
1.32 |
thread.start(); |
874 |
dl |
1.22 |
|
875 |
jsr166 |
1.32 |
while (!thread.lockStarted) { |
876 |
|
|
Thread.sleep(100); |
877 |
|
|
} |
878 |
dl |
1.22 |
|
879 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
880 |
|
|
try { |
881 |
|
|
thread.interrupt(); |
882 |
|
|
thread.canAwake = true; |
883 |
|
|
c.signal(); |
884 |
|
|
} finally { |
885 |
|
|
lock.writeLock().unlock(); |
886 |
|
|
} |
887 |
dl |
1.22 |
|
888 |
jsr166 |
1.32 |
thread.join(); |
889 |
|
|
assertTrue(thread.interrupted); |
890 |
|
|
assertFalse(thread.isAlive()); |
891 |
dl |
1.2 |
} |
892 |
|
|
|
893 |
dl |
1.4 |
/** |
894 |
|
|
* await is interruptible |
895 |
|
|
*/ |
896 |
jsr166 |
1.32 |
public void testAwait_Interrupt() throws InterruptedException { |
897 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
898 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
899 |
jsr166 |
1.35 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
900 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
901 |
|
|
lock.writeLock().lock(); |
902 |
|
|
c.await(); |
903 |
|
|
lock.writeLock().unlock(); |
904 |
|
|
}}); |
905 |
dl |
1.2 |
|
906 |
jsr166 |
1.32 |
t.start(); |
907 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
908 |
|
|
t.interrupt(); |
909 |
|
|
t.join(SHORT_DELAY_MS); |
910 |
|
|
assertFalse(t.isAlive()); |
911 |
dl |
1.2 |
} |
912 |
|
|
|
913 |
dl |
1.4 |
/** |
914 |
|
|
* awaitNanos is interruptible |
915 |
|
|
*/ |
916 |
jsr166 |
1.32 |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
917 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
918 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
919 |
jsr166 |
1.35 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
920 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
921 |
|
|
lock.writeLock().lock(); |
922 |
|
|
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
923 |
|
|
lock.writeLock().unlock(); |
924 |
|
|
}}); |
925 |
dl |
1.2 |
|
926 |
jsr166 |
1.32 |
t.start(); |
927 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
928 |
|
|
t.interrupt(); |
929 |
|
|
t.join(SHORT_DELAY_MS); |
930 |
|
|
assertFalse(t.isAlive()); |
931 |
dl |
1.2 |
} |
932 |
dl |
1.1 |
|
933 |
dl |
1.4 |
/** |
934 |
|
|
* awaitUntil is interruptible |
935 |
|
|
*/ |
936 |
jsr166 |
1.32 |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
937 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
938 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
939 |
jsr166 |
1.35 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
940 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
941 |
|
|
lock.writeLock().lock(); |
942 |
|
|
java.util.Date d = new java.util.Date(); |
943 |
|
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
944 |
|
|
lock.writeLock().unlock(); |
945 |
|
|
}}); |
946 |
dl |
1.2 |
|
947 |
jsr166 |
1.32 |
t.start(); |
948 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
949 |
|
|
t.interrupt(); |
950 |
|
|
t.join(SHORT_DELAY_MS); |
951 |
|
|
assertFalse(t.isAlive()); |
952 |
dl |
1.2 |
} |
953 |
|
|
|
954 |
dl |
1.4 |
/** |
955 |
|
|
* signalAll wakes up all threads |
956 |
|
|
*/ |
957 |
jsr166 |
1.32 |
public void testSignalAll() throws InterruptedException { |
958 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
959 |
dl |
1.2 |
final Condition c = lock.writeLock().newCondition(); |
960 |
jsr166 |
1.35 |
Thread t1 = new Thread(new CheckedRunnable() { |
961 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
962 |
|
|
lock.writeLock().lock(); |
963 |
|
|
c.await(); |
964 |
|
|
lock.writeLock().unlock(); |
965 |
|
|
}}); |
966 |
|
|
|
967 |
jsr166 |
1.35 |
Thread t2 = new Thread(new CheckedRunnable() { |
968 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
969 |
|
|
lock.writeLock().lock(); |
970 |
|
|
c.await(); |
971 |
|
|
lock.writeLock().unlock(); |
972 |
|
|
}}); |
973 |
dl |
1.2 |
|
974 |
jsr166 |
1.32 |
t1.start(); |
975 |
|
|
t2.start(); |
976 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
977 |
|
|
lock.writeLock().lock(); |
978 |
|
|
c.signalAll(); |
979 |
|
|
lock.writeLock().unlock(); |
980 |
|
|
t1.join(SHORT_DELAY_MS); |
981 |
|
|
t2.join(SHORT_DELAY_MS); |
982 |
|
|
assertFalse(t1.isAlive()); |
983 |
|
|
assertFalse(t2.isAlive()); |
984 |
dl |
1.2 |
} |
985 |
|
|
|
986 |
dl |
1.4 |
/** |
987 |
|
|
* A serialized lock deserializes as unlocked |
988 |
|
|
*/ |
989 |
jsr166 |
1.32 |
public void testSerialization() throws Exception { |
990 |
dl |
1.2 |
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
991 |
|
|
l.readLock().lock(); |
992 |
|
|
l.readLock().unlock(); |
993 |
|
|
|
994 |
jsr166 |
1.32 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
995 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
996 |
|
|
out.writeObject(l); |
997 |
|
|
out.close(); |
998 |
|
|
|
999 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1000 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1001 |
|
|
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
1002 |
|
|
r.readLock().lock(); |
1003 |
|
|
r.readLock().unlock(); |
1004 |
dl |
1.1 |
} |
1005 |
dl |
1.2 |
|
1006 |
dl |
1.6 |
/** |
1007 |
dl |
1.13 |
* hasQueuedThreads reports whether there are waiting threads |
1008 |
|
|
*/ |
1009 |
jsr166 |
1.32 |
public void testhasQueuedThreads() throws InterruptedException { |
1010 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1011 |
dl |
1.13 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1012 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1013 |
jsr166 |
1.32 |
assertFalse(lock.hasQueuedThreads()); |
1014 |
|
|
lock.writeLock().lock(); |
1015 |
|
|
t1.start(); |
1016 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1017 |
|
|
assertTrue(lock.hasQueuedThreads()); |
1018 |
|
|
t2.start(); |
1019 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1020 |
|
|
assertTrue(lock.hasQueuedThreads()); |
1021 |
|
|
t1.interrupt(); |
1022 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1023 |
|
|
assertTrue(lock.hasQueuedThreads()); |
1024 |
|
|
lock.writeLock().unlock(); |
1025 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1026 |
|
|
assertFalse(lock.hasQueuedThreads()); |
1027 |
|
|
t1.join(); |
1028 |
|
|
t2.join(); |
1029 |
jsr166 |
1.28 |
} |
1030 |
dl |
1.13 |
|
1031 |
|
|
/** |
1032 |
dl |
1.19 |
* hasQueuedThread(null) throws NPE |
1033 |
|
|
*/ |
1034 |
jsr166 |
1.28 |
public void testHasQueuedThreadNPE() { |
1035 |
jsr166 |
1.35 |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1036 |
dl |
1.19 |
try { |
1037 |
|
|
sync.hasQueuedThread(null); |
1038 |
|
|
shouldThrow(); |
1039 |
jsr166 |
1.33 |
} catch (NullPointerException success) {} |
1040 |
dl |
1.19 |
} |
1041 |
|
|
|
1042 |
|
|
/** |
1043 |
|
|
* hasQueuedThread reports whether a thread is queued. |
1044 |
|
|
*/ |
1045 |
jsr166 |
1.32 |
public void testHasQueuedThread() throws InterruptedException { |
1046 |
jsr166 |
1.35 |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1047 |
dl |
1.19 |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1048 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1049 |
jsr166 |
1.32 |
assertFalse(sync.hasQueuedThread(t1)); |
1050 |
|
|
assertFalse(sync.hasQueuedThread(t2)); |
1051 |
|
|
sync.writeLock().lock(); |
1052 |
|
|
t1.start(); |
1053 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1054 |
|
|
assertTrue(sync.hasQueuedThread(t1)); |
1055 |
|
|
t2.start(); |
1056 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1057 |
|
|
assertTrue(sync.hasQueuedThread(t1)); |
1058 |
|
|
assertTrue(sync.hasQueuedThread(t2)); |
1059 |
|
|
t1.interrupt(); |
1060 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1061 |
|
|
assertFalse(sync.hasQueuedThread(t1)); |
1062 |
|
|
assertTrue(sync.hasQueuedThread(t2)); |
1063 |
|
|
sync.writeLock().unlock(); |
1064 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1065 |
|
|
assertFalse(sync.hasQueuedThread(t1)); |
1066 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1067 |
|
|
assertFalse(sync.hasQueuedThread(t2)); |
1068 |
|
|
t1.join(); |
1069 |
|
|
t2.join(); |
1070 |
jsr166 |
1.28 |
} |
1071 |
dl |
1.19 |
|
1072 |
|
|
|
1073 |
|
|
/** |
1074 |
dl |
1.6 |
* getQueueLength reports number of waiting threads |
1075 |
|
|
*/ |
1076 |
jsr166 |
1.32 |
public void testGetQueueLength() throws InterruptedException { |
1077 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1078 |
dl |
1.6 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1079 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1080 |
jsr166 |
1.32 |
assertEquals(0, lock.getQueueLength()); |
1081 |
|
|
lock.writeLock().lock(); |
1082 |
|
|
t1.start(); |
1083 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1084 |
|
|
assertEquals(1, lock.getQueueLength()); |
1085 |
|
|
t2.start(); |
1086 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1087 |
|
|
assertEquals(2, lock.getQueueLength()); |
1088 |
|
|
t1.interrupt(); |
1089 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1090 |
|
|
assertEquals(1, lock.getQueueLength()); |
1091 |
|
|
lock.writeLock().unlock(); |
1092 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1093 |
|
|
assertEquals(0, lock.getQueueLength()); |
1094 |
|
|
t1.join(); |
1095 |
|
|
t2.join(); |
1096 |
jsr166 |
1.28 |
} |
1097 |
dl |
1.6 |
|
1098 |
|
|
/** |
1099 |
|
|
* getQueuedThreads includes waiting threads |
1100 |
|
|
*/ |
1101 |
jsr166 |
1.32 |
public void testGetQueuedThreads() throws InterruptedException { |
1102 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1103 |
dl |
1.6 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1104 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1105 |
jsr166 |
1.32 |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1106 |
|
|
lock.writeLock().lock(); |
1107 |
|
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
1108 |
|
|
t1.start(); |
1109 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1110 |
|
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
1111 |
|
|
t2.start(); |
1112 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1113 |
|
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
1114 |
|
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
1115 |
|
|
t1.interrupt(); |
1116 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1117 |
|
|
assertFalse(lock.getQueuedThreads().contains(t1)); |
1118 |
|
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
1119 |
|
|
lock.writeLock().unlock(); |
1120 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1121 |
|
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
1122 |
|
|
t1.join(); |
1123 |
|
|
t2.join(); |
1124 |
jsr166 |
1.28 |
} |
1125 |
dl |
1.6 |
|
1126 |
|
|
/** |
1127 |
dl |
1.14 |
* hasWaiters throws NPE if null |
1128 |
|
|
*/ |
1129 |
|
|
public void testHasWaitersNPE() { |
1130 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1131 |
dl |
1.14 |
try { |
1132 |
|
|
lock.hasWaiters(null); |
1133 |
|
|
shouldThrow(); |
1134 |
jsr166 |
1.32 |
} catch (NullPointerException success) {} |
1135 |
dl |
1.14 |
} |
1136 |
|
|
|
1137 |
|
|
/** |
1138 |
|
|
* getWaitQueueLength throws NPE if null |
1139 |
|
|
*/ |
1140 |
|
|
public void testGetWaitQueueLengthNPE() { |
1141 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1142 |
dl |
1.14 |
try { |
1143 |
|
|
lock.getWaitQueueLength(null); |
1144 |
|
|
shouldThrow(); |
1145 |
jsr166 |
1.32 |
} catch (NullPointerException success) {} |
1146 |
dl |
1.14 |
} |
1147 |
|
|
|
1148 |
|
|
|
1149 |
|
|
/** |
1150 |
|
|
* getWaitingThreads throws NPE if null |
1151 |
|
|
*/ |
1152 |
|
|
public void testGetWaitingThreadsNPE() { |
1153 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1154 |
dl |
1.14 |
try { |
1155 |
|
|
lock.getWaitingThreads(null); |
1156 |
|
|
shouldThrow(); |
1157 |
jsr166 |
1.32 |
} catch (NullPointerException success) {} |
1158 |
dl |
1.14 |
} |
1159 |
|
|
|
1160 |
|
|
/** |
1161 |
dl |
1.13 |
* hasWaiters throws IAE if not owned |
1162 |
|
|
*/ |
1163 |
|
|
public void testHasWaitersIAE() { |
1164 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1165 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1166 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1167 |
dl |
1.13 |
try { |
1168 |
|
|
lock2.hasWaiters(c); |
1169 |
|
|
shouldThrow(); |
1170 |
jsr166 |
1.32 |
} catch (IllegalArgumentException success) {} |
1171 |
dl |
1.13 |
} |
1172 |
|
|
|
1173 |
|
|
/** |
1174 |
|
|
* hasWaiters throws IMSE if not locked |
1175 |
|
|
*/ |
1176 |
|
|
public void testHasWaitersIMSE() { |
1177 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1178 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1179 |
dl |
1.13 |
try { |
1180 |
|
|
lock.hasWaiters(c); |
1181 |
|
|
shouldThrow(); |
1182 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
1183 |
dl |
1.13 |
} |
1184 |
|
|
|
1185 |
|
|
|
1186 |
|
|
/** |
1187 |
|
|
* getWaitQueueLength throws IAE if not owned |
1188 |
|
|
*/ |
1189 |
|
|
public void testGetWaitQueueLengthIAE() { |
1190 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1191 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1192 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1193 |
dl |
1.13 |
try { |
1194 |
|
|
lock2.getWaitQueueLength(c); |
1195 |
|
|
shouldThrow(); |
1196 |
jsr166 |
1.32 |
} catch (IllegalArgumentException success) {} |
1197 |
dl |
1.13 |
} |
1198 |
|
|
|
1199 |
|
|
/** |
1200 |
|
|
* getWaitQueueLength throws IMSE if not locked |
1201 |
|
|
*/ |
1202 |
|
|
public void testGetWaitQueueLengthIMSE() { |
1203 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1204 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1205 |
dl |
1.13 |
try { |
1206 |
|
|
lock.getWaitQueueLength(c); |
1207 |
|
|
shouldThrow(); |
1208 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
1209 |
dl |
1.13 |
} |
1210 |
|
|
|
1211 |
|
|
|
1212 |
|
|
/** |
1213 |
|
|
* getWaitingThreads throws IAE if not owned |
1214 |
|
|
*/ |
1215 |
|
|
public void testGetWaitingThreadsIAE() { |
1216 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1217 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1218 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1219 |
dl |
1.13 |
try { |
1220 |
|
|
lock2.getWaitingThreads(c); |
1221 |
|
|
shouldThrow(); |
1222 |
jsr166 |
1.32 |
} catch (IllegalArgumentException success) {} |
1223 |
dl |
1.13 |
} |
1224 |
|
|
|
1225 |
|
|
/** |
1226 |
|
|
* getWaitingThreads throws IMSE if not locked |
1227 |
|
|
*/ |
1228 |
|
|
public void testGetWaitingThreadsIMSE() { |
1229 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1230 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1231 |
dl |
1.13 |
try { |
1232 |
|
|
lock.getWaitingThreads(c); |
1233 |
|
|
shouldThrow(); |
1234 |
jsr166 |
1.32 |
} catch (IllegalMonitorStateException success) {} |
1235 |
dl |
1.13 |
} |
1236 |
|
|
|
1237 |
|
|
|
1238 |
|
|
/** |
1239 |
dl |
1.6 |
* hasWaiters returns true when a thread is waiting, else false |
1240 |
|
|
*/ |
1241 |
jsr166 |
1.32 |
public void testHasWaiters() throws InterruptedException { |
1242 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1243 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1244 |
jsr166 |
1.35 |
Thread t = new Thread(new CheckedRunnable() { |
1245 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
1246 |
|
|
lock.writeLock().lock(); |
1247 |
|
|
threadAssertFalse(lock.hasWaiters(c)); |
1248 |
|
|
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1249 |
|
|
c.await(); |
1250 |
|
|
lock.writeLock().unlock(); |
1251 |
|
|
}}); |
1252 |
dl |
1.6 |
|
1253 |
jsr166 |
1.32 |
t.start(); |
1254 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1255 |
|
|
lock.writeLock().lock(); |
1256 |
|
|
assertTrue(lock.hasWaiters(c)); |
1257 |
|
|
assertEquals(1, lock.getWaitQueueLength(c)); |
1258 |
|
|
c.signal(); |
1259 |
|
|
lock.writeLock().unlock(); |
1260 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1261 |
|
|
lock.writeLock().lock(); |
1262 |
|
|
assertFalse(lock.hasWaiters(c)); |
1263 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
1264 |
|
|
lock.writeLock().unlock(); |
1265 |
|
|
t.join(SHORT_DELAY_MS); |
1266 |
|
|
assertFalse(t.isAlive()); |
1267 |
dl |
1.6 |
} |
1268 |
|
|
|
1269 |
|
|
/** |
1270 |
|
|
* getWaitQueueLength returns number of waiting threads |
1271 |
|
|
*/ |
1272 |
jsr166 |
1.32 |
public void testGetWaitQueueLength() throws InterruptedException { |
1273 |
jsr166 |
1.35 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1274 |
jsr166 |
1.31 |
final Condition c = lock.writeLock().newCondition(); |
1275 |
jsr166 |
1.35 |
Thread t = new Thread(new CheckedRunnable() { |
1276 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
1277 |
|
|
lock.writeLock().lock(); |
1278 |
|
|
threadAssertFalse(lock.hasWaiters(c)); |
1279 |
|
|
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1280 |
|
|
c.await(); |
1281 |
|
|
lock.writeLock().unlock(); |
1282 |
|
|
}}); |
1283 |
dl |
1.13 |
|
1284 |
jsr166 |
1.32 |
t.start(); |
1285 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1286 |
|
|
lock.writeLock().lock(); |
1287 |
|
|
assertTrue(lock.hasWaiters(c)); |
1288 |
|
|
assertEquals(1, lock.getWaitQueueLength(c)); |
1289 |
|
|
c.signal(); |
1290 |
|
|
lock.writeLock().unlock(); |
1291 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1292 |
|
|
lock.writeLock().lock(); |
1293 |
|
|
assertFalse(lock.hasWaiters(c)); |
1294 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
1295 |
|
|
lock.writeLock().unlock(); |
1296 |
|
|
t.join(SHORT_DELAY_MS); |
1297 |
|
|
assertFalse(t.isAlive()); |
1298 |
dl |
1.13 |
} |
1299 |
|
|
|
1300 |
|
|
|
1301 |
|
|
/** |
1302 |
|
|
* getWaitingThreads returns only and all waiting threads |
1303 |
|
|
*/ |
1304 |
jsr166 |
1.32 |
public void testGetWaitingThreads() throws InterruptedException { |
1305 |
jsr166 |
1.35 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1306 |
dl |
1.13 |
final Condition c = lock.writeLock().newCondition(); |
1307 |
jsr166 |
1.35 |
Thread t1 = new Thread(new CheckedRunnable() { |
1308 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
1309 |
|
|
lock.writeLock().lock(); |
1310 |
|
|
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1311 |
|
|
c.await(); |
1312 |
|
|
lock.writeLock().unlock(); |
1313 |
|
|
}}); |
1314 |
|
|
|
1315 |
jsr166 |
1.35 |
Thread t2 = new Thread(new CheckedRunnable() { |
1316 |
jsr166 |
1.33 |
public void realRun() throws InterruptedException { |
1317 |
|
|
lock.writeLock().lock(); |
1318 |
|
|
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1319 |
|
|
c.await(); |
1320 |
|
|
lock.writeLock().unlock(); |
1321 |
|
|
}}); |
1322 |
dl |
1.6 |
|
1323 |
jsr166 |
1.32 |
lock.writeLock().lock(); |
1324 |
|
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1325 |
|
|
lock.writeLock().unlock(); |
1326 |
|
|
t1.start(); |
1327 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1328 |
|
|
t2.start(); |
1329 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1330 |
|
|
lock.writeLock().lock(); |
1331 |
|
|
assertTrue(lock.hasWaiters(c)); |
1332 |
|
|
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1333 |
|
|
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1334 |
|
|
c.signalAll(); |
1335 |
|
|
lock.writeLock().unlock(); |
1336 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1337 |
|
|
lock.writeLock().lock(); |
1338 |
|
|
assertFalse(lock.hasWaiters(c)); |
1339 |
|
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1340 |
|
|
lock.writeLock().unlock(); |
1341 |
|
|
t1.join(SHORT_DELAY_MS); |
1342 |
|
|
t2.join(SHORT_DELAY_MS); |
1343 |
|
|
assertFalse(t1.isAlive()); |
1344 |
|
|
assertFalse(t2.isAlive()); |
1345 |
dl |
1.6 |
} |
1346 |
dl |
1.13 |
|
1347 |
dl |
1.18 |
/** |
1348 |
|
|
* toString indicates current lock state |
1349 |
|
|
*/ |
1350 |
|
|
public void testToString() { |
1351 |
|
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1352 |
|
|
String us = lock.toString(); |
1353 |
|
|
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1354 |
|
|
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1355 |
|
|
lock.writeLock().lock(); |
1356 |
|
|
String ws = lock.toString(); |
1357 |
|
|
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1358 |
|
|
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1359 |
|
|
lock.writeLock().unlock(); |
1360 |
|
|
lock.readLock().lock(); |
1361 |
|
|
lock.readLock().lock(); |
1362 |
|
|
String rs = lock.toString(); |
1363 |
|
|
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1364 |
|
|
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1365 |
|
|
} |
1366 |
|
|
|
1367 |
|
|
/** |
1368 |
|
|
* readLock.toString indicates current lock state |
1369 |
|
|
*/ |
1370 |
|
|
public void testReadLockToString() { |
1371 |
|
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1372 |
|
|
String us = lock.readLock().toString(); |
1373 |
|
|
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1374 |
|
|
lock.readLock().lock(); |
1375 |
|
|
lock.readLock().lock(); |
1376 |
|
|
String rs = lock.readLock().toString(); |
1377 |
|
|
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1378 |
|
|
} |
1379 |
|
|
|
1380 |
|
|
/** |
1381 |
|
|
* writeLock.toString indicates current lock state |
1382 |
|
|
*/ |
1383 |
|
|
public void testWriteLockToString() { |
1384 |
|
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1385 |
|
|
String us = lock.writeLock().toString(); |
1386 |
|
|
assertTrue(us.indexOf("Unlocked") >= 0); |
1387 |
|
|
lock.writeLock().lock(); |
1388 |
|
|
String ls = lock.writeLock().toString(); |
1389 |
|
|
assertTrue(ls.indexOf("Locked") >= 0); |
1390 |
|
|
} |
1391 |
|
|
|
1392 |
dl |
1.1 |
} |