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.55 by jsr166, Mon May 2 01:15:26 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   */
# Line 15 | Line 15 | import java.util.*;
15  
16   public class ReentrantReadWriteLockTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21          return new TestSuite(ReentrantReadWriteLockTest.class);
# 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       * Constructor sets given fairness, and is in unlocked state
73       */
74      public void testConstructor() {
# Line 190 | Line 200 | public class ReentrantReadWriteLockTest
200       */
201      public void testWriteLockInterruptibly_Interrupted() throws Exception {
202          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
203 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
203 >        lock.writeLock().lock();
204 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
205              public void realRun() throws InterruptedException {
206                  lock.writeLock().lockInterruptibly();
196                lock.writeLock().unlock();
197                lock.writeLock().lockInterruptibly();
198                lock.writeLock().unlock();
207              }});
208  
201        lock.writeLock().lock();
202        t.start();
209          Thread.sleep(SHORT_DELAY_MS);
210          t.interrupt();
211 <        Thread.sleep(SHORT_DELAY_MS);
212 <        lock.writeLock().unlock();
207 <        t.join();
211 >        awaitTermination(t, LONG_DELAY_MS);
212 >        releaseWriteLock(lock);
213      }
214  
215      /**
# Line 213 | Line 218 | public class ReentrantReadWriteLockTest
218      public void testWriteTryLock_Interrupted() throws InterruptedException {
219          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
220          lock.writeLock().lock();
221 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
221 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
222              public void realRun() throws InterruptedException {
223 <                lock.writeLock().tryLock(1000,MILLISECONDS);
223 >                lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS);
224              }});
225  
226 <        t.start();
226 >        Thread.sleep(SHORT_DELAY_MS);
227          t.interrupt();
228 <        lock.writeLock().unlock();
229 <        t.join();
228 >        awaitTermination(t, LONG_DELAY_MS);
229 >        releaseWriteLock(lock);
230      }
231  
232      /**
# Line 230 | Line 235 | public class ReentrantReadWriteLockTest
235      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
236          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
237          lock.writeLock().lock();
238 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
238 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
239              public void realRun() throws InterruptedException {
240                  lock.readLock().lockInterruptibly();
241              }});
242  
238        t.start();
243          Thread.sleep(SHORT_DELAY_MS);
244          t.interrupt();
245 <        Thread.sleep(SHORT_DELAY_MS);
246 <        lock.writeLock().unlock();
243 <        t.join();
245 >        awaitTermination(t, LONG_DELAY_MS);
246 >        releaseWriteLock(lock);
247      }
248  
249      /**
# Line 249 | Line 252 | public class ReentrantReadWriteLockTest
252      public void testReadTryLock_Interrupted() throws InterruptedException {
253          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
254          lock.writeLock().lock();
255 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
255 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
256              public void realRun() throws InterruptedException {
257 <                lock.readLock().tryLock(1000,MILLISECONDS);
257 >                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
258              }});
259  
260 <        t.start();
260 >        Thread.sleep(SHORT_DELAY_MS);
261          t.interrupt();
262 <        t.join();
262 >        awaitTermination(t, LONG_DELAY_MS);
263 >        releaseWriteLock(lock);
264      }
265  
266  
# Line 266 | Line 270 | public class ReentrantReadWriteLockTest
270      public void testWriteTryLockWhenLocked() throws InterruptedException {
271          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
272          lock.writeLock().lock();
273 <        Thread t = new Thread(new CheckedRunnable() {
273 >        Thread t = newStartedThread(new CheckedRunnable() {
274              public void realRun() {
275 <                threadAssertFalse(lock.writeLock().tryLock());
275 >                assertFalse(lock.writeLock().tryLock());
276              }});
277  
278 <        t.start();
279 <        t.join();
276 <        lock.writeLock().unlock();
278 >        awaitTermination(t, LONG_DELAY_MS);
279 >        releaseWriteLock(lock);
280      }
281  
282      /**
# Line 282 | Line 285 | public class ReentrantReadWriteLockTest
285      public void testReadTryLockWhenLocked() throws InterruptedException {
286          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
287          lock.writeLock().lock();
288 <        Thread t = new Thread(new CheckedRunnable() {
288 >        Thread t = newStartedThread(new CheckedRunnable() {
289              public void realRun() {
290 <                threadAssertFalse(lock.readLock().tryLock());
290 >                assertFalse(lock.readLock().tryLock());
291              }});
292  
293 <        t.start();
294 <        t.join();
292 <        lock.writeLock().unlock();
293 >        awaitTermination(t, LONG_DELAY_MS);
294 >        releaseWriteLock(lock);
295      }
296  
297      /**
# Line 298 | Line 300 | public class ReentrantReadWriteLockTest
300      public void testMultipleReadLocks() throws InterruptedException {
301          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
302          lock.readLock().lock();
303 <        Thread t = new Thread(new CheckedRunnable() {
303 >        Thread t = newStartedThread(new CheckedRunnable() {
304              public void realRun() {
305 <                threadAssertTrue(lock.readLock().tryLock());
305 >                assertTrue(lock.readLock().tryLock());
306                  lock.readLock().unlock();
307              }});
308  
309 <        t.start();
308 <        t.join();
309 >        awaitTermination(t, LONG_DELAY_MS);
310          lock.readLock().unlock();
311      }
312  
# Line 315 | Line 316 | public class ReentrantReadWriteLockTest
316      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
317          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
318          lock.readLock().lock();
319 <        Thread t1 = new Thread(new CheckedRunnable() {
319 >        Thread t1 = newStartedThread(new CheckedRunnable() {
320              public void realRun() {
321                  lock.readLock().lock();
322                  lock.readLock().unlock();
323              }});
324 <        Thread t2 = new Thread(new CheckedRunnable() {
324 >        Thread t2 = newStartedThread(new CheckedRunnable() {
325              public void realRun() {
326                  lock.writeLock().lock();
327                  lock.writeLock().unlock();
328              }});
329  
329        t1.start();
330        t2.start();
330          Thread.sleep(SHORT_DELAY_MS);
331          lock.readLock().unlock();
332 <        t1.join(MEDIUM_DELAY_MS);
333 <        t2.join(MEDIUM_DELAY_MS);
335 <        assertTrue(!t1.isAlive());
336 <        assertTrue(!t2.isAlive());
332 >        awaitTermination(t1, LONG_DELAY_MS);
333 >        awaitTermination(t2, LONG_DELAY_MS);
334      }
335  
336      /**
# Line 342 | Line 339 | public class ReentrantReadWriteLockTest
339      public void testReadAfterWriteLock() throws InterruptedException {
340          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
341          lock.writeLock().lock();
342 <        Thread t1 = new Thread(new CheckedRunnable() {
342 >        Thread t1 = newStartedThread(new CheckedRunnable() {
343              public void realRun() {
344                  lock.readLock().lock();
345                  lock.readLock().unlock();
346              }});
347 <        Thread t2 = new Thread(new CheckedRunnable() {
347 >        Thread t2 = newStartedThread(new CheckedRunnable() {
348              public void realRun() {
349                  lock.readLock().lock();
350                  lock.readLock().unlock();
351              }});
352  
356        t1.start();
357        t2.start();
353          Thread.sleep(SHORT_DELAY_MS);
354 <        lock.writeLock().unlock();
355 <        t1.join(MEDIUM_DELAY_MS);
356 <        t2.join(MEDIUM_DELAY_MS);
357 <        assertTrue(!t1.isAlive());
358 <        assertTrue(!t2.isAlive());
354 >        assertTrue(t1.isAlive());
355 >        assertTrue(t2.isAlive());
356 >        releaseWriteLock(lock);
357 >        awaitTermination(t1, LONG_DELAY_MS);
358 >        awaitTermination(t2, LONG_DELAY_MS);
359      }
360  
361      /**
# Line 381 | Line 376 | public class ReentrantReadWriteLockTest
376      public void testReadHoldingWriteLock2() throws InterruptedException {
377          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
378          lock.writeLock().lock();
379 <        Thread t1 = new Thread(new CheckedRunnable() {
379 >        Thread t1 = newStartedThread(new CheckedRunnable() {
380              public void realRun() {
381                  lock.readLock().lock();
382                  lock.readLock().unlock();
383              }});
384 <        Thread t2 = new Thread(new CheckedRunnable() {
384 >        Thread t2 = newStartedThread(new CheckedRunnable() {
385              public void realRun() {
386                  lock.readLock().lock();
387                  lock.readLock().unlock();
388              }});
389  
395        t1.start();
396        t2.start();
390          lock.readLock().lock();
391          lock.readLock().unlock();
392          Thread.sleep(SHORT_DELAY_MS);
393          lock.readLock().lock();
394          lock.readLock().unlock();
395          lock.writeLock().unlock();
396 <        t1.join(MEDIUM_DELAY_MS);
397 <        t2.join(MEDIUM_DELAY_MS);
405 <        assertTrue(!t1.isAlive());
406 <        assertTrue(!t2.isAlive());
396 >        awaitTermination(t1, LONG_DELAY_MS);
397 >        awaitTermination(t2, LONG_DELAY_MS);
398      }
399  
400      /**
401 <     *  Read lock succeeds if write locked by current thread even if
401 >     * Read lock succeeds if write locked by current thread even if
402       * other threads are waiting for writelock
403       */
404      public void testReadHoldingWriteLock3() throws InterruptedException {
405          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
406          lock.writeLock().lock();
407 <        Thread t1 = new Thread(new CheckedRunnable() {
407 >        Thread t1 = newStartedThread(new CheckedRunnable() {
408              public void realRun() {
409                  lock.writeLock().lock();
410                  lock.writeLock().unlock();
411              }});
412 <        Thread t2 = new Thread(new CheckedRunnable() {
412 >        Thread t2 = newStartedThread(new CheckedRunnable() {
413              public void realRun() {
414                  lock.writeLock().lock();
415                  lock.writeLock().unlock();
416              }});
417  
427        t1.start();
428        t2.start();
418          lock.readLock().lock();
419          lock.readLock().unlock();
420          Thread.sleep(SHORT_DELAY_MS);
421          lock.readLock().lock();
422          lock.readLock().unlock();
423          lock.writeLock().unlock();
424 <        t1.join(MEDIUM_DELAY_MS);
425 <        t2.join(MEDIUM_DELAY_MS);
437 <        assertTrue(!t1.isAlive());
438 <        assertTrue(!t2.isAlive());
424 >        awaitTermination(t1, LONG_DELAY_MS);
425 >        awaitTermination(t2, LONG_DELAY_MS);
426      }
427  
428  
429      /**
430 <     *  Write lock succeeds if write locked by current thread even if
430 >     * Write lock succeeds if write locked by current thread even if
431       * other threads are waiting for writelock
432       */
433      public void testWriteHoldingWriteLock4() throws InterruptedException {
434          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
435          lock.writeLock().lock();
436 <        Thread t1 = new Thread(new CheckedRunnable() {
436 >        Thread t1 = newStartedThread(new CheckedRunnable() {
437              public void realRun() {
438                  lock.writeLock().lock();
439                  lock.writeLock().unlock();
440              }});
441 <        Thread t2 = new Thread(new CheckedRunnable() {
441 >        Thread t2 = newStartedThread(new CheckedRunnable() {
442              public void realRun() {
443                  lock.writeLock().lock();
444                  lock.writeLock().unlock();
445              }});
446  
460        t1.start();
461        t2.start();
447          lock.writeLock().lock();
448          lock.writeLock().unlock();
449          Thread.sleep(SHORT_DELAY_MS);
450          lock.writeLock().lock();
451          lock.writeLock().unlock();
452          lock.writeLock().unlock();
453 <        t1.join(MEDIUM_DELAY_MS);
454 <        t2.join(MEDIUM_DELAY_MS);
470 <        assertTrue(!t1.isAlive());
471 <        assertTrue(!t2.isAlive());
453 >        awaitTermination(t1, LONG_DELAY_MS);
454 >        awaitTermination(t2, LONG_DELAY_MS);
455      }
456  
457  
# Line 490 | Line 473 | public class ReentrantReadWriteLockTest
473      public void testReadHoldingWriteLockFair2() throws InterruptedException {
474          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
475          lock.writeLock().lock();
476 <        Thread t1 = new Thread(new CheckedRunnable() {
476 >        Thread t1 = newStartedThread(new CheckedRunnable() {
477              public void realRun() {
478                  lock.readLock().lock();
479                  lock.readLock().unlock();
480              }});
481 <        Thread t2 = new Thread(new CheckedRunnable() {
481 >        Thread t2 = newStartedThread(new CheckedRunnable() {
482              public void realRun() {
483                  lock.readLock().lock();
484                  lock.readLock().unlock();
485              }});
486  
504        t1.start();
505        t2.start();
487          lock.readLock().lock();
488          lock.readLock().unlock();
489          Thread.sleep(SHORT_DELAY_MS);
490          lock.readLock().lock();
491          lock.readLock().unlock();
492          lock.writeLock().unlock();
493 <        t1.join(MEDIUM_DELAY_MS);
494 <        t2.join(MEDIUM_DELAY_MS);
514 <        assertTrue(!t1.isAlive());
515 <        assertTrue(!t2.isAlive());
493 >        awaitTermination(t1, LONG_DELAY_MS);
494 >        awaitTermination(t2, LONG_DELAY_MS);
495      }
496  
497  
# Line 523 | Line 502 | public class ReentrantReadWriteLockTest
502      public void testReadHoldingWriteLockFair3() throws InterruptedException {
503          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
504          lock.writeLock().lock();
505 <        Thread t1 = new Thread(new CheckedRunnable() {
505 >        Thread t1 = newStartedThread(new CheckedRunnable() {
506              public void realRun() {
507                  lock.writeLock().lock();
508                  lock.writeLock().unlock();
509              }});
510 <        Thread t2 = new Thread(new CheckedRunnable() {
510 >        Thread t2 = newStartedThread(new CheckedRunnable() {
511              public void realRun() {
512                  lock.writeLock().lock();
513                  lock.writeLock().unlock();
514              }});
515  
537        t1.start();
538        t2.start();
516          lock.readLock().lock();
517          lock.readLock().unlock();
518          Thread.sleep(SHORT_DELAY_MS);
519          lock.readLock().lock();
520          lock.readLock().unlock();
521          lock.writeLock().unlock();
522 <        t1.join(MEDIUM_DELAY_MS);
523 <        t2.join(MEDIUM_DELAY_MS);
547 <        assertTrue(!t1.isAlive());
548 <        assertTrue(!t2.isAlive());
522 >        awaitTermination(t1, LONG_DELAY_MS);
523 >        awaitTermination(t2, LONG_DELAY_MS);
524      }
525  
526  
# Line 556 | Line 531 | public class ReentrantReadWriteLockTest
531      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
532          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
533          lock.writeLock().lock();
534 <        Thread t1 = new Thread(new CheckedRunnable() {
534 >        Thread t1 = newStartedThread(new CheckedRunnable() {
535              public void realRun() {
536                  lock.writeLock().lock();
537                  lock.writeLock().unlock();
538              }});
539 <        Thread t2 = new Thread(new CheckedRunnable() {
539 >        Thread t2 = newStartedThread(new CheckedRunnable() {
540              public void realRun() {
541                  lock.writeLock().lock();
542                  lock.writeLock().unlock();
543              }});
544  
570        t1.start();
571        t2.start();
545          Thread.sleep(SHORT_DELAY_MS);
546          assertTrue(lock.isWriteLockedByCurrentThread());
547 <        assertTrue(lock.getWriteHoldCount() == 1);
547 >        assertEquals(1, lock.getWriteHoldCount());
548          lock.writeLock().lock();
549 <        assertTrue(lock.getWriteHoldCount() == 2);
549 >        assertEquals(2, lock.getWriteHoldCount());
550          lock.writeLock().unlock();
551          lock.writeLock().lock();
552          lock.writeLock().unlock();
553          lock.writeLock().unlock();
554 <        t1.join(MEDIUM_DELAY_MS);
555 <        t2.join(MEDIUM_DELAY_MS);
583 <        assertTrue(!t1.isAlive());
584 <        assertTrue(!t2.isAlive());
554 >        awaitTermination(t1, LONG_DELAY_MS);
555 >        awaitTermination(t2, LONG_DELAY_MS);
556      }
557  
558  
# Line 591 | Line 562 | public class ReentrantReadWriteLockTest
562      public void testTryLockWhenReadLocked() throws InterruptedException {
563          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
564          lock.readLock().lock();
565 <        Thread t = new Thread(new CheckedRunnable() {
565 >        Thread t = newStartedThread(new CheckedRunnable() {
566              public void realRun() {
567 <                threadAssertTrue(lock.readLock().tryLock());
567 >                assertTrue(lock.readLock().tryLock());
568                  lock.readLock().unlock();
569              }});
570  
571 <        t.start();
601 <        t.join();
571 >        awaitTermination(t, LONG_DELAY_MS);
572          lock.readLock().unlock();
573      }
574  
605
606
575      /**
576       * write tryLock fails when readlocked
577       */
578      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
579          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
580          lock.readLock().lock();
581 <        Thread t = new Thread(new CheckedRunnable() {
581 >        Thread t = newStartedThread(new CheckedRunnable() {
582              public void realRun() {
583 <                threadAssertFalse(lock.writeLock().tryLock());
583 >                assertFalse(lock.writeLock().tryLock());
584              }});
585  
586 <        t.start();
619 <        t.join();
586 >        awaitTermination(t, LONG_DELAY_MS);
587          lock.readLock().unlock();
588      }
589  
# Line 627 | Line 594 | public class ReentrantReadWriteLockTest
594      public void testTryLockWhenReadLockedFair() throws InterruptedException {
595          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
596          lock.readLock().lock();
597 <        Thread t = new Thread(new CheckedRunnable() {
597 >        Thread t = newStartedThread(new CheckedRunnable() {
598              public void realRun() {
599 <                threadAssertTrue(lock.readLock().tryLock());
599 >                assertTrue(lock.readLock().tryLock());
600                  lock.readLock().unlock();
601              }});
602  
603 <        t.start();
637 <        t.join();
603 >        awaitTermination(t, LONG_DELAY_MS);
604          lock.readLock().unlock();
605      }
606  
# Line 646 | Line 612 | public class ReentrantReadWriteLockTest
612      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
613          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
614          lock.readLock().lock();
615 <        Thread t = new Thread(new CheckedRunnable() {
615 >        Thread t = newStartedThread(new CheckedRunnable() {
616              public void realRun() {
617 <                threadAssertFalse(lock.writeLock().tryLock());
617 >                assertFalse(lock.writeLock().tryLock());
618              }});
619  
620 <        t.start();
655 <        t.join();
620 >        awaitTermination(t, LONG_DELAY_MS);
621          lock.readLock().unlock();
622      }
623  
# Line 664 | Line 629 | public class ReentrantReadWriteLockTest
629      public void testWriteTryLock_Timeout() throws InterruptedException {
630          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
631          lock.writeLock().lock();
632 <        Thread t = new Thread(new CheckedRunnable() {
632 >        Thread t = newStartedThread(new CheckedRunnable() {
633              public void realRun() throws InterruptedException {
634 <                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
634 >                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
635              }});
636  
637 <        t.start();
673 <        t.join();
637 >        awaitTermination(t, LONG_DELAY_MS);
638          assertTrue(lock.writeLock().isHeldByCurrentThread());
639          lock.writeLock().unlock();
640      }
# Line 681 | Line 645 | public class ReentrantReadWriteLockTest
645      public void testReadTryLock_Timeout() throws InterruptedException {
646          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
647          lock.writeLock().lock();
648 <        Thread t = new Thread(new CheckedRunnable() {
648 >        Thread t = newStartedThread(new CheckedRunnable() {
649              public void realRun() throws InterruptedException {
650 <                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
650 >                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
651              }});
652  
653 <        t.start();
690 <        t.join();
653 >        awaitTermination(t, LONG_DELAY_MS);
654          assertTrue(lock.writeLock().isHeldByCurrentThread());
655          lock.writeLock().unlock();
656      }
# Line 699 | Line 662 | public class ReentrantReadWriteLockTest
662      public void testWriteLockInterruptibly() throws InterruptedException {
663          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
664          lock.writeLock().lockInterruptibly();
665 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
665 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
666              public void realRun() throws InterruptedException {
667                  lock.writeLock().lockInterruptibly();
668              }});
669  
707        t.start();
670          Thread.sleep(SHORT_DELAY_MS);
671          t.interrupt();
672 <        Thread.sleep(SHORT_DELAY_MS);
673 <        t.join();
712 <        lock.writeLock().unlock();
672 >        awaitTermination(t, LONG_DELAY_MS);
673 >        releaseWriteLock(lock);
674      }
675  
676      /**
677 <     *  read lockInterruptibly succeeds if lock free else is interruptible
677 >     * read lockInterruptibly succeeds if lock free else is interruptible
678       */
679      public void testReadLockInterruptibly() throws InterruptedException {
680          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
681          lock.writeLock().lockInterruptibly();
682 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
682 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
683              public void realRun() throws InterruptedException {
684                  lock.readLock().lockInterruptibly();
685              }});
686  
726        t.start();
687          Thread.sleep(SHORT_DELAY_MS);
688          t.interrupt();
689 <        t.join();
690 <        lock.writeLock().unlock();
689 >        awaitTermination(t, LONG_DELAY_MS);
690 >        releaseWriteLock(lock);
691      }
692  
693      /**
# Line 769 | Line 729 | public class ReentrantReadWriteLockTest
729  
730  
731      /**
732 <     *  timed await without a signal times out
732 >     * timed await without a signal times out
733       */
734      public void testAwait_Timeout() throws InterruptedException {
735          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 797 | Line 757 | public class ReentrantReadWriteLockTest
757      public void testAwait() throws InterruptedException {
758          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
759          final Condition c = lock.writeLock().newCondition();
760 <        Thread t = new Thread(new CheckedRunnable() {
760 >        Thread t = newStartedThread(new CheckedRunnable() {
761              public void realRun() throws InterruptedException {
762                  lock.writeLock().lock();
763                  c.await();
764                  lock.writeLock().unlock();
765              }});
766  
807        t.start();
767          Thread.sleep(SHORT_DELAY_MS);
768          lock.writeLock().lock();
769          c.signal();
770          lock.writeLock().unlock();
771 <        t.join(SHORT_DELAY_MS);
813 <        assertFalse(t.isAlive());
771 >        awaitTermination(t, LONG_DELAY_MS);
772      }
773  
774      /** A helper class for uninterruptible wait tests */
# Line 863 | Line 821 | public class ReentrantReadWriteLockTest
821              lock.writeLock().unlock();
822          }
823  
824 <        thread.join();
824 >        awaitTermination(thread, LONG_DELAY_MS);
825          assertTrue(thread.interrupted);
868        assertFalse(thread.isAlive());
826      }
827  
828      /**
# Line 874 | Line 831 | public class ReentrantReadWriteLockTest
831      public void testAwait_Interrupt() throws InterruptedException {
832          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
833          final Condition c = lock.writeLock().newCondition();
834 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
834 >        final CountDownLatch locked = new CountDownLatch(1);
835 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
836              public void realRun() throws InterruptedException {
837                  lock.writeLock().lock();
838 <                c.await();
839 <                lock.writeLock().unlock();
838 >                assertTrue(lock.isWriteLocked());
839 >                locked.countDown();
840 >                try { c.await(); }
841 >                finally { lock.writeLock().unlock(); }
842              }});
843  
844 <        t.start();
845 <        Thread.sleep(SHORT_DELAY_MS);
844 >        locked.await();
845 >        while (lock.isWriteLocked())
846 >            Thread.yield();
847          t.interrupt();
848 <        t.join(SHORT_DELAY_MS);
849 <        assertFalse(t.isAlive());
848 >        awaitTermination(t, LONG_DELAY_MS);
849 >        assertFalse(lock.isWriteLocked());
850      }
851  
852      /**
# Line 894 | Line 855 | public class ReentrantReadWriteLockTest
855      public void testAwaitNanos_Interrupt() throws InterruptedException {
856          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
857          final Condition c = lock.writeLock().newCondition();
858 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
858 >        final CountDownLatch locked = new CountDownLatch(1);
859 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
860              public void realRun() throws InterruptedException {
861                  lock.writeLock().lock();
862 <                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
863 <                lock.writeLock().unlock();
862 >                assertTrue(lock.isWriteLocked());
863 >                locked.countDown();
864 >                try { c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); }
865 >                finally { lock.writeLock().unlock(); }
866              }});
867  
868 <        t.start();
869 <        Thread.sleep(SHORT_DELAY_MS);
868 >        locked.await();
869 >        while (lock.isWriteLocked())
870 >            Thread.yield();
871          t.interrupt();
872 <        t.join(SHORT_DELAY_MS);
873 <        assertFalse(t.isAlive());
872 >        awaitTermination(t, LONG_DELAY_MS);
873 >        assertFalse(lock.isWriteLocked());
874      }
875  
876      /**
# Line 914 | Line 879 | public class ReentrantReadWriteLockTest
879      public void testAwaitUntil_Interrupt() throws InterruptedException {
880          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
881          final Condition c = lock.writeLock().newCondition();
882 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
882 >        final CountDownLatch locked = new CountDownLatch(1);
883 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
884              public void realRun() throws InterruptedException {
885                  lock.writeLock().lock();
886 +                assertTrue(lock.isWriteLocked());
887 +                locked.countDown();
888                  java.util.Date d = new java.util.Date();
889 <                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
890 <                lock.writeLock().unlock();
889 >                try { c.awaitUntil(new java.util.Date(d.getTime() + 10000)); }
890 >                finally { lock.writeLock().unlock(); }
891              }});
892  
893 <        t.start();
894 <        Thread.sleep(SHORT_DELAY_MS);
893 >        locked.await();
894 >        while (lock.isWriteLocked())
895 >            Thread.yield();
896          t.interrupt();
897 <        t.join(SHORT_DELAY_MS);
898 <        assertFalse(t.isAlive());
897 >        awaitTermination(t, LONG_DELAY_MS);
898 >        assertFalse(lock.isWriteLocked());
899      }
900  
901      /**
# Line 935 | Line 904 | public class ReentrantReadWriteLockTest
904      public void testSignalAll() throws InterruptedException {
905          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
906          final Condition c = lock.writeLock().newCondition();
907 <        Thread t1 = new Thread(new CheckedRunnable() {
907 >        Thread t1 = newStartedThread(new CheckedRunnable() {
908              public void realRun() throws InterruptedException {
909                  lock.writeLock().lock();
910                  c.await();
911                  lock.writeLock().unlock();
912              }});
913  
914 <        Thread t2 = new Thread(new CheckedRunnable() {
914 >        Thread t2 = newStartedThread(new CheckedRunnable() {
915              public void realRun() throws InterruptedException {
916                  lock.writeLock().lock();
917                  c.await();
918                  lock.writeLock().unlock();
919              }});
920  
952        t1.start();
953        t2.start();
921          Thread.sleep(SHORT_DELAY_MS);
922          lock.writeLock().lock();
923          c.signalAll();
924          lock.writeLock().unlock();
925 <        t1.join(SHORT_DELAY_MS);
926 <        t2.join(SHORT_DELAY_MS);
960 <        assertFalse(t1.isAlive());
961 <        assertFalse(t2.isAlive());
925 >        awaitTermination(t1, LONG_DELAY_MS);
926 >        awaitTermination(t2, LONG_DELAY_MS);
927      }
928  
929      /**
# Line 1002 | Line 967 | public class ReentrantReadWriteLockTest
967          lock.writeLock().unlock();
968          Thread.sleep(SHORT_DELAY_MS);
969          assertFalse(lock.hasQueuedThreads());
970 <        t1.join();
971 <        t2.join();
970 >        awaitTermination(t1, LONG_DELAY_MS);
971 >        awaitTermination(t2, LONG_DELAY_MS);
972      }
973  
974      /**
# Line 1043 | Line 1008 | public class ReentrantReadWriteLockTest
1008          assertFalse(sync.hasQueuedThread(t1));
1009          Thread.sleep(SHORT_DELAY_MS);
1010          assertFalse(sync.hasQueuedThread(t2));
1011 <        t1.join();
1012 <        t2.join();
1011 >        awaitTermination(t1, LONG_DELAY_MS);
1012 >        awaitTermination(t2, LONG_DELAY_MS);
1013      }
1014  
1015  
# Line 1069 | Line 1034 | public class ReentrantReadWriteLockTest
1034          lock.writeLock().unlock();
1035          Thread.sleep(SHORT_DELAY_MS);
1036          assertEquals(0, lock.getQueueLength());
1037 <        t1.join();
1038 <        t2.join();
1037 >        awaitTermination(t1, LONG_DELAY_MS);
1038 >        awaitTermination(t2, LONG_DELAY_MS);
1039      }
1040  
1041      /**
# Line 1097 | Line 1062 | public class ReentrantReadWriteLockTest
1062          lock.writeLock().unlock();
1063          Thread.sleep(SHORT_DELAY_MS);
1064          assertTrue(lock.getQueuedThreads().isEmpty());
1065 <        t1.join();
1066 <        t2.join();
1065 >        awaitTermination(t1, LONG_DELAY_MS);
1066 >        awaitTermination(t2, LONG_DELAY_MS);
1067      }
1068  
1069      /**
# Line 1219 | Line 1184 | public class ReentrantReadWriteLockTest
1184      public void testHasWaiters() throws InterruptedException {
1185          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1186          final Condition c = lock.writeLock().newCondition();
1187 <        Thread t = new Thread(new CheckedRunnable() {
1187 >        Thread t = newStartedThread(new CheckedRunnable() {
1188              public void realRun() throws InterruptedException {
1189                  lock.writeLock().lock();
1190 <                threadAssertFalse(lock.hasWaiters(c));
1191 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1190 >                assertFalse(lock.hasWaiters(c));
1191 >                assertEquals(0, lock.getWaitQueueLength(c));
1192                  c.await();
1193                  lock.writeLock().unlock();
1194              }});
1195  
1231        t.start();
1196          Thread.sleep(SHORT_DELAY_MS);
1197          lock.writeLock().lock();
1198          assertTrue(lock.hasWaiters(c));
# Line 1240 | Line 1204 | public class ReentrantReadWriteLockTest
1204          assertFalse(lock.hasWaiters(c));
1205          assertEquals(0, lock.getWaitQueueLength(c));
1206          lock.writeLock().unlock();
1207 <        t.join(SHORT_DELAY_MS);
1244 <        assertFalse(t.isAlive());
1207 >        awaitTermination(t, LONG_DELAY_MS);
1208      }
1209  
1210      /**
# Line 1250 | Line 1213 | public class ReentrantReadWriteLockTest
1213      public void testGetWaitQueueLength() throws InterruptedException {
1214          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1215          final Condition c = lock.writeLock().newCondition();
1216 <        Thread t = new Thread(new CheckedRunnable() {
1216 >        Thread t = newStartedThread(new CheckedRunnable() {
1217              public void realRun() throws InterruptedException {
1218                  lock.writeLock().lock();
1219 <                threadAssertFalse(lock.hasWaiters(c));
1220 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1219 >                assertFalse(lock.hasWaiters(c));
1220 >                assertEquals(0, lock.getWaitQueueLength(c));
1221                  c.await();
1222                  lock.writeLock().unlock();
1223              }});
1224  
1262        t.start();
1225          Thread.sleep(SHORT_DELAY_MS);
1226          lock.writeLock().lock();
1227          assertTrue(lock.hasWaiters(c));
# Line 1271 | Line 1233 | public class ReentrantReadWriteLockTest
1233          assertFalse(lock.hasWaiters(c));
1234          assertEquals(0, lock.getWaitQueueLength(c));
1235          lock.writeLock().unlock();
1236 <        t.join(SHORT_DELAY_MS);
1275 <        assertFalse(t.isAlive());
1236 >        awaitTermination(t, LONG_DELAY_MS);
1237      }
1238  
1239  
# Line 1285 | Line 1246 | public class ReentrantReadWriteLockTest
1246          Thread t1 = new Thread(new CheckedRunnable() {
1247              public void realRun() throws InterruptedException {
1248                  lock.writeLock().lock();
1249 <                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1249 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
1250                  c.await();
1251                  lock.writeLock().unlock();
1252              }});
# Line 1293 | Line 1254 | public class ReentrantReadWriteLockTest
1254          Thread t2 = new Thread(new CheckedRunnable() {
1255              public void realRun() throws InterruptedException {
1256                  lock.writeLock().lock();
1257 <                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1257 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
1258                  c.await();
1259                  lock.writeLock().unlock();
1260              }});
# Line 1316 | Line 1277 | public class ReentrantReadWriteLockTest
1277          assertFalse(lock.hasWaiters(c));
1278          assertTrue(lock.getWaitingThreads(c).isEmpty());
1279          lock.writeLock().unlock();
1280 <        t1.join(SHORT_DELAY_MS);
1281 <        t2.join(SHORT_DELAY_MS);
1321 <        assertFalse(t1.isAlive());
1322 <        assertFalse(t2.isAlive());
1280 >        awaitTermination(t1, LONG_DELAY_MS);
1281 >        awaitTermination(t2, LONG_DELAY_MS);
1282      }
1283  
1284      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines