ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.58
Committed: Sat May 7 14:43:13 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.57: +180 -101 lines
Log Message:
improve infrastructure; remove delay(); add tests of multiple competing locking threads

File Contents

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