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.36 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.54 by jsr166, Mon May 2 01:07:15 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 Runnable() {
274 <                public void run() {
275 <                    threadAssertFalse(lock.writeLock().tryLock());
276 <                }
273 <            });
273 >        Thread t = newStartedThread(new CheckedRunnable() {
274 >            public void realRun() {
275 >                assertFalse(lock.writeLock().tryLock());
276 >            }});
277  
278 <        t.start();
279 <        t.join();
277 <        lock.writeLock().unlock();
278 >        awaitTermination(t, LONG_DELAY_MS);
279 >        releaseWriteLock(lock);
280      }
281  
282      /**
# Line 283 | 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 Runnable() {
289 <                public void run() {
290 <                    threadAssertFalse(lock.readLock().tryLock());
291 <                }
290 <            });
288 >        Thread t = newStartedThread(new CheckedRunnable() {
289 >            public void realRun() {
290 >                assertFalse(lock.readLock().tryLock());
291 >            }});
292  
293 <        t.start();
294 <        t.join();
294 <        lock.writeLock().unlock();
293 >        awaitTermination(t, LONG_DELAY_MS);
294 >        releaseWriteLock(lock);
295      }
296  
297      /**
# Line 300 | 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 Runnable() {
304 <                public void run() {
305 <                    threadAssertTrue(lock.readLock().tryLock());
306 <                    lock.readLock().unlock();
307 <                }
308 <            });
303 >        Thread t = newStartedThread(new CheckedRunnable() {
304 >            public void realRun() {
305 >                assertTrue(lock.readLock().tryLock());
306 >                lock.readLock().unlock();
307 >            }});
308  
309 <        t.start();
311 <        t.join();
309 >        awaitTermination(t, LONG_DELAY_MS);
310          lock.readLock().unlock();
311      }
312  
# Line 318 | 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 Runnable() {
320 <                public void run() {
321 <                    lock.readLock().lock();
322 <                    lock.readLock().unlock();
323 <                }
324 <            });
325 <        Thread t2 = new Thread(new Runnable() {
326 <                public void run() {
327 <                    lock.writeLock().lock();
328 <                    lock.writeLock().unlock();
331 <                }
332 <            });
319 >        Thread t1 = newStartedThread(new CheckedRunnable() {
320 >            public void realRun() {
321 >                lock.readLock().lock();
322 >                lock.readLock().unlock();
323 >            }});
324 >        Thread t2 = newStartedThread(new CheckedRunnable() {
325 >            public void realRun() {
326 >                lock.writeLock().lock();
327 >                lock.writeLock().unlock();
328 >            }});
329  
334        t1.start();
335        t2.start();
330          Thread.sleep(SHORT_DELAY_MS);
331          lock.readLock().unlock();
332 <        t1.join(MEDIUM_DELAY_MS);
333 <        t2.join(MEDIUM_DELAY_MS);
340 <        assertTrue(!t1.isAlive());
341 <        assertTrue(!t2.isAlive());
332 >        awaitTermination(t1, LONG_DELAY_MS);
333 >        awaitTermination(t2, LONG_DELAY_MS);
334      }
335  
336      /**
# Line 347 | 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 Runnable() {
343 <                public void run() {
344 <                    lock.readLock().lock();
345 <                    lock.readLock().unlock();
346 <                }
347 <            });
348 <        Thread t2 = new Thread(new Runnable() {
349 <                public void run() {
350 <                    lock.readLock().lock();
351 <                    lock.readLock().unlock();
360 <                }
361 <            });
342 >        Thread t1 = newStartedThread(new CheckedRunnable() {
343 >            public void realRun() {
344 >                lock.readLock().lock();
345 >                lock.readLock().unlock();
346 >            }});
347 >        Thread t2 = newStartedThread(new CheckedRunnable() {
348 >            public void realRun() {
349 >                lock.readLock().lock();
350 >                lock.readLock().unlock();
351 >            }});
352  
363        t1.start();
364        t2.start();
353          Thread.sleep(SHORT_DELAY_MS);
354 <        lock.writeLock().unlock();
355 <        t1.join(MEDIUM_DELAY_MS);
356 <        t2.join(MEDIUM_DELAY_MS);
369 <        assertTrue(!t1.isAlive());
370 <        assertTrue(!t2.isAlive());
354 >        releaseWriteLock(lock);
355 >        awaitTermination(t1, LONG_DELAY_MS);
356 >        awaitTermination(t2, LONG_DELAY_MS);
357      }
358  
359      /**
# Line 388 | Line 374 | public class ReentrantReadWriteLockTest
374      public void testReadHoldingWriteLock2() throws InterruptedException {
375          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
376          lock.writeLock().lock();
377 <        Thread t1 = new Thread(new Runnable() {
378 <                public void run() {
379 <                    lock.readLock().lock();
380 <                    lock.readLock().unlock();
381 <                }
382 <            });
383 <        Thread t2 = new Thread(new Runnable() {
384 <                public void run() {
385 <                    lock.readLock().lock();
386 <                    lock.readLock().unlock();
401 <                }
402 <            });
377 >        Thread t1 = newStartedThread(new CheckedRunnable() {
378 >            public void realRun() {
379 >                lock.readLock().lock();
380 >                lock.readLock().unlock();
381 >            }});
382 >        Thread t2 = newStartedThread(new CheckedRunnable() {
383 >            public void realRun() {
384 >                lock.readLock().lock();
385 >                lock.readLock().unlock();
386 >            }});
387  
404        t1.start();
405        t2.start();
388          lock.readLock().lock();
389          lock.readLock().unlock();
390          Thread.sleep(SHORT_DELAY_MS);
391          lock.readLock().lock();
392          lock.readLock().unlock();
393          lock.writeLock().unlock();
394 <        t1.join(MEDIUM_DELAY_MS);
395 <        t2.join(MEDIUM_DELAY_MS);
414 <        assertTrue(!t1.isAlive());
415 <        assertTrue(!t2.isAlive());
394 >        awaitTermination(t1, LONG_DELAY_MS);
395 >        awaitTermination(t2, LONG_DELAY_MS);
396      }
397  
398      /**
399 <     *  Read lock succeeds if write locked by current thread even if
399 >     * Read lock succeeds if write locked by current thread even if
400       * other threads are waiting for writelock
401       */
402      public void testReadHoldingWriteLock3() throws InterruptedException {
403          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
404          lock.writeLock().lock();
405 <        Thread t1 = new Thread(new Runnable() {
406 <                public void run() {
407 <                    lock.writeLock().lock();
408 <                    lock.writeLock().unlock();
409 <                }
410 <            });
411 <        Thread t2 = new Thread(new Runnable() {
412 <                public void run() {
413 <                    lock.writeLock().lock();
414 <                    lock.writeLock().unlock();
435 <                }
436 <            });
405 >        Thread t1 = newStartedThread(new CheckedRunnable() {
406 >            public void realRun() {
407 >                lock.writeLock().lock();
408 >                lock.writeLock().unlock();
409 >            }});
410 >        Thread t2 = newStartedThread(new CheckedRunnable() {
411 >            public void realRun() {
412 >                lock.writeLock().lock();
413 >                lock.writeLock().unlock();
414 >            }});
415  
438        t1.start();
439        t2.start();
416          lock.readLock().lock();
417          lock.readLock().unlock();
418          Thread.sleep(SHORT_DELAY_MS);
419          lock.readLock().lock();
420          lock.readLock().unlock();
421          lock.writeLock().unlock();
422 <        t1.join(MEDIUM_DELAY_MS);
423 <        t2.join(MEDIUM_DELAY_MS);
448 <        assertTrue(!t1.isAlive());
449 <        assertTrue(!t2.isAlive());
422 >        awaitTermination(t1, LONG_DELAY_MS);
423 >        awaitTermination(t2, LONG_DELAY_MS);
424      }
425  
426  
427      /**
428 <     *  Write lock succeeds if write locked by current thread even if
428 >     * Write lock succeeds if write locked by current thread even if
429       * other threads are waiting for writelock
430       */
431      public void testWriteHoldingWriteLock4() throws InterruptedException {
432          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
433          lock.writeLock().lock();
434 <        Thread t1 = new Thread(new Runnable() {
435 <                public void run() {
436 <                    lock.writeLock().lock();
437 <                    lock.writeLock().unlock();
438 <                }
439 <            });
440 <        Thread t2 = new Thread(new Runnable() {
441 <                public void run() {
442 <                    lock.writeLock().lock();
443 <                    lock.writeLock().unlock();
470 <                }
471 <            });
434 >        Thread t1 = newStartedThread(new CheckedRunnable() {
435 >            public void realRun() {
436 >                lock.writeLock().lock();
437 >                lock.writeLock().unlock();
438 >            }});
439 >        Thread t2 = newStartedThread(new CheckedRunnable() {
440 >            public void realRun() {
441 >                lock.writeLock().lock();
442 >                lock.writeLock().unlock();
443 >            }});
444  
473        t1.start();
474        t2.start();
445          lock.writeLock().lock();
446          lock.writeLock().unlock();
447          Thread.sleep(SHORT_DELAY_MS);
448          lock.writeLock().lock();
449          lock.writeLock().unlock();
450          lock.writeLock().unlock();
451 <        t1.join(MEDIUM_DELAY_MS);
452 <        t2.join(MEDIUM_DELAY_MS);
483 <        assertTrue(!t1.isAlive());
484 <        assertTrue(!t2.isAlive());
451 >        awaitTermination(t1, LONG_DELAY_MS);
452 >        awaitTermination(t2, LONG_DELAY_MS);
453      }
454  
455  
# Line 503 | Line 471 | public class ReentrantReadWriteLockTest
471      public void testReadHoldingWriteLockFair2() throws InterruptedException {
472          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
473          lock.writeLock().lock();
474 <        Thread t1 = new Thread(new Runnable() {
475 <                public void run() {
476 <                    lock.readLock().lock();
477 <                    lock.readLock().unlock();
478 <                }
479 <            });
480 <        Thread t2 = new Thread(new Runnable() {
481 <                public void run() {
482 <                    lock.readLock().lock();
483 <                    lock.readLock().unlock();
516 <                }
517 <            });
474 >        Thread t1 = newStartedThread(new CheckedRunnable() {
475 >            public void realRun() {
476 >                lock.readLock().lock();
477 >                lock.readLock().unlock();
478 >            }});
479 >        Thread t2 = newStartedThread(new CheckedRunnable() {
480 >            public void realRun() {
481 >                lock.readLock().lock();
482 >                lock.readLock().unlock();
483 >            }});
484  
519        t1.start();
520        t2.start();
485          lock.readLock().lock();
486          lock.readLock().unlock();
487          Thread.sleep(SHORT_DELAY_MS);
488          lock.readLock().lock();
489          lock.readLock().unlock();
490          lock.writeLock().unlock();
491 <        t1.join(MEDIUM_DELAY_MS);
492 <        t2.join(MEDIUM_DELAY_MS);
529 <        assertTrue(!t1.isAlive());
530 <        assertTrue(!t2.isAlive());
491 >        awaitTermination(t1, LONG_DELAY_MS);
492 >        awaitTermination(t2, LONG_DELAY_MS);
493      }
494  
495  
# Line 538 | Line 500 | public class ReentrantReadWriteLockTest
500      public void testReadHoldingWriteLockFair3() throws InterruptedException {
501          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
502          lock.writeLock().lock();
503 <        Thread t1 = new Thread(new Runnable() {
504 <                public void run() {
505 <                    lock.writeLock().lock();
506 <                    lock.writeLock().unlock();
507 <                }
508 <            });
509 <        Thread t2 = new Thread(new Runnable() {
510 <                public void run() {
511 <                    lock.writeLock().lock();
512 <                    lock.writeLock().unlock();
551 <                }
552 <            });
503 >        Thread t1 = newStartedThread(new CheckedRunnable() {
504 >            public void realRun() {
505 >                lock.writeLock().lock();
506 >                lock.writeLock().unlock();
507 >            }});
508 >        Thread t2 = newStartedThread(new CheckedRunnable() {
509 >            public void realRun() {
510 >                lock.writeLock().lock();
511 >                lock.writeLock().unlock();
512 >            }});
513  
554        t1.start();
555        t2.start();
514          lock.readLock().lock();
515          lock.readLock().unlock();
516          Thread.sleep(SHORT_DELAY_MS);
517          lock.readLock().lock();
518          lock.readLock().unlock();
519          lock.writeLock().unlock();
520 <        t1.join(MEDIUM_DELAY_MS);
521 <        t2.join(MEDIUM_DELAY_MS);
564 <        assertTrue(!t1.isAlive());
565 <        assertTrue(!t2.isAlive());
520 >        awaitTermination(t1, LONG_DELAY_MS);
521 >        awaitTermination(t2, LONG_DELAY_MS);
522      }
523  
524  
# Line 573 | Line 529 | public class ReentrantReadWriteLockTest
529      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
530          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
531          lock.writeLock().lock();
532 <        Thread t1 = new Thread(new Runnable() {
533 <                public void run() {
534 <                    lock.writeLock().lock();
535 <                    lock.writeLock().unlock();
536 <                }
537 <            });
538 <        Thread t2 = new Thread(new Runnable() {
539 <                public void run() {
540 <                    lock.writeLock().lock();
541 <                    lock.writeLock().unlock();
586 <                }
587 <            });
532 >        Thread t1 = newStartedThread(new CheckedRunnable() {
533 >            public void realRun() {
534 >                lock.writeLock().lock();
535 >                lock.writeLock().unlock();
536 >            }});
537 >        Thread t2 = newStartedThread(new CheckedRunnable() {
538 >            public void realRun() {
539 >                lock.writeLock().lock();
540 >                lock.writeLock().unlock();
541 >            }});
542  
589        t1.start();
590        t2.start();
543          Thread.sleep(SHORT_DELAY_MS);
544          assertTrue(lock.isWriteLockedByCurrentThread());
545 <        assertTrue(lock.getWriteHoldCount() == 1);
545 >        assertEquals(1, lock.getWriteHoldCount());
546          lock.writeLock().lock();
547 <        assertTrue(lock.getWriteHoldCount() == 2);
547 >        assertEquals(2, lock.getWriteHoldCount());
548          lock.writeLock().unlock();
549          lock.writeLock().lock();
550          lock.writeLock().unlock();
551          lock.writeLock().unlock();
552 <        t1.join(MEDIUM_DELAY_MS);
553 <        t2.join(MEDIUM_DELAY_MS);
602 <        assertTrue(!t1.isAlive());
603 <        assertTrue(!t2.isAlive());
552 >        awaitTermination(t1, LONG_DELAY_MS);
553 >        awaitTermination(t2, LONG_DELAY_MS);
554      }
555  
556  
# Line 610 | Line 560 | public class ReentrantReadWriteLockTest
560      public void testTryLockWhenReadLocked() throws InterruptedException {
561          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
562          lock.readLock().lock();
563 <        Thread t = new Thread(new Runnable() {
564 <                public void run() {
565 <                    threadAssertTrue(lock.readLock().tryLock());
566 <                    lock.readLock().unlock();
567 <                }
618 <            });
563 >        Thread t = newStartedThread(new CheckedRunnable() {
564 >            public void realRun() {
565 >                assertTrue(lock.readLock().tryLock());
566 >                lock.readLock().unlock();
567 >            }});
568  
569 <        t.start();
621 <        t.join();
569 >        awaitTermination(t, LONG_DELAY_MS);
570          lock.readLock().unlock();
571      }
572  
625
626
573      /**
574       * write tryLock fails when readlocked
575       */
576      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
577          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
578          lock.readLock().lock();
579 <        Thread t = new Thread(new Runnable() {
580 <                public void run() {
581 <                    threadAssertFalse(lock.writeLock().tryLock());
582 <                }
637 <            });
579 >        Thread t = newStartedThread(new CheckedRunnable() {
580 >            public void realRun() {
581 >                assertFalse(lock.writeLock().tryLock());
582 >            }});
583  
584 <        t.start();
640 <        t.join();
584 >        awaitTermination(t, LONG_DELAY_MS);
585          lock.readLock().unlock();
586      }
587  
# Line 648 | Line 592 | public class ReentrantReadWriteLockTest
592      public void testTryLockWhenReadLockedFair() throws InterruptedException {
593          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
594          lock.readLock().lock();
595 <        Thread t = new Thread(new Runnable() {
596 <                public void run() {
597 <                    threadAssertTrue(lock.readLock().tryLock());
598 <                    lock.readLock().unlock();
599 <                }
656 <            });
595 >        Thread t = newStartedThread(new CheckedRunnable() {
596 >            public void realRun() {
597 >                assertTrue(lock.readLock().tryLock());
598 >                lock.readLock().unlock();
599 >            }});
600  
601 <        t.start();
659 <        t.join();
601 >        awaitTermination(t, LONG_DELAY_MS);
602          lock.readLock().unlock();
603      }
604  
# Line 668 | Line 610 | public class ReentrantReadWriteLockTest
610      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
611          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
612          lock.readLock().lock();
613 <        Thread t = new Thread(new Runnable() {
614 <                public void run() {
615 <                    threadAssertFalse(lock.writeLock().tryLock());
616 <                }
675 <            });
613 >        Thread t = newStartedThread(new CheckedRunnable() {
614 >            public void realRun() {
615 >                assertFalse(lock.writeLock().tryLock());
616 >            }});
617  
618 <        t.start();
678 <        t.join();
618 >        awaitTermination(t, LONG_DELAY_MS);
619          lock.readLock().unlock();
620      }
621  
# Line 687 | Line 627 | public class ReentrantReadWriteLockTest
627      public void testWriteTryLock_Timeout() throws InterruptedException {
628          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
629          lock.writeLock().lock();
630 <        Thread t = new Thread(new CheckedRunnable() {
630 >        Thread t = newStartedThread(new CheckedRunnable() {
631              public void realRun() throws InterruptedException {
632 <                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
632 >                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
633              }});
634  
635 <        t.start();
696 <        t.join();
635 >        awaitTermination(t, LONG_DELAY_MS);
636          assertTrue(lock.writeLock().isHeldByCurrentThread());
637          lock.writeLock().unlock();
638      }
# Line 704 | Line 643 | public class ReentrantReadWriteLockTest
643      public void testReadTryLock_Timeout() throws InterruptedException {
644          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
645          lock.writeLock().lock();
646 <        Thread t = new Thread(new CheckedRunnable() {
646 >        Thread t = newStartedThread(new CheckedRunnable() {
647              public void realRun() throws InterruptedException {
648 <                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
648 >                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
649              }});
650  
651 <        t.start();
713 <        t.join();
651 >        awaitTermination(t, LONG_DELAY_MS);
652          assertTrue(lock.writeLock().isHeldByCurrentThread());
653          lock.writeLock().unlock();
654      }
# Line 722 | Line 660 | public class ReentrantReadWriteLockTest
660      public void testWriteLockInterruptibly() throws InterruptedException {
661          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
662          lock.writeLock().lockInterruptibly();
663 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
663 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
664              public void realRun() throws InterruptedException {
665                  lock.writeLock().lockInterruptibly();
666              }});
667  
730        t.start();
668          Thread.sleep(SHORT_DELAY_MS);
669          t.interrupt();
670 <        Thread.sleep(SHORT_DELAY_MS);
671 <        t.join();
735 <        lock.writeLock().unlock();
670 >        awaitTermination(t, LONG_DELAY_MS);
671 >        releaseWriteLock(lock);
672      }
673  
674      /**
675 <     *  read lockInterruptibly succeeds if lock free else is interruptible
675 >     * read lockInterruptibly succeeds if lock free else is interruptible
676       */
677      public void testReadLockInterruptibly() throws InterruptedException {
678          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
679          lock.writeLock().lockInterruptibly();
680 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
680 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
681              public void realRun() throws InterruptedException {
682                  lock.readLock().lockInterruptibly();
683              }});
684  
749        t.start();
685          Thread.sleep(SHORT_DELAY_MS);
686          t.interrupt();
687 <        t.join();
688 <        lock.writeLock().unlock();
687 >        awaitTermination(t, LONG_DELAY_MS);
688 >        releaseWriteLock(lock);
689      }
690  
691      /**
# Line 792 | Line 727 | public class ReentrantReadWriteLockTest
727  
728  
729      /**
730 <     *  timed await without a signal times out
730 >     * timed await without a signal times out
731       */
732      public void testAwait_Timeout() throws InterruptedException {
733          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 820 | Line 755 | public class ReentrantReadWriteLockTest
755      public void testAwait() throws InterruptedException {
756          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
757          final Condition c = lock.writeLock().newCondition();
758 <        Thread t = new Thread(new CheckedRunnable() {
758 >        Thread t = newStartedThread(new CheckedRunnable() {
759              public void realRun() throws InterruptedException {
760                  lock.writeLock().lock();
761                  c.await();
762                  lock.writeLock().unlock();
763              }});
764  
830        t.start();
765          Thread.sleep(SHORT_DELAY_MS);
766          lock.writeLock().lock();
767          c.signal();
768          lock.writeLock().unlock();
769 <        t.join(SHORT_DELAY_MS);
836 <        assertFalse(t.isAlive());
769 >        awaitTermination(t, LONG_DELAY_MS);
770      }
771  
772      /** A helper class for uninterruptible wait tests */
# Line 886 | Line 819 | public class ReentrantReadWriteLockTest
819              lock.writeLock().unlock();
820          }
821  
822 <        thread.join();
822 >        awaitTermination(thread, LONG_DELAY_MS);
823          assertTrue(thread.interrupted);
891        assertFalse(thread.isAlive());
824      }
825  
826      /**
# Line 897 | Line 829 | public class ReentrantReadWriteLockTest
829      public void testAwait_Interrupt() throws InterruptedException {
830          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
831          final Condition c = lock.writeLock().newCondition();
832 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
832 >        final CountDownLatch locked = new CountDownLatch(1);
833 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
834              public void realRun() throws InterruptedException {
835                  lock.writeLock().lock();
836 <                c.await();
837 <                lock.writeLock().unlock();
836 >                assertTrue(lock.isWriteLocked());
837 >                locked.countDown();
838 >                try { c.await(); }
839 >                finally { lock.writeLock().unlock(); }
840              }});
841  
842 <        t.start();
843 <        Thread.sleep(SHORT_DELAY_MS);
842 >        locked.await();
843 >        while (lock.isWriteLocked())
844 >            Thread.yield();
845          t.interrupt();
846 <        t.join(SHORT_DELAY_MS);
847 <        assertFalse(t.isAlive());
846 >        awaitTermination(t, LONG_DELAY_MS);
847 >        assertFalse(lock.isWriteLocked());
848      }
849  
850      /**
# Line 917 | Line 853 | public class ReentrantReadWriteLockTest
853      public void testAwaitNanos_Interrupt() throws InterruptedException {
854          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
855          final Condition c = lock.writeLock().newCondition();
856 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
856 >        final CountDownLatch locked = new CountDownLatch(1);
857 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
858              public void realRun() throws InterruptedException {
859                  lock.writeLock().lock();
860 <                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
861 <                lock.writeLock().unlock();
860 >                assertTrue(lock.isWriteLocked());
861 >                locked.countDown();
862 >                try { c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); }
863 >                finally { lock.writeLock().unlock(); }
864              }});
865  
866 <        t.start();
867 <        Thread.sleep(SHORT_DELAY_MS);
866 >        locked.await();
867 >        while (lock.isWriteLocked())
868 >            Thread.yield();
869          t.interrupt();
870 <        t.join(SHORT_DELAY_MS);
871 <        assertFalse(t.isAlive());
870 >        awaitTermination(t, LONG_DELAY_MS);
871 >        assertFalse(lock.isWriteLocked());
872      }
873  
874      /**
# Line 937 | Line 877 | public class ReentrantReadWriteLockTest
877      public void testAwaitUntil_Interrupt() throws InterruptedException {
878          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
879          final Condition c = lock.writeLock().newCondition();
880 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
880 >        final CountDownLatch locked = new CountDownLatch(1);
881 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
882              public void realRun() throws InterruptedException {
883                  lock.writeLock().lock();
884 +                assertTrue(lock.isWriteLocked());
885 +                locked.countDown();
886                  java.util.Date d = new java.util.Date();
887 <                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
888 <                lock.writeLock().unlock();
887 >                try { c.awaitUntil(new java.util.Date(d.getTime() + 10000)); }
888 >                finally { lock.writeLock().unlock(); }
889              }});
890  
891 <        t.start();
892 <        Thread.sleep(SHORT_DELAY_MS);
891 >        locked.await();
892 >        while (lock.isWriteLocked())
893 >            Thread.yield();
894          t.interrupt();
895 <        t.join(SHORT_DELAY_MS);
896 <        assertFalse(t.isAlive());
895 >        awaitTermination(t, LONG_DELAY_MS);
896 >        assertFalse(lock.isWriteLocked());
897      }
898  
899      /**
# Line 958 | Line 902 | public class ReentrantReadWriteLockTest
902      public void testSignalAll() throws InterruptedException {
903          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
904          final Condition c = lock.writeLock().newCondition();
905 <        Thread t1 = new Thread(new CheckedRunnable() {
905 >        Thread t1 = newStartedThread(new CheckedRunnable() {
906              public void realRun() throws InterruptedException {
907                  lock.writeLock().lock();
908                  c.await();
909                  lock.writeLock().unlock();
910              }});
911  
912 <        Thread t2 = new Thread(new CheckedRunnable() {
912 >        Thread t2 = newStartedThread(new CheckedRunnable() {
913              public void realRun() throws InterruptedException {
914                  lock.writeLock().lock();
915                  c.await();
916                  lock.writeLock().unlock();
917              }});
918  
975        t1.start();
976        t2.start();
919          Thread.sleep(SHORT_DELAY_MS);
920          lock.writeLock().lock();
921          c.signalAll();
922          lock.writeLock().unlock();
923 <        t1.join(SHORT_DELAY_MS);
924 <        t2.join(SHORT_DELAY_MS);
983 <        assertFalse(t1.isAlive());
984 <        assertFalse(t2.isAlive());
923 >        awaitTermination(t1, LONG_DELAY_MS);
924 >        awaitTermination(t2, LONG_DELAY_MS);
925      }
926  
927      /**
# Line 1025 | Line 965 | public class ReentrantReadWriteLockTest
965          lock.writeLock().unlock();
966          Thread.sleep(SHORT_DELAY_MS);
967          assertFalse(lock.hasQueuedThreads());
968 <        t1.join();
969 <        t2.join();
968 >        awaitTermination(t1, LONG_DELAY_MS);
969 >        awaitTermination(t2, LONG_DELAY_MS);
970      }
971  
972      /**
# Line 1066 | Line 1006 | public class ReentrantReadWriteLockTest
1006          assertFalse(sync.hasQueuedThread(t1));
1007          Thread.sleep(SHORT_DELAY_MS);
1008          assertFalse(sync.hasQueuedThread(t2));
1009 <        t1.join();
1010 <        t2.join();
1009 >        awaitTermination(t1, LONG_DELAY_MS);
1010 >        awaitTermination(t2, LONG_DELAY_MS);
1011      }
1012  
1013  
# Line 1092 | Line 1032 | public class ReentrantReadWriteLockTest
1032          lock.writeLock().unlock();
1033          Thread.sleep(SHORT_DELAY_MS);
1034          assertEquals(0, lock.getQueueLength());
1035 <        t1.join();
1036 <        t2.join();
1035 >        awaitTermination(t1, LONG_DELAY_MS);
1036 >        awaitTermination(t2, LONG_DELAY_MS);
1037      }
1038  
1039      /**
# Line 1120 | Line 1060 | public class ReentrantReadWriteLockTest
1060          lock.writeLock().unlock();
1061          Thread.sleep(SHORT_DELAY_MS);
1062          assertTrue(lock.getQueuedThreads().isEmpty());
1063 <        t1.join();
1064 <        t2.join();
1063 >        awaitTermination(t1, LONG_DELAY_MS);
1064 >        awaitTermination(t2, LONG_DELAY_MS);
1065      }
1066  
1067      /**
# Line 1242 | Line 1182 | public class ReentrantReadWriteLockTest
1182      public void testHasWaiters() throws InterruptedException {
1183          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1184          final Condition c = lock.writeLock().newCondition();
1185 <        Thread t = new Thread(new CheckedRunnable() {
1185 >        Thread t = newStartedThread(new CheckedRunnable() {
1186              public void realRun() throws InterruptedException {
1187                  lock.writeLock().lock();
1188 <                threadAssertFalse(lock.hasWaiters(c));
1189 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1188 >                assertFalse(lock.hasWaiters(c));
1189 >                assertEquals(0, lock.getWaitQueueLength(c));
1190                  c.await();
1191                  lock.writeLock().unlock();
1192              }});
1193  
1254        t.start();
1194          Thread.sleep(SHORT_DELAY_MS);
1195          lock.writeLock().lock();
1196          assertTrue(lock.hasWaiters(c));
# Line 1263 | Line 1202 | public class ReentrantReadWriteLockTest
1202          assertFalse(lock.hasWaiters(c));
1203          assertEquals(0, lock.getWaitQueueLength(c));
1204          lock.writeLock().unlock();
1205 <        t.join(SHORT_DELAY_MS);
1267 <        assertFalse(t.isAlive());
1205 >        awaitTermination(t, LONG_DELAY_MS);
1206      }
1207  
1208      /**
# Line 1273 | Line 1211 | public class ReentrantReadWriteLockTest
1211      public void testGetWaitQueueLength() throws InterruptedException {
1212          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1213          final Condition c = lock.writeLock().newCondition();
1214 <        Thread t = new Thread(new CheckedRunnable() {
1214 >        Thread t = newStartedThread(new CheckedRunnable() {
1215              public void realRun() throws InterruptedException {
1216                  lock.writeLock().lock();
1217 <                threadAssertFalse(lock.hasWaiters(c));
1218 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1217 >                assertFalse(lock.hasWaiters(c));
1218 >                assertEquals(0, lock.getWaitQueueLength(c));
1219                  c.await();
1220                  lock.writeLock().unlock();
1221              }});
1222  
1285        t.start();
1223          Thread.sleep(SHORT_DELAY_MS);
1224          lock.writeLock().lock();
1225          assertTrue(lock.hasWaiters(c));
# Line 1294 | Line 1231 | public class ReentrantReadWriteLockTest
1231          assertFalse(lock.hasWaiters(c));
1232          assertEquals(0, lock.getWaitQueueLength(c));
1233          lock.writeLock().unlock();
1234 <        t.join(SHORT_DELAY_MS);
1298 <        assertFalse(t.isAlive());
1234 >        awaitTermination(t, LONG_DELAY_MS);
1235      }
1236  
1237  
# Line 1308 | Line 1244 | public class ReentrantReadWriteLockTest
1244          Thread t1 = new Thread(new CheckedRunnable() {
1245              public void realRun() throws InterruptedException {
1246                  lock.writeLock().lock();
1247 <                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1247 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
1248                  c.await();
1249                  lock.writeLock().unlock();
1250              }});
# Line 1316 | Line 1252 | public class ReentrantReadWriteLockTest
1252          Thread t2 = new Thread(new CheckedRunnable() {
1253              public void realRun() throws InterruptedException {
1254                  lock.writeLock().lock();
1255 <                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1255 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
1256                  c.await();
1257                  lock.writeLock().unlock();
1258              }});
# Line 1339 | Line 1275 | public class ReentrantReadWriteLockTest
1275          assertFalse(lock.hasWaiters(c));
1276          assertTrue(lock.getWaitingThreads(c).isEmpty());
1277          lock.writeLock().unlock();
1278 <        t1.join(SHORT_DELAY_MS);
1279 <        t2.join(SHORT_DELAY_MS);
1344 <        assertFalse(t1.isAlive());
1345 <        assertFalse(t2.isAlive());
1278 >        awaitTermination(t1, LONG_DELAY_MS);
1279 >        awaitTermination(t2, LONG_DELAY_MS);
1280      }
1281  
1282      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines