ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.37 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.56 by jsr166, Tue May 3 06:08:49 2011 UTC

# Line 1 | Line 1
1   /*
2   * 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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 + import java.util.concurrent.atomic.AtomicBoolean;
11   import java.util.concurrent.locks.*;
12   import java.util.concurrent.*;
13   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 15 | Line 16 | import java.util.*;
16  
17   public class ReentrantReadWriteLockTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22          return new TestSuite(ReentrantReadWriteLockTest.class);
# Line 32 | Line 33 | public class ReentrantReadWriteLockTest
33          }
34      }
35  
35
36      /**
37       * A runnable calling lockInterruptibly that expects to be
38       * interrupted
# Line 59 | Line 59 | public class ReentrantReadWriteLockTest
59      }
60  
61      /**
62 +     * Releases write lock, checking that it had a hold count of 1.
63 +     */
64 +    void releaseWriteLock(ReentrantReadWriteLock lock) {
65 +        ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
66 +        assertTrue(writeLock.isHeldByCurrentThread());
67 +        writeLock.unlock();
68 +        assertFalse(writeLock.isHeldByCurrentThread());
69 +    }
70 +
71 +    /**
72 +     * Spin-waits until lock.hasQueuedThread(t) becomes true.
73 +     */
74 +    void waitForQueuedThread(ReentrantReadWriteLock lock, Thread t) {
75 +        long startTime = System.nanoTime();
76 +        while (!lock.hasQueuedThread(t)) {
77 +            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
78 +                throw new AssertionError("timed out");
79 +            Thread.yield();
80 +        }
81 +    }
82 +
83 +    /**
84 +     * Checks that lock is not write-locked.
85 +     */
86 +    void assertNotWriteLocked(ReentrantReadWriteLock lock) {
87 +        assertFalse(lock.isWriteLocked());
88 +        assertFalse(lock.isWriteLockedByCurrentThread());
89 +        assertEquals(0, lock.getWriteHoldCount());
90 +    }
91 +
92 +    /**
93 +     * Checks that condition c has no waiters.
94 +     */
95 +    void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
96 +        assertHasWaiters(lock, c, new Thread[] {});
97 +    }
98 +
99 +    /**
100 +     * Checks that condition c has exactly the given waiter threads.
101 +     */
102 +    void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c,
103 +                          Thread... threads) {
104 +        lock.writeLock().lock();
105 +        assertEquals(threads.length > 0, lock.hasWaiters(c));
106 +        assertEquals(threads.length, lock.getWaitQueueLength(c));
107 +        assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
108 +        assertEquals(threads.length, lock.getWaitingThreads(c).size());
109 +        assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
110 +                     new HashSet<Thread>(Arrays.asList(threads)));
111 +        lock.writeLock().unlock();
112 +    }
113 +
114 +    /**
115       * Constructor sets given fairness, and is in unlocked state
116       */
117      public void testConstructor() {
# Line 101 | Line 154 | public class ReentrantReadWriteLockTest
154          assertEquals(0, rl.getReadLockCount());
155      }
156  
104
157      /**
158       * locking an unlocked fair lock succeeds
159       */
# Line 172 | Line 224 | public class ReentrantReadWriteLockTest
224          }
225      }
226  
175
227      /**
228       * write-unlocking an unlocked lock throws IllegalMonitorStateException
229       */
230 <    public void testUnlock_IllegalMonitorStateException() {
230 >    public void testWriteUnlock_IllegalMonitorStateException() {
231          ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
232          try {
233              rl.writeLock().unlock();
# Line 184 | Line 235 | public class ReentrantReadWriteLockTest
235          } catch (IllegalMonitorStateException success) {}
236      }
237  
238 +    /**
239 +     * read-unlocking an unlocked lock throws IllegalMonitorStateException
240 +     */
241 +    public void testReadUnlock_IllegalMonitorStateException() {
242 +        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
243 +        try {
244 +            rl.readLock().unlock();
245 +            shouldThrow();
246 +        } catch (IllegalMonitorStateException success) {}
247 +    }
248  
249      /**
250       * write-lockInterruptibly is interruptible
251       */
252      public void testWriteLockInterruptibly_Interrupted() throws Exception {
253          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
254 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
254 >        lock.writeLock().lock();
255 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
256              public void realRun() throws InterruptedException {
257                  lock.writeLock().lockInterruptibly();
196                lock.writeLock().unlock();
197                lock.writeLock().lockInterruptibly();
198                lock.writeLock().unlock();
258              }});
259  
260 <        lock.writeLock().lock();
202 <        t.start();
203 <        Thread.sleep(SHORT_DELAY_MS);
260 >        waitForQueuedThread(lock, t);
261          t.interrupt();
262 <        Thread.sleep(SHORT_DELAY_MS);
263 <        lock.writeLock().unlock();
207 <        t.join();
262 >        awaitTermination(t);
263 >        releaseWriteLock(lock);
264      }
265  
266      /**
# Line 213 | Line 269 | public class ReentrantReadWriteLockTest
269      public void testWriteTryLock_Interrupted() throws InterruptedException {
270          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
271          lock.writeLock().lock();
272 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
272 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
273              public void realRun() throws InterruptedException {
274 <                lock.writeLock().tryLock(1000,MILLISECONDS);
274 >                lock.writeLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
275              }});
276  
277 <        t.start();
277 >        waitForQueuedThread(lock, t);
278          t.interrupt();
279 <        lock.writeLock().unlock();
280 <        t.join();
279 >        awaitTermination(t);
280 >        releaseWriteLock(lock);
281      }
282  
283      /**
# Line 230 | Line 286 | public class ReentrantReadWriteLockTest
286      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
287          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
288          lock.writeLock().lock();
289 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
289 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
290              public void realRun() throws InterruptedException {
291                  lock.readLock().lockInterruptibly();
292              }});
293  
294 <        t.start();
239 <        Thread.sleep(SHORT_DELAY_MS);
294 >        waitForQueuedThread(lock, t);
295          t.interrupt();
296 <        Thread.sleep(SHORT_DELAY_MS);
297 <        lock.writeLock().unlock();
243 <        t.join();
296 >        awaitTermination(t);
297 >        releaseWriteLock(lock);
298      }
299  
300      /**
# Line 249 | Line 303 | public class ReentrantReadWriteLockTest
303      public void testReadTryLock_Interrupted() throws InterruptedException {
304          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
305          lock.writeLock().lock();
306 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
306 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
307              public void realRun() throws InterruptedException {
308 <                lock.readLock().tryLock(1000,MILLISECONDS);
308 >                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
309              }});
310  
311 <        t.start();
311 >        waitForQueuedThread(lock, t);
312          t.interrupt();
313 <        t.join();
313 >        awaitTermination(t);
314 >        releaseWriteLock(lock);
315      }
316  
262
317      /**
318       * write-tryLock fails if locked
319       */
320      public void testWriteTryLockWhenLocked() throws InterruptedException {
321          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
322          lock.writeLock().lock();
323 <        Thread t = new Thread(new CheckedRunnable() {
323 >        Thread t = newStartedThread(new CheckedRunnable() {
324              public void realRun() {
325 <                threadAssertFalse(lock.writeLock().tryLock());
325 >                assertFalse(lock.writeLock().tryLock());
326              }});
327  
328 <        t.start();
329 <        t.join();
276 <        lock.writeLock().unlock();
328 >        awaitTermination(t);
329 >        releaseWriteLock(lock);
330      }
331  
332      /**
# Line 282 | Line 335 | public class ReentrantReadWriteLockTest
335      public void testReadTryLockWhenLocked() throws InterruptedException {
336          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
337          lock.writeLock().lock();
338 <        Thread t = new Thread(new CheckedRunnable() {
338 >        Thread t = newStartedThread(new CheckedRunnable() {
339              public void realRun() {
340 <                threadAssertFalse(lock.readLock().tryLock());
340 >                assertFalse(lock.readLock().tryLock());
341              }});
342  
343 <        t.start();
344 <        t.join();
292 <        lock.writeLock().unlock();
343 >        awaitTermination(t);
344 >        releaseWriteLock(lock);
345      }
346  
347      /**
# Line 298 | Line 350 | public class ReentrantReadWriteLockTest
350      public void testMultipleReadLocks() throws InterruptedException {
351          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
352          lock.readLock().lock();
353 <        Thread t = new Thread(new CheckedRunnable() {
353 >        Thread t = newStartedThread(new CheckedRunnable() {
354              public void realRun() {
355 <                threadAssertTrue(lock.readLock().tryLock());
355 >                assertTrue(lock.readLock().tryLock());
356                  lock.readLock().unlock();
357              }});
358  
359 <        t.start();
308 <        t.join();
359 >        awaitTermination(t);
360          lock.readLock().unlock();
361      }
362  
363      /**
364 <     * A writelock succeeds after reading threads unlock
364 >     * A writelock succeeds only after reading threads unlock
365       */
366      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
367          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
368          lock.readLock().lock();
369 <        Thread t1 = new Thread(new CheckedRunnable() {
369 >        Thread t1 = newStartedThread(new CheckedRunnable() {
370              public void realRun() {
371                  lock.readLock().lock();
372                  lock.readLock().unlock();
373              }});
374 <        Thread t2 = new Thread(new CheckedRunnable() {
374 >        Thread t2 = newStartedThread(new CheckedRunnable() {
375              public void realRun() {
376                  lock.writeLock().lock();
377                  lock.writeLock().unlock();
378              }});
379  
380 <        t1.start();
381 <        t2.start();
382 <        Thread.sleep(SHORT_DELAY_MS);
380 >        awaitTermination(t1);
381 >        waitForQueuedThread(lock, t2);
382 >        assertNotWriteLocked(lock);
383          lock.readLock().unlock();
384 <        t1.join(MEDIUM_DELAY_MS);
334 <        t2.join(MEDIUM_DELAY_MS);
335 <        assertTrue(!t1.isAlive());
336 <        assertTrue(!t2.isAlive());
384 >        awaitTermination(t2);
385      }
386  
387      /**
388 <     * Readlocks succeed after a writing thread unlocks
388 >     * Readlocks succeed only after a writing thread unlocks
389       */
390      public void testReadAfterWriteLock() throws InterruptedException {
391          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
392          lock.writeLock().lock();
393 <        Thread t1 = new Thread(new CheckedRunnable() {
393 >        Thread t1 = newStartedThread(new CheckedRunnable() {
394              public void realRun() {
395                  lock.readLock().lock();
396                  lock.readLock().unlock();
397              }});
398 <        Thread t2 = new Thread(new CheckedRunnable() {
398 >        Thread t2 = newStartedThread(new CheckedRunnable() {
399              public void realRun() {
400                  lock.readLock().lock();
401                  lock.readLock().unlock();
402              }});
403  
404 <        t1.start();
405 <        t2.start();
406 <        Thread.sleep(SHORT_DELAY_MS);
407 <        lock.writeLock().unlock();
408 <        t1.join(MEDIUM_DELAY_MS);
361 <        t2.join(MEDIUM_DELAY_MS);
362 <        assertTrue(!t1.isAlive());
363 <        assertTrue(!t2.isAlive());
404 >        waitForQueuedThread(lock, t1);
405 >        waitForQueuedThread(lock, t2);
406 >        releaseWriteLock(lock);
407 >        awaitTermination(t1);
408 >        awaitTermination(t2);
409      }
410  
411      /**
# Line 381 | Line 426 | public class ReentrantReadWriteLockTest
426      public void testReadHoldingWriteLock2() throws InterruptedException {
427          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
428          lock.writeLock().lock();
429 <        Thread t1 = new Thread(new CheckedRunnable() {
429 >        lock.readLock().lock();
430 >        lock.readLock().unlock();
431 >
432 >        Thread t1 = newStartedThread(new CheckedRunnable() {
433              public void realRun() {
434                  lock.readLock().lock();
435                  lock.readLock().unlock();
436              }});
437 <        Thread t2 = new Thread(new CheckedRunnable() {
437 >        Thread t2 = newStartedThread(new CheckedRunnable() {
438              public void realRun() {
439                  lock.readLock().lock();
440                  lock.readLock().unlock();
441              }});
442  
443 <        t1.start();
444 <        t2.start();
445 <        lock.readLock().lock();
398 <        lock.readLock().unlock();
399 <        Thread.sleep(SHORT_DELAY_MS);
443 >        waitForQueuedThread(lock, t1);
444 >        waitForQueuedThread(lock, t2);
445 >        assertTrue(lock.isWriteLockedByCurrentThread());
446          lock.readLock().lock();
447          lock.readLock().unlock();
448 <        lock.writeLock().unlock();
449 <        t1.join(MEDIUM_DELAY_MS);
450 <        t2.join(MEDIUM_DELAY_MS);
405 <        assertTrue(!t1.isAlive());
406 <        assertTrue(!t2.isAlive());
448 >        releaseWriteLock(lock);
449 >        awaitTermination(t1);
450 >        awaitTermination(t2);
451      }
452  
453      /**
454 <     *  Read lock succeeds if write locked by current thread even if
454 >     * Read lock succeeds if write locked by current thread even if
455       * other threads are waiting for writelock
456       */
457      public void testReadHoldingWriteLock3() throws InterruptedException {
458          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
459          lock.writeLock().lock();
460 <        Thread t1 = new Thread(new CheckedRunnable() {
460 >        lock.readLock().lock();
461 >        lock.readLock().unlock();
462 >
463 >        Thread t1 = newStartedThread(new CheckedRunnable() {
464              public void realRun() {
465                  lock.writeLock().lock();
466                  lock.writeLock().unlock();
467              }});
468 <        Thread t2 = new Thread(new CheckedRunnable() {
468 >        Thread t2 = newStartedThread(new CheckedRunnable() {
469              public void realRun() {
470                  lock.writeLock().lock();
471                  lock.writeLock().unlock();
472              }});
473  
474 <        t1.start();
475 <        t2.start();
476 <        lock.readLock().lock();
430 <        lock.readLock().unlock();
431 <        Thread.sleep(SHORT_DELAY_MS);
474 >        waitForQueuedThread(lock, t1);
475 >        waitForQueuedThread(lock, t2);
476 >        assertTrue(lock.isWriteLockedByCurrentThread());
477          lock.readLock().lock();
478          lock.readLock().unlock();
479 <        lock.writeLock().unlock();
480 <        t1.join(MEDIUM_DELAY_MS);
481 <        t2.join(MEDIUM_DELAY_MS);
437 <        assertTrue(!t1.isAlive());
438 <        assertTrue(!t2.isAlive());
479 >        releaseWriteLock(lock);
480 >        awaitTermination(t1);
481 >        awaitTermination(t2);
482      }
483  
441
484      /**
485 <     *  Write lock succeeds if write locked by current thread even if
485 >     * Write lock succeeds if write locked by current thread even if
486       * other threads are waiting for writelock
487       */
488      public void testWriteHoldingWriteLock4() throws InterruptedException {
489          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
490          lock.writeLock().lock();
491 <        Thread t1 = new Thread(new CheckedRunnable() {
491 >        lock.writeLock().lock();
492 >        lock.writeLock().unlock();
493 >
494 >        Thread t1 = newStartedThread(new CheckedRunnable() {
495              public void realRun() {
496                  lock.writeLock().lock();
497                  lock.writeLock().unlock();
498              }});
499 <        Thread t2 = new Thread(new CheckedRunnable() {
499 >        Thread t2 = newStartedThread(new CheckedRunnable() {
500              public void realRun() {
501                  lock.writeLock().lock();
502                  lock.writeLock().unlock();
503              }});
504  
505 <        t1.start();
506 <        t2.start();
507 <        lock.writeLock().lock();
508 <        lock.writeLock().unlock();
464 <        Thread.sleep(SHORT_DELAY_MS);
505 >        waitForQueuedThread(lock, t1);
506 >        waitForQueuedThread(lock, t2);
507 >        assertTrue(lock.isWriteLockedByCurrentThread());
508 >        assertEquals(1, lock.getWriteHoldCount());
509          lock.writeLock().lock();
510 +        assertTrue(lock.isWriteLockedByCurrentThread());
511 +        assertEquals(2, lock.getWriteHoldCount());
512          lock.writeLock().unlock();
513 <        lock.writeLock().unlock();
514 <        t1.join(MEDIUM_DELAY_MS);
515 <        t2.join(MEDIUM_DELAY_MS);
470 <        assertTrue(!t1.isAlive());
471 <        assertTrue(!t2.isAlive());
513 >        releaseWriteLock(lock);
514 >        awaitTermination(t1);
515 >        awaitTermination(t2);
516      }
517  
474
518      /**
519       * Fair Read trylock succeeds if write locked by current thread
520       */
# Line 490 | Line 533 | public class ReentrantReadWriteLockTest
533      public void testReadHoldingWriteLockFair2() throws InterruptedException {
534          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
535          lock.writeLock().lock();
536 <        Thread t1 = new Thread(new CheckedRunnable() {
536 >        lock.readLock().lock();
537 >        lock.readLock().unlock();
538 >
539 >        Thread t1 = newStartedThread(new CheckedRunnable() {
540              public void realRun() {
541                  lock.readLock().lock();
542                  lock.readLock().unlock();
543              }});
544 <        Thread t2 = new Thread(new CheckedRunnable() {
544 >        Thread t2 = newStartedThread(new CheckedRunnable() {
545              public void realRun() {
546                  lock.readLock().lock();
547                  lock.readLock().unlock();
548              }});
549  
550 <        t1.start();
551 <        t2.start();
552 <        lock.readLock().lock();
507 <        lock.readLock().unlock();
508 <        Thread.sleep(SHORT_DELAY_MS);
550 >        waitForQueuedThread(lock, t1);
551 >        waitForQueuedThread(lock, t2);
552 >        assertTrue(lock.isWriteLockedByCurrentThread());
553          lock.readLock().lock();
554          lock.readLock().unlock();
555 <        lock.writeLock().unlock();
556 <        t1.join(MEDIUM_DELAY_MS);
557 <        t2.join(MEDIUM_DELAY_MS);
514 <        assertTrue(!t1.isAlive());
515 <        assertTrue(!t2.isAlive());
555 >        releaseWriteLock(lock);
556 >        awaitTermination(t1);
557 >        awaitTermination(t2);
558      }
559  
518
560      /**
561       * Fair Read lock succeeds if write locked by current thread even if
562       * other threads are waiting for writelock
# Line 523 | Line 564 | public class ReentrantReadWriteLockTest
564      public void testReadHoldingWriteLockFair3() throws InterruptedException {
565          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
566          lock.writeLock().lock();
567 <        Thread t1 = new Thread(new CheckedRunnable() {
567 >        lock.readLock().lock();
568 >        lock.readLock().unlock();
569 >
570 >        Thread t1 = newStartedThread(new CheckedRunnable() {
571              public void realRun() {
572                  lock.writeLock().lock();
573                  lock.writeLock().unlock();
574              }});
575 <        Thread t2 = new Thread(new CheckedRunnable() {
575 >        Thread t2 = newStartedThread(new CheckedRunnable() {
576              public void realRun() {
577                  lock.writeLock().lock();
578                  lock.writeLock().unlock();
579              }});
580  
581 <        t1.start();
582 <        t2.start();
583 <        lock.readLock().lock();
540 <        lock.readLock().unlock();
541 <        Thread.sleep(SHORT_DELAY_MS);
581 >        waitForQueuedThread(lock, t1);
582 >        waitForQueuedThread(lock, t2);
583 >        assertTrue(lock.isWriteLockedByCurrentThread());
584          lock.readLock().lock();
585          lock.readLock().unlock();
586 <        lock.writeLock().unlock();
587 <        t1.join(MEDIUM_DELAY_MS);
588 <        t2.join(MEDIUM_DELAY_MS);
547 <        assertTrue(!t1.isAlive());
548 <        assertTrue(!t2.isAlive());
586 >        releaseWriteLock(lock);
587 >        awaitTermination(t1);
588 >        awaitTermination(t2);
589      }
590  
551
591      /**
592       * Fair Write lock succeeds if write locked by current thread even if
593       * other threads are waiting for writelock
# Line 556 | Line 595 | public class ReentrantReadWriteLockTest
595      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
596          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
597          lock.writeLock().lock();
598 <        Thread t1 = new Thread(new CheckedRunnable() {
598 >        Thread t1 = newStartedThread(new CheckedRunnable() {
599              public void realRun() {
600                  lock.writeLock().lock();
601                  lock.writeLock().unlock();
602              }});
603 <        Thread t2 = new Thread(new CheckedRunnable() {
603 >        Thread t2 = newStartedThread(new CheckedRunnable() {
604              public void realRun() {
605                  lock.writeLock().lock();
606                  lock.writeLock().unlock();
607              }});
608  
609 <        t1.start();
610 <        t2.start();
572 <        Thread.sleep(SHORT_DELAY_MS);
609 >        waitForQueuedThread(lock, t1);
610 >        waitForQueuedThread(lock, t2);
611          assertTrue(lock.isWriteLockedByCurrentThread());
612 <        assertTrue(lock.getWriteHoldCount() == 1);
612 >        assertEquals(1, lock.getWriteHoldCount());
613          lock.writeLock().lock();
614 <        assertTrue(lock.getWriteHoldCount() == 2);
614 >        assertEquals(2, lock.getWriteHoldCount());
615          lock.writeLock().unlock();
616          lock.writeLock().lock();
617          lock.writeLock().unlock();
618 <        lock.writeLock().unlock();
619 <        t1.join(MEDIUM_DELAY_MS);
620 <        t2.join(MEDIUM_DELAY_MS);
583 <        assertTrue(!t1.isAlive());
584 <        assertTrue(!t2.isAlive());
618 >        releaseWriteLock(lock);
619 >        awaitTermination(t1);
620 >        awaitTermination(t2);
621      }
622  
587
623      /**
624       * Read tryLock succeeds if readlocked but not writelocked
625       */
626      public void testTryLockWhenReadLocked() throws InterruptedException {
627          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
628          lock.readLock().lock();
629 <        Thread t = new Thread(new CheckedRunnable() {
629 >        Thread t = newStartedThread(new CheckedRunnable() {
630              public void realRun() {
631 <                threadAssertTrue(lock.readLock().tryLock());
631 >                assertTrue(lock.readLock().tryLock());
632                  lock.readLock().unlock();
633              }});
634  
635 <        t.start();
601 <        t.join();
635 >        awaitTermination(t);
636          lock.readLock().unlock();
637      }
638  
605
606
639      /**
640       * write tryLock fails when readlocked
641       */
642      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
643          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
644          lock.readLock().lock();
645 <        Thread t = new Thread(new CheckedRunnable() {
645 >        Thread t = newStartedThread(new CheckedRunnable() {
646              public void realRun() {
647 <                threadAssertFalse(lock.writeLock().tryLock());
647 >                assertFalse(lock.writeLock().tryLock());
648              }});
649  
650 <        t.start();
619 <        t.join();
650 >        awaitTermination(t);
651          lock.readLock().unlock();
652      }
653  
623
654      /**
655       * Fair Read tryLock succeeds if readlocked but not writelocked
656       */
657      public void testTryLockWhenReadLockedFair() throws InterruptedException {
658          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
659          lock.readLock().lock();
660 <        Thread t = new Thread(new CheckedRunnable() {
660 >        Thread t = newStartedThread(new CheckedRunnable() {
661              public void realRun() {
662 <                threadAssertTrue(lock.readLock().tryLock());
662 >                assertTrue(lock.readLock().tryLock());
663                  lock.readLock().unlock();
664              }});
665  
666 <        t.start();
637 <        t.join();
666 >        awaitTermination(t);
667          lock.readLock().unlock();
668      }
669  
641
642
670      /**
671       * Fair write tryLock fails when readlocked
672       */
673      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
674          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
675          lock.readLock().lock();
676 <        Thread t = new Thread(new CheckedRunnable() {
676 >        Thread t = newStartedThread(new CheckedRunnable() {
677              public void realRun() {
678 <                threadAssertFalse(lock.writeLock().tryLock());
678 >                assertFalse(lock.writeLock().tryLock());
679              }});
680  
681 <        t.start();
655 <        t.join();
681 >        awaitTermination(t);
682          lock.readLock().unlock();
683      }
684  
659
660
685      /**
686       * write timed tryLock times out if locked
687       */
688      public void testWriteTryLock_Timeout() throws InterruptedException {
689          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
690          lock.writeLock().lock();
691 <        Thread t = new Thread(new CheckedRunnable() {
691 >        Thread t = newStartedThread(new CheckedRunnable() {
692              public void realRun() throws InterruptedException {
693 <                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
693 >                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
694              }});
695  
696 <        t.start();
673 <        t.join();
696 >        awaitTermination(t);
697          assertTrue(lock.writeLock().isHeldByCurrentThread());
698          lock.writeLock().unlock();
699      }
# Line 681 | Line 704 | public class ReentrantReadWriteLockTest
704      public void testReadTryLock_Timeout() throws InterruptedException {
705          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
706          lock.writeLock().lock();
707 <        Thread t = new Thread(new CheckedRunnable() {
707 >        Thread t = newStartedThread(new CheckedRunnable() {
708              public void realRun() throws InterruptedException {
709 <                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
709 >                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
710              }});
711  
712 <        t.start();
690 <        t.join();
712 >        awaitTermination(t);
713          assertTrue(lock.writeLock().isHeldByCurrentThread());
714          lock.writeLock().unlock();
715      }
716  
695
717      /**
718       * write lockInterruptibly succeeds if lock free else is interruptible
719       */
720      public void testWriteLockInterruptibly() throws InterruptedException {
721          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
722          lock.writeLock().lockInterruptibly();
723 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
723 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
724              public void realRun() throws InterruptedException {
725                  lock.writeLock().lockInterruptibly();
726              }});
727  
728 <        t.start();
708 <        Thread.sleep(SHORT_DELAY_MS);
728 >        waitForQueuedThread(lock, t);
729          t.interrupt();
730 <        Thread.sleep(SHORT_DELAY_MS);
731 <        t.join();
712 <        lock.writeLock().unlock();
730 >        awaitTermination(t);
731 >        releaseWriteLock(lock);
732      }
733  
734      /**
735 <     *  read lockInterruptibly succeeds if lock free else is interruptible
735 >     * read lockInterruptibly succeeds if lock free else is interruptible
736       */
737      public void testReadLockInterruptibly() throws InterruptedException {
738          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
739          lock.writeLock().lockInterruptibly();
740 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
740 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
741              public void realRun() throws InterruptedException {
742                  lock.readLock().lockInterruptibly();
743              }});
744  
745 <        t.start();
727 <        Thread.sleep(SHORT_DELAY_MS);
745 >        waitForQueuedThread(lock, t);
746          t.interrupt();
747 <        t.join();
748 <        lock.writeLock().unlock();
747 >        awaitTermination(t);
748 >        releaseWriteLock(lock);
749      }
750  
751      /**
# Line 740 | Line 758 | public class ReentrantReadWriteLockTest
758              c.await();
759              shouldThrow();
760          } catch (IllegalMonitorStateException success) {}
761 +        try {
762 +            c.await(LONG_DELAY_MS, MILLISECONDS);
763 +            shouldThrow();
764 +        } catch (IllegalMonitorStateException success) {}
765 +        try {
766 +            c.awaitNanos(100);
767 +            shouldThrow();
768 +        } catch (IllegalMonitorStateException success) {}
769 +        try {
770 +            c.awaitUninterruptibly();
771 +            shouldThrow();
772 +        } catch (IllegalMonitorStateException success) {}
773      }
774  
775      /**
# Line 755 | Line 785 | public class ReentrantReadWriteLockTest
785      }
786  
787      /**
788 +     * Calling signalAll without holding lock throws IllegalMonitorStateException
789 +     */
790 +    public void testSignalAll_IllegalMonitor() {
791 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
792 +        final Condition c = lock.writeLock().newCondition();
793 +        try {
794 +            c.signalAll();
795 +            shouldThrow();
796 +        } catch (IllegalMonitorStateException success) {}
797 +    }
798 +
799 +    /**
800       * awaitNanos without a signal times out
801       */
802      public void testAwaitNanos_Timeout() throws InterruptedException {
803          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
804          final Condition c = lock.writeLock().newCondition();
763
805          lock.writeLock().lock();
806 <        long t = c.awaitNanos(100);
807 <        assertTrue(t <= 0);
806 >        long startTime = System.nanoTime();
807 >        long timeoutMillis = 10;
808 >        long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
809 >        long nanosRemaining = c.awaitNanos(timeoutNanos);
810 >        assertTrue(nanosRemaining <= 0);
811 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
812          lock.writeLock().unlock();
813      }
814  
770
815      /**
816 <     *  timed await without a signal times out
816 >     * timed await without a signal times out
817       */
818      public void testAwait_Timeout() throws InterruptedException {
819          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
820          final Condition c = lock.writeLock().newCondition();
821          lock.writeLock().lock();
822 <        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
822 >        long startTime = System.nanoTime();
823 >        long timeoutMillis = 10;
824 >        assertFalse(c.await(timeoutMillis, MILLISECONDS));
825 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
826          lock.writeLock().unlock();
827      }
828  
# Line 797 | Line 844 | public class ReentrantReadWriteLockTest
844      public void testAwait() throws InterruptedException {
845          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
846          final Condition c = lock.writeLock().newCondition();
847 <        Thread t = new Thread(new CheckedRunnable() {
847 >        final CountDownLatch locked = new CountDownLatch(1);
848 >        Thread t = newStartedThread(new CheckedRunnable() {
849              public void realRun() throws InterruptedException {
850                  lock.writeLock().lock();
851 +                locked.countDown();
852                  c.await();
853                  lock.writeLock().unlock();
854              }});
855  
856 <        t.start();
808 <        Thread.sleep(SHORT_DELAY_MS);
856 >        locked.await();
857          lock.writeLock().lock();
858          c.signal();
859 +        assertTrue(t.isAlive());
860          lock.writeLock().unlock();
861 <        t.join(SHORT_DELAY_MS);
813 <        assertFalse(t.isAlive());
814 <    }
815 <
816 <    /** A helper class for uninterruptible wait tests */
817 <    class UninterruptableThread extends Thread {
818 <        private Lock lock;
819 <        private Condition c;
820 <
821 <        public volatile boolean canAwake = false;
822 <        public volatile boolean interrupted = false;
823 <        public volatile boolean lockStarted = false;
824 <
825 <        public UninterruptableThread(Lock lock, Condition c) {
826 <            this.lock = lock;
827 <            this.c = c;
828 <        }
829 <
830 <        public synchronized void run() {
831 <            lock.lock();
832 <            lockStarted = true;
833 <
834 <            while (!canAwake) {
835 <                c.awaitUninterruptibly();
836 <            }
837 <
838 <            interrupted = isInterrupted();
839 <            lock.unlock();
840 <        }
861 >        awaitTermination(t);
862      }
863  
864      /**
# Line 846 | Line 867 | public class ReentrantReadWriteLockTest
867      public void testAwaitUninterruptibly() throws InterruptedException {
868          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
869          final Condition c = lock.writeLock().newCondition();
870 <        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
871 <
872 <        thread.start();
873 <
874 <        while (!thread.lockStarted) {
875 <            Thread.sleep(100);
876 <        }
870 >        final CountDownLatch locked = new CountDownLatch(1);
871 >        final AtomicBoolean canAwake = new AtomicBoolean(false);
872 >        Thread t = newStartedThread(new CheckedRunnable() {
873 >            public void realRun() {
874 >                lock.writeLock().lock();
875 >                locked.countDown();
876 >                c.awaitUninterruptibly();
877 >                assertTrue(Thread.interrupted());
878 >                lock.writeLock().unlock();
879 >            }});
880  
881 +        locked.await();
882          lock.writeLock().lock();
883 <        try {
884 <            thread.interrupt();
885 <            thread.canAwake = true;
886 <            c.signal();
887 <        } finally {
888 <            lock.writeLock().unlock();
889 <        }
890 <
866 <        thread.join();
867 <        assertTrue(thread.interrupted);
868 <        assertFalse(thread.isAlive());
883 >        lock.writeLock().unlock();
884 >        t.interrupt();
885 >        t.join(10);
886 >        assertTrue(t.isAlive());
887 >        lock.writeLock().lock();
888 >        c.signal();
889 >        lock.writeLock().unlock();
890 >        awaitTermination(t);
891      }
892  
893      /**
894       * await is interruptible
895       */
896      public void testAwait_Interrupt() throws InterruptedException {
897 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
897 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
898          final Condition c = lock.writeLock().newCondition();
899 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
899 >        final CountDownLatch locked = new CountDownLatch(1);
900 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
901              public void realRun() throws InterruptedException {
902                  lock.writeLock().lock();
903 <                c.await();
904 <                lock.writeLock().unlock();
903 >                assertTrue(lock.isWriteLocked());
904 >                assertTrue(lock.isWriteLockedByCurrentThread());
905 >                assertHasNoWaiters(lock, c);
906 >                locked.countDown();
907 >                try {
908 >                    c.await();
909 >                } finally {
910 >                    assertTrue(lock.isWriteLocked());
911 >                    assertTrue(lock.isWriteLockedByCurrentThread());
912 >                    assertHasNoWaiters(lock, c);
913 >                    lock.writeLock().unlock();
914 >                    assertFalse(Thread.interrupted());
915 >                }
916              }});
917  
918 <        t.start();
919 <        Thread.sleep(SHORT_DELAY_MS);
918 >        locked.await();
919 >        assertHasWaiters(lock, c, t);
920          t.interrupt();
921 <        t.join(SHORT_DELAY_MS);
922 <        assertFalse(t.isAlive());
921 >        awaitTermination(t);
922 >        assertFalse(lock.isWriteLocked());
923      }
924  
925      /**
926       * awaitNanos is interruptible
927       */
928      public void testAwaitNanos_Interrupt() throws InterruptedException {
929 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
929 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
930          final Condition c = lock.writeLock().newCondition();
931 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
931 >        final CountDownLatch locked = new CountDownLatch(1);
932 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
933              public void realRun() throws InterruptedException {
934                  lock.writeLock().lock();
935 <                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
936 <                lock.writeLock().unlock();
935 >                assertTrue(lock.isWriteLocked());
936 >                assertTrue(lock.isWriteLockedByCurrentThread());
937 >                assertHasNoWaiters(lock, c);
938 >                locked.countDown();
939 >                try {
940 >                    c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
941 >                } finally {
942 >                    assertTrue(lock.isWriteLocked());
943 >                    assertTrue(lock.isWriteLockedByCurrentThread());
944 >                    assertHasNoWaiters(lock, c);
945 >                    lock.writeLock().unlock();
946 >                    assertFalse(Thread.interrupted());
947 >                }
948              }});
949  
950 <        t.start();
951 <        Thread.sleep(SHORT_DELAY_MS);
950 >        locked.await();
951 >        assertHasWaiters(lock, c, t);
952          t.interrupt();
953 <        t.join(SHORT_DELAY_MS);
954 <        assertFalse(t.isAlive());
953 >        awaitTermination(t);
954 >        assertFalse(lock.isWriteLocked());
955      }
956  
957      /**
958       * awaitUntil is interruptible
959       */
960      public void testAwaitUntil_Interrupt() throws InterruptedException {
961 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
961 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
962          final Condition c = lock.writeLock().newCondition();
963 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
963 >        final CountDownLatch locked = new CountDownLatch(1);
964 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
965              public void realRun() throws InterruptedException {
966                  lock.writeLock().lock();
967 +                assertTrue(lock.isWriteLocked());
968 +                assertTrue(lock.isWriteLockedByCurrentThread());
969 +                assertHasNoWaiters(lock, c);
970 +                locked.countDown();
971                  java.util.Date d = new java.util.Date();
972 <                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
973 <                lock.writeLock().unlock();
972 >                try {
973 >                    c.awaitUntil(new java.util.Date(d.getTime() + 10000));
974 >                } finally {
975 >                    assertTrue(lock.isWriteLocked());
976 >                    assertTrue(lock.isWriteLockedByCurrentThread());
977 >                    assertHasNoWaiters(lock, c);
978 >                    lock.writeLock().unlock();
979 >                    assertFalse(Thread.interrupted());
980 >                }
981              }});
982  
983 <        t.start();
984 <        Thread.sleep(SHORT_DELAY_MS);
983 >        locked.await();
984 >        assertHasWaiters(lock, c, t);
985          t.interrupt();
986 <        t.join(SHORT_DELAY_MS);
987 <        assertFalse(t.isAlive());
986 >        awaitTermination(t);
987 >        assertFalse(lock.isWriteLocked());
988      }
989  
990      /**
991       * signalAll wakes up all threads
992       */
993      public void testSignalAll() throws InterruptedException {
994 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
994 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
995          final Condition c = lock.writeLock().newCondition();
996 <        Thread t1 = new Thread(new CheckedRunnable() {
996 >        final CountDownLatch locked = new CountDownLatch(2);
997 >        final Lock writeLock = lock.writeLock();
998 >        Thread t1 = newStartedThread(new CheckedRunnable() {
999              public void realRun() throws InterruptedException {
1000 <                lock.writeLock().lock();
1000 >                writeLock.lock();
1001 >                locked.countDown();
1002                  c.await();
1003 <                lock.writeLock().unlock();
1003 >                writeLock.unlock();
1004              }});
1005  
1006 <        Thread t2 = new Thread(new CheckedRunnable() {
1006 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1007              public void realRun() throws InterruptedException {
1008 <                lock.writeLock().lock();
1008 >                writeLock.lock();
1009 >                locked.countDown();
1010                  c.await();
1011 <                lock.writeLock().unlock();
1011 >                writeLock.unlock();
1012              }});
1013  
1014 <        t1.start();
1015 <        t2.start();
1016 <        Thread.sleep(SHORT_DELAY_MS);
955 <        lock.writeLock().lock();
1014 >        locked.await();
1015 >        writeLock.lock();
1016 >        assertHasWaiters(lock, c, t1, t2);
1017          c.signalAll();
1018 <        lock.writeLock().unlock();
1019 <        t1.join(SHORT_DELAY_MS);
1020 <        t2.join(SHORT_DELAY_MS);
1021 <        assertFalse(t1.isAlive());
1022 <        assertFalse(t2.isAlive());
1018 >        assertHasNoWaiters(lock, c);
1019 >        writeLock.unlock();
1020 >        awaitTermination(t1);
1021 >        awaitTermination(t2);
1022 >    }
1023 >
1024 >    /**
1025 >     * signal wakes up waiting threads in FIFO order.
1026 >     */
1027 >    public void testSignalWakesFifo() throws InterruptedException {
1028 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1029 >        final Condition c = lock.writeLock().newCondition();
1030 >        final CountDownLatch locked1 = new CountDownLatch(1);
1031 >        final CountDownLatch locked2 = new CountDownLatch(1);
1032 >        final Lock writeLock = lock.writeLock();
1033 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1034 >            public void realRun() throws InterruptedException {
1035 >                writeLock.lock();
1036 >                locked1.countDown();
1037 >                c.await();
1038 >                writeLock.unlock();
1039 >            }});
1040 >
1041 >        locked1.await();
1042 >
1043 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1044 >            public void realRun() throws InterruptedException {
1045 >                writeLock.lock();
1046 >                locked2.countDown();
1047 >                c.await();
1048 >                writeLock.unlock();
1049 >            }});
1050 >
1051 >        locked2.await();
1052 >
1053 >        writeLock.lock();
1054 >        assertHasWaiters(lock, c, t1, t2);
1055 >        assertFalse(lock.hasQueuedThreads());
1056 >        c.signal();
1057 >        assertHasWaiters(lock, c, t2);
1058 >        assertTrue(lock.hasQueuedThread(t1));
1059 >        assertFalse(lock.hasQueuedThread(t2));
1060 >        c.signal();
1061 >        assertHasNoWaiters(lock, c);
1062 >        assertTrue(lock.hasQueuedThread(t1));
1063 >        assertTrue(lock.hasQueuedThread(t2));
1064 >        writeLock.unlock();
1065 >        awaitTermination(t1);
1066 >        awaitTermination(t2);
1067      }
1068  
1069      /**
# Line 990 | Line 1095 | public class ReentrantReadWriteLockTest
1095          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1096          assertFalse(lock.hasQueuedThreads());
1097          lock.writeLock().lock();
1098 +        assertFalse(lock.hasQueuedThreads());
1099 +        long startTime = System.nanoTime();
1100          t1.start();
1101 <        Thread.sleep(SHORT_DELAY_MS);
995 <        assertTrue(lock.hasQueuedThreads());
1101 >        waitForQueuedThread(lock, t1);
1102          t2.start();
1103 <        Thread.sleep(SHORT_DELAY_MS);
1103 >        waitForQueuedThread(lock, t2);
1104          assertTrue(lock.hasQueuedThreads());
1105          t1.interrupt();
1106 <        Thread.sleep(SHORT_DELAY_MS);
1106 >        awaitTermination(t1);
1107          assertTrue(lock.hasQueuedThreads());
1108          lock.writeLock().unlock();
1109 <        Thread.sleep(SHORT_DELAY_MS);
1109 >        awaitTermination(t2);
1110          assertFalse(lock.hasQueuedThreads());
1005        t1.join();
1006        t2.join();
1111      }
1112  
1113      /**
# Line 1021 | Line 1125 | public class ReentrantReadWriteLockTest
1125       * hasQueuedThread reports whether a thread is queued.
1126       */
1127      public void testHasQueuedThread() throws InterruptedException {
1128 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1129 <        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1130 <        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1131 <        assertFalse(sync.hasQueuedThread(t1));
1132 <        assertFalse(sync.hasQueuedThread(t2));
1133 <        sync.writeLock().lock();
1128 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1129 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1130 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1131 >        assertFalse(lock.hasQueuedThread(t1));
1132 >        assertFalse(lock.hasQueuedThread(t2));
1133 >        lock.writeLock().lock();
1134 >        long startTime = System.nanoTime();
1135          t1.start();
1136 <        Thread.sleep(SHORT_DELAY_MS);
1137 <        assertTrue(sync.hasQueuedThread(t1));
1136 >        waitForQueuedThread(lock, t1);
1137 >        assertTrue(lock.hasQueuedThread(t1));
1138 >        assertFalse(lock.hasQueuedThread(t2));
1139          t2.start();
1140 <        Thread.sleep(SHORT_DELAY_MS);
1141 <        assertTrue(sync.hasQueuedThread(t1));
1142 <        assertTrue(sync.hasQueuedThread(t2));
1140 >        waitForQueuedThread(lock, t2);
1141 >        assertTrue(lock.hasQueuedThread(t1));
1142 >        assertTrue(lock.hasQueuedThread(t2));
1143          t1.interrupt();
1144 <        Thread.sleep(SHORT_DELAY_MS);
1145 <        assertFalse(sync.hasQueuedThread(t1));
1146 <        assertTrue(sync.hasQueuedThread(t2));
1147 <        sync.writeLock().unlock();
1148 <        Thread.sleep(SHORT_DELAY_MS);
1149 <        assertFalse(sync.hasQueuedThread(t1));
1150 <        Thread.sleep(SHORT_DELAY_MS);
1045 <        assertFalse(sync.hasQueuedThread(t2));
1046 <        t1.join();
1047 <        t2.join();
1144 >        awaitTermination(t1);
1145 >        assertFalse(lock.hasQueuedThread(t1));
1146 >        assertTrue(lock.hasQueuedThread(t2));
1147 >        lock.writeLock().unlock();
1148 >        awaitTermination(t2);
1149 >        assertFalse(lock.hasQueuedThread(t1));
1150 >        assertFalse(lock.hasQueuedThread(t2));
1151      }
1152  
1050
1153      /**
1154       * getQueueLength reports number of waiting threads
1155       */
# Line 1057 | Line 1159 | public class ReentrantReadWriteLockTest
1159          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1160          assertEquals(0, lock.getQueueLength());
1161          lock.writeLock().lock();
1162 +        long startTime = System.nanoTime();
1163          t1.start();
1164 <        Thread.sleep(SHORT_DELAY_MS);
1164 >        waitForQueuedThread(lock, t1);
1165          assertEquals(1, lock.getQueueLength());
1166          t2.start();
1167 <        Thread.sleep(SHORT_DELAY_MS);
1167 >        waitForQueuedThread(lock, t2);
1168          assertEquals(2, lock.getQueueLength());
1169          t1.interrupt();
1170 <        Thread.sleep(SHORT_DELAY_MS);
1170 >        awaitTermination(t1);
1171          assertEquals(1, lock.getQueueLength());
1172          lock.writeLock().unlock();
1173 <        Thread.sleep(SHORT_DELAY_MS);
1173 >        awaitTermination(t2);
1174          assertEquals(0, lock.getQueueLength());
1072        t1.join();
1073        t2.join();
1175      }
1176  
1177      /**
# Line 1082 | Line 1183 | public class ReentrantReadWriteLockTest
1183          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1184          assertTrue(lock.getQueuedThreads().isEmpty());
1185          lock.writeLock().lock();
1186 +        long startTime = System.nanoTime();
1187          assertTrue(lock.getQueuedThreads().isEmpty());
1188          t1.start();
1189 <        Thread.sleep(SHORT_DELAY_MS);
1189 >        waitForQueuedThread(lock, t1);
1190 >        assertEquals(1, lock.getQueuedThreads().size());
1191          assertTrue(lock.getQueuedThreads().contains(t1));
1192          t2.start();
1193 <        Thread.sleep(SHORT_DELAY_MS);
1193 >        waitForQueuedThread(lock, t2);
1194 >        assertEquals(2, lock.getQueuedThreads().size());
1195          assertTrue(lock.getQueuedThreads().contains(t1));
1196          assertTrue(lock.getQueuedThreads().contains(t2));
1197          t1.interrupt();
1198 <        Thread.sleep(SHORT_DELAY_MS);
1198 >        awaitTermination(t1);
1199          assertFalse(lock.getQueuedThreads().contains(t1));
1200          assertTrue(lock.getQueuedThreads().contains(t2));
1201 +        assertEquals(1, lock.getQueuedThreads().size());
1202          lock.writeLock().unlock();
1203 <        Thread.sleep(SHORT_DELAY_MS);
1203 >        awaitTermination(t2);
1204          assertTrue(lock.getQueuedThreads().isEmpty());
1100        t1.join();
1101        t2.join();
1205      }
1206  
1207      /**
# Line 1123 | Line 1226 | public class ReentrantReadWriteLockTest
1226          } catch (NullPointerException success) {}
1227      }
1228  
1126
1229      /**
1230       * getWaitingThreads throws NPE if null
1231       */
# Line 1160 | Line 1262 | public class ReentrantReadWriteLockTest
1262          } catch (IllegalMonitorStateException success) {}
1263      }
1264  
1163
1265      /**
1266       * getWaitQueueLength throws IAE if not owned
1267       */
# Line 1186 | Line 1287 | public class ReentrantReadWriteLockTest
1287          } catch (IllegalMonitorStateException success) {}
1288      }
1289  
1189
1290      /**
1291       * getWaitingThreads throws IAE if not owned
1292       */
# Line 1212 | Line 1312 | public class ReentrantReadWriteLockTest
1312          } catch (IllegalMonitorStateException success) {}
1313      }
1314  
1215
1315      /**
1316       * hasWaiters returns true when a thread is waiting, else false
1317       */
1318      public void testHasWaiters() throws InterruptedException {
1319 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1319 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1320          final Condition c = lock.writeLock().newCondition();
1321 <        Thread t = new Thread(new CheckedRunnable() {
1321 >        final CountDownLatch locked = new CountDownLatch(1);
1322 >        Thread t = newStartedThread(new CheckedRunnable() {
1323              public void realRun() throws InterruptedException {
1324                  lock.writeLock().lock();
1325 <                threadAssertFalse(lock.hasWaiters(c));
1326 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1325 >                assertFalse(lock.hasWaiters(c));
1326 >                locked.countDown();
1327 >                assertEquals(0, lock.getWaitQueueLength(c));
1328                  c.await();
1329                  lock.writeLock().unlock();
1330              }});
1331  
1332 <        t.start();
1232 <        Thread.sleep(SHORT_DELAY_MS);
1332 >        locked.await();
1333          lock.writeLock().lock();
1334          assertTrue(lock.hasWaiters(c));
1335          assertEquals(1, lock.getWaitQueueLength(c));
1336          c.signal();
1337 +        assertHasNoWaiters(lock, c);
1338          lock.writeLock().unlock();
1339 <        Thread.sleep(SHORT_DELAY_MS);
1340 <        lock.writeLock().lock();
1240 <        assertFalse(lock.hasWaiters(c));
1241 <        assertEquals(0, lock.getWaitQueueLength(c));
1242 <        lock.writeLock().unlock();
1243 <        t.join(SHORT_DELAY_MS);
1244 <        assertFalse(t.isAlive());
1339 >        awaitTermination(t);
1340 >        assertHasNoWaiters(lock, c);
1341      }
1342  
1343      /**
1344       * getWaitQueueLength returns number of waiting threads
1345       */
1346      public void testGetWaitQueueLength() throws InterruptedException {
1347 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1347 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1348          final Condition c = lock.writeLock().newCondition();
1349 <        Thread t = new Thread(new CheckedRunnable() {
1349 >        final CountDownLatch locked = new CountDownLatch(1);
1350 >        Thread t = newStartedThread(new CheckedRunnable() {
1351              public void realRun() throws InterruptedException {
1352                  lock.writeLock().lock();
1353 <                threadAssertFalse(lock.hasWaiters(c));
1354 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1353 >                assertEquals(0, lock.getWaitQueueLength(c));
1354 >                locked.countDown();
1355                  c.await();
1356                  lock.writeLock().unlock();
1357              }});
1358  
1359 <        t.start();
1263 <        Thread.sleep(SHORT_DELAY_MS);
1359 >        locked.await();
1360          lock.writeLock().lock();
1361 <        assertTrue(lock.hasWaiters(c));
1361 >        assertHasWaiters(lock, c, t);
1362          assertEquals(1, lock.getWaitQueueLength(c));
1363          c.signal();
1364 <        lock.writeLock().unlock();
1269 <        Thread.sleep(SHORT_DELAY_MS);
1270 <        lock.writeLock().lock();
1271 <        assertFalse(lock.hasWaiters(c));
1364 >        assertHasNoWaiters(lock, c);
1365          assertEquals(0, lock.getWaitQueueLength(c));
1366          lock.writeLock().unlock();
1367 <        t.join(SHORT_DELAY_MS);
1275 <        assertFalse(t.isAlive());
1367 >        awaitTermination(t);
1368      }
1369  
1278
1370      /**
1371       * getWaitingThreads returns only and all waiting threads
1372       */
1373      public void testGetWaitingThreads() throws InterruptedException {
1374          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1375          final Condition c = lock.writeLock().newCondition();
1376 +        final CountDownLatch locked1 = new CountDownLatch(1);
1377 +        final CountDownLatch locked2 = new CountDownLatch(1);
1378          Thread t1 = new Thread(new CheckedRunnable() {
1379              public void realRun() throws InterruptedException {
1380                  lock.writeLock().lock();
1381 <                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1381 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
1382 >                locked1.countDown();
1383                  c.await();
1384                  lock.writeLock().unlock();
1385              }});
# Line 1293 | Line 1387 | public class ReentrantReadWriteLockTest
1387          Thread t2 = new Thread(new CheckedRunnable() {
1388              public void realRun() throws InterruptedException {
1389                  lock.writeLock().lock();
1390 <                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1390 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
1391 >                locked2.countDown();
1392                  c.await();
1393                  lock.writeLock().unlock();
1394              }});
# Line 1301 | Line 1396 | public class ReentrantReadWriteLockTest
1396          lock.writeLock().lock();
1397          assertTrue(lock.getWaitingThreads(c).isEmpty());
1398          lock.writeLock().unlock();
1399 +
1400          t1.start();
1401 <        Thread.sleep(SHORT_DELAY_MS);
1401 >        locked1.await();
1402          t2.start();
1403 <        Thread.sleep(SHORT_DELAY_MS);
1403 >        locked2.await();
1404 >
1405          lock.writeLock().lock();
1406          assertTrue(lock.hasWaiters(c));
1407          assertTrue(lock.getWaitingThreads(c).contains(t1));
1408          assertTrue(lock.getWaitingThreads(c).contains(t2));
1409 +        assertEquals(2, lock.getWaitingThreads(c).size());
1410          c.signalAll();
1411 +        assertHasNoWaiters(lock, c);
1412          lock.writeLock().unlock();
1413 <        Thread.sleep(SHORT_DELAY_MS);
1414 <        lock.writeLock().lock();
1415 <        assertFalse(lock.hasWaiters(c));
1416 <        assertTrue(lock.getWaitingThreads(c).isEmpty());
1417 <        lock.writeLock().unlock();
1319 <        t1.join(SHORT_DELAY_MS);
1320 <        t2.join(SHORT_DELAY_MS);
1321 <        assertFalse(t1.isAlive());
1322 <        assertFalse(t2.isAlive());
1413 >
1414 >        awaitTermination(t1);
1415 >        awaitTermination(t2);
1416 >
1417 >        assertHasNoWaiters(lock, c);
1418      }
1419  
1420      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines