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.35 by jsr166, Sat Nov 21 02:07:27 2009 UTC vs.
Revision 1.53 by jsr166, Mon May 2 00:49: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 9 | Line 9
9   import junit.framework.*;
10   import java.util.concurrent.locks.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14   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 58 | Line 59 | public class ReentrantReadWriteLockTest
59      }
60  
61      /**
62 +     * Releases lock, checking that it had a hold count of 1.
63 +     */
64 +    void releaseLock(ReentrantReadWriteLock.WriteLock lock) {
65 +        assertTrue(lock.isHeldByCurrentThread());
66 +        lock.unlock();
67 +        assertFalse(lock.isHeldByCurrentThread());
68 +    }
69 +
70 +    /**
71       * Constructor sets given fairness, and is in unlocked state
72       */
73      public void testConstructor() {
# Line 189 | Line 199 | public class ReentrantReadWriteLockTest
199       */
200      public void testWriteLockInterruptibly_Interrupted() throws Exception {
201          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
202 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
202 >        lock.writeLock().lock();
203 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
204              public void realRun() throws InterruptedException {
205                  lock.writeLock().lockInterruptibly();
195                lock.writeLock().unlock();
196                lock.writeLock().lockInterruptibly();
197                lock.writeLock().unlock();
206              }});
207  
200        lock.writeLock().lock();
201        t.start();
208          Thread.sleep(SHORT_DELAY_MS);
209          t.interrupt();
210 <        Thread.sleep(SHORT_DELAY_MS);
211 <        lock.writeLock().unlock();
206 <        t.join();
210 >        awaitTermination(t, LONG_DELAY_MS);
211 >        releaseLock(lock.writeLock());
212      }
213  
214      /**
# Line 212 | Line 217 | public class ReentrantReadWriteLockTest
217      public void testWriteTryLock_Interrupted() throws InterruptedException {
218          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
219          lock.writeLock().lock();
220 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
220 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
221              public void realRun() throws InterruptedException {
222 <                lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
222 >                lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS);
223              }});
224  
225 <        t.start();
225 >        Thread.sleep(SHORT_DELAY_MS);
226          t.interrupt();
227 <        lock.writeLock().unlock();
228 <        t.join();
227 >        awaitTermination(t, LONG_DELAY_MS);
228 >        releaseLock(lock.writeLock());
229      }
230  
231      /**
# Line 229 | Line 234 | public class ReentrantReadWriteLockTest
234      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
235          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
236          lock.writeLock().lock();
237 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
237 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
238              public void realRun() throws InterruptedException {
239                  lock.readLock().lockInterruptibly();
240              }});
241  
237        t.start();
242          Thread.sleep(SHORT_DELAY_MS);
243          t.interrupt();
244 <        Thread.sleep(SHORT_DELAY_MS);
245 <        lock.writeLock().unlock();
242 <        t.join();
244 >        awaitTermination(t, LONG_DELAY_MS);
245 >        releaseLock(lock.writeLock());
246      }
247  
248      /**
# Line 248 | Line 251 | public class ReentrantReadWriteLockTest
251      public void testReadTryLock_Interrupted() throws InterruptedException {
252          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
253          lock.writeLock().lock();
254 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
254 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
255              public void realRun() throws InterruptedException {
256 <                lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
256 >                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
257              }});
258  
259 <        t.start();
259 >        Thread.sleep(SHORT_DELAY_MS);
260          t.interrupt();
261 <        t.join();
261 >        awaitTermination(t, LONG_DELAY_MS);
262 >        releaseLock(lock.writeLock());
263      }
264  
265  
# Line 265 | Line 269 | public class ReentrantReadWriteLockTest
269      public void testWriteTryLockWhenLocked() throws InterruptedException {
270          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
271          lock.writeLock().lock();
272 <        Thread t = new Thread(new Runnable() {
273 <                public void run() {
274 <                    threadAssertFalse(lock.writeLock().tryLock());
275 <                }
272 <            });
272 >        Thread t = newStartedThread(new CheckedRunnable() {
273 >            public void realRun() {
274 >                assertFalse(lock.writeLock().tryLock());
275 >            }});
276  
277 <        t.start();
275 <        t.join();
277 >        awaitTermination(t, LONG_DELAY_MS);
278          lock.writeLock().unlock();
279      }
280  
# Line 282 | Line 284 | public class ReentrantReadWriteLockTest
284      public void testReadTryLockWhenLocked() throws InterruptedException {
285          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
286          lock.writeLock().lock();
287 <        Thread t = new Thread(new Runnable() {
288 <                public void run() {
289 <                    threadAssertFalse(lock.readLock().tryLock());
290 <                }
289 <            });
287 >        Thread t = newStartedThread(new CheckedRunnable() {
288 >            public void realRun() {
289 >                assertFalse(lock.readLock().tryLock());
290 >            }});
291  
292 <        t.start();
292 <        t.join();
292 >        awaitTermination(t, LONG_DELAY_MS);
293          lock.writeLock().unlock();
294      }
295  
# Line 299 | Line 299 | public class ReentrantReadWriteLockTest
299      public void testMultipleReadLocks() throws InterruptedException {
300          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
301          lock.readLock().lock();
302 <        Thread t = new Thread(new Runnable() {
303 <                public void run() {
304 <                    threadAssertTrue(lock.readLock().tryLock());
305 <                    lock.readLock().unlock();
306 <                }
307 <            });
302 >        Thread t = newStartedThread(new CheckedRunnable() {
303 >            public void realRun() {
304 >                assertTrue(lock.readLock().tryLock());
305 >                lock.readLock().unlock();
306 >            }});
307  
308 <        t.start();
310 <        t.join();
308 >        awaitTermination(t, LONG_DELAY_MS);
309          lock.readLock().unlock();
310      }
311  
# Line 317 | Line 315 | public class ReentrantReadWriteLockTest
315      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
316          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
317          lock.readLock().lock();
318 <        Thread t1 = new Thread(new Runnable() {
319 <                public void run() {
320 <                    lock.readLock().lock();
321 <                    lock.readLock().unlock();
322 <                }
323 <            });
324 <        Thread t2 = new Thread(new Runnable() {
325 <                public void run() {
326 <                    lock.writeLock().lock();
327 <                    lock.writeLock().unlock();
330 <                }
331 <            });
318 >        Thread t1 = newStartedThread(new CheckedRunnable() {
319 >            public void realRun() {
320 >                lock.readLock().lock();
321 >                lock.readLock().unlock();
322 >            }});
323 >        Thread t2 = newStartedThread(new CheckedRunnable() {
324 >            public void realRun() {
325 >                lock.writeLock().lock();
326 >                lock.writeLock().unlock();
327 >            }});
328  
333        t1.start();
334        t2.start();
329          Thread.sleep(SHORT_DELAY_MS);
330          lock.readLock().unlock();
331 <        t1.join(MEDIUM_DELAY_MS);
332 <        t2.join(MEDIUM_DELAY_MS);
339 <        assertTrue(!t1.isAlive());
340 <        assertTrue(!t2.isAlive());
331 >        awaitTermination(t1, LONG_DELAY_MS);
332 >        awaitTermination(t2, LONG_DELAY_MS);
333      }
334  
335      /**
# Line 346 | Line 338 | public class ReentrantReadWriteLockTest
338      public void testReadAfterWriteLock() throws InterruptedException {
339          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
340          lock.writeLock().lock();
341 <        Thread t1 = new Thread(new Runnable() {
342 <                public void run() {
343 <                    lock.readLock().lock();
344 <                    lock.readLock().unlock();
345 <                }
346 <            });
347 <        Thread t2 = new Thread(new Runnable() {
348 <                public void run() {
349 <                    lock.readLock().lock();
350 <                    lock.readLock().unlock();
359 <                }
360 <            });
341 >        Thread t1 = newStartedThread(new CheckedRunnable() {
342 >            public void realRun() {
343 >                lock.readLock().lock();
344 >                lock.readLock().unlock();
345 >            }});
346 >        Thread t2 = newStartedThread(new CheckedRunnable() {
347 >            public void realRun() {
348 >                lock.readLock().lock();
349 >                lock.readLock().unlock();
350 >            }});
351  
362        t1.start();
363        t2.start();
352          Thread.sleep(SHORT_DELAY_MS);
353          lock.writeLock().unlock();
354 <        t1.join(MEDIUM_DELAY_MS);
355 <        t2.join(MEDIUM_DELAY_MS);
368 <        assertTrue(!t1.isAlive());
369 <        assertTrue(!t2.isAlive());
354 >        awaitTermination(t1, LONG_DELAY_MS);
355 >        awaitTermination(t2, LONG_DELAY_MS);
356      }
357  
358      /**
# Line 387 | Line 373 | public class ReentrantReadWriteLockTest
373      public void testReadHoldingWriteLock2() throws InterruptedException {
374          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
375          lock.writeLock().lock();
376 <        Thread t1 = new Thread(new Runnable() {
377 <                public void run() {
378 <                    lock.readLock().lock();
379 <                    lock.readLock().unlock();
380 <                }
381 <            });
382 <        Thread t2 = new Thread(new Runnable() {
383 <                public void run() {
384 <                    lock.readLock().lock();
385 <                    lock.readLock().unlock();
400 <                }
401 <            });
376 >        Thread t1 = newStartedThread(new CheckedRunnable() {
377 >            public void realRun() {
378 >                lock.readLock().lock();
379 >                lock.readLock().unlock();
380 >            }});
381 >        Thread t2 = newStartedThread(new CheckedRunnable() {
382 >            public void realRun() {
383 >                lock.readLock().lock();
384 >                lock.readLock().unlock();
385 >            }});
386  
403        t1.start();
404        t2.start();
387          lock.readLock().lock();
388          lock.readLock().unlock();
389          Thread.sleep(SHORT_DELAY_MS);
390          lock.readLock().lock();
391          lock.readLock().unlock();
392          lock.writeLock().unlock();
393 <        t1.join(MEDIUM_DELAY_MS);
394 <        t2.join(MEDIUM_DELAY_MS);
413 <        assertTrue(!t1.isAlive());
414 <        assertTrue(!t2.isAlive());
393 >        awaitTermination(t1, LONG_DELAY_MS);
394 >        awaitTermination(t2, LONG_DELAY_MS);
395      }
396  
397      /**
398 <     *  Read lock succeeds if write locked by current thread even if
398 >     * Read lock succeeds if write locked by current thread even if
399       * other threads are waiting for writelock
400       */
401      public void testReadHoldingWriteLock3() throws InterruptedException {
402          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
403          lock.writeLock().lock();
404 <        Thread t1 = new Thread(new Runnable() {
405 <                public void run() {
406 <                    lock.writeLock().lock();
407 <                    lock.writeLock().unlock();
408 <                }
409 <            });
410 <        Thread t2 = new Thread(new Runnable() {
411 <                public void run() {
412 <                    lock.writeLock().lock();
413 <                    lock.writeLock().unlock();
434 <                }
435 <            });
404 >        Thread t1 = newStartedThread(new CheckedRunnable() {
405 >            public void realRun() {
406 >                lock.writeLock().lock();
407 >                lock.writeLock().unlock();
408 >            }});
409 >        Thread t2 = newStartedThread(new CheckedRunnable() {
410 >            public void realRun() {
411 >                lock.writeLock().lock();
412 >                lock.writeLock().unlock();
413 >            }});
414  
437        t1.start();
438        t2.start();
415          lock.readLock().lock();
416          lock.readLock().unlock();
417          Thread.sleep(SHORT_DELAY_MS);
418          lock.readLock().lock();
419          lock.readLock().unlock();
420          lock.writeLock().unlock();
421 <        t1.join(MEDIUM_DELAY_MS);
422 <        t2.join(MEDIUM_DELAY_MS);
447 <        assertTrue(!t1.isAlive());
448 <        assertTrue(!t2.isAlive());
421 >        awaitTermination(t1, LONG_DELAY_MS);
422 >        awaitTermination(t2, LONG_DELAY_MS);
423      }
424  
425  
426      /**
427 <     *  Write lock succeeds if write locked by current thread even if
427 >     * Write lock succeeds if write locked by current thread even if
428       * other threads are waiting for writelock
429       */
430      public void testWriteHoldingWriteLock4() throws InterruptedException {
431          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
432          lock.writeLock().lock();
433 <        Thread t1 = new Thread(new Runnable() {
434 <                public void run() {
435 <                    lock.writeLock().lock();
436 <                    lock.writeLock().unlock();
437 <                }
438 <            });
439 <        Thread t2 = new Thread(new Runnable() {
440 <                public void run() {
441 <                    lock.writeLock().lock();
442 <                    lock.writeLock().unlock();
469 <                }
470 <            });
433 >        Thread t1 = newStartedThread(new CheckedRunnable() {
434 >            public void realRun() {
435 >                lock.writeLock().lock();
436 >                lock.writeLock().unlock();
437 >            }});
438 >        Thread t2 = newStartedThread(new CheckedRunnable() {
439 >            public void realRun() {
440 >                lock.writeLock().lock();
441 >                lock.writeLock().unlock();
442 >            }});
443  
472        t1.start();
473        t2.start();
444          lock.writeLock().lock();
445          lock.writeLock().unlock();
446          Thread.sleep(SHORT_DELAY_MS);
447          lock.writeLock().lock();
448          lock.writeLock().unlock();
449          lock.writeLock().unlock();
450 <        t1.join(MEDIUM_DELAY_MS);
451 <        t2.join(MEDIUM_DELAY_MS);
482 <        assertTrue(!t1.isAlive());
483 <        assertTrue(!t2.isAlive());
450 >        awaitTermination(t1, LONG_DELAY_MS);
451 >        awaitTermination(t2, LONG_DELAY_MS);
452      }
453  
454  
# Line 502 | Line 470 | public class ReentrantReadWriteLockTest
470      public void testReadHoldingWriteLockFair2() throws InterruptedException {
471          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
472          lock.writeLock().lock();
473 <        Thread t1 = new Thread(new Runnable() {
474 <                public void run() {
475 <                    lock.readLock().lock();
476 <                    lock.readLock().unlock();
477 <                }
478 <            });
479 <        Thread t2 = new Thread(new Runnable() {
480 <                public void run() {
481 <                    lock.readLock().lock();
482 <                    lock.readLock().unlock();
515 <                }
516 <            });
473 >        Thread t1 = newStartedThread(new CheckedRunnable() {
474 >            public void realRun() {
475 >                lock.readLock().lock();
476 >                lock.readLock().unlock();
477 >            }});
478 >        Thread t2 = newStartedThread(new CheckedRunnable() {
479 >            public void realRun() {
480 >                lock.readLock().lock();
481 >                lock.readLock().unlock();
482 >            }});
483  
518        t1.start();
519        t2.start();
484          lock.readLock().lock();
485          lock.readLock().unlock();
486          Thread.sleep(SHORT_DELAY_MS);
487          lock.readLock().lock();
488          lock.readLock().unlock();
489          lock.writeLock().unlock();
490 <        t1.join(MEDIUM_DELAY_MS);
491 <        t2.join(MEDIUM_DELAY_MS);
528 <        assertTrue(!t1.isAlive());
529 <        assertTrue(!t2.isAlive());
490 >        awaitTermination(t1, LONG_DELAY_MS);
491 >        awaitTermination(t2, LONG_DELAY_MS);
492      }
493  
494  
# Line 537 | Line 499 | public class ReentrantReadWriteLockTest
499      public void testReadHoldingWriteLockFair3() throws InterruptedException {
500          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
501          lock.writeLock().lock();
502 <        Thread t1 = new Thread(new Runnable() {
503 <                public void run() {
504 <                    lock.writeLock().lock();
505 <                    lock.writeLock().unlock();
506 <                }
507 <            });
508 <        Thread t2 = new Thread(new Runnable() {
509 <                public void run() {
510 <                    lock.writeLock().lock();
511 <                    lock.writeLock().unlock();
550 <                }
551 <            });
502 >        Thread t1 = newStartedThread(new CheckedRunnable() {
503 >            public void realRun() {
504 >                lock.writeLock().lock();
505 >                lock.writeLock().unlock();
506 >            }});
507 >        Thread t2 = newStartedThread(new CheckedRunnable() {
508 >            public void realRun() {
509 >                lock.writeLock().lock();
510 >                lock.writeLock().unlock();
511 >            }});
512  
553        t1.start();
554        t2.start();
513          lock.readLock().lock();
514          lock.readLock().unlock();
515          Thread.sleep(SHORT_DELAY_MS);
516          lock.readLock().lock();
517          lock.readLock().unlock();
518          lock.writeLock().unlock();
519 <        t1.join(MEDIUM_DELAY_MS);
520 <        t2.join(MEDIUM_DELAY_MS);
563 <        assertTrue(!t1.isAlive());
564 <        assertTrue(!t2.isAlive());
519 >        awaitTermination(t1, LONG_DELAY_MS);
520 >        awaitTermination(t2, LONG_DELAY_MS);
521      }
522  
523  
# Line 572 | Line 528 | public class ReentrantReadWriteLockTest
528      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
529          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
530          lock.writeLock().lock();
531 <        Thread t1 = new Thread(new Runnable() {
532 <                public void run() {
533 <                    lock.writeLock().lock();
534 <                    lock.writeLock().unlock();
535 <                }
536 <            });
537 <        Thread t2 = new Thread(new Runnable() {
538 <                public void run() {
539 <                    lock.writeLock().lock();
540 <                    lock.writeLock().unlock();
585 <                }
586 <            });
531 >        Thread t1 = newStartedThread(new CheckedRunnable() {
532 >            public void realRun() {
533 >                lock.writeLock().lock();
534 >                lock.writeLock().unlock();
535 >            }});
536 >        Thread t2 = newStartedThread(new CheckedRunnable() {
537 >            public void realRun() {
538 >                lock.writeLock().lock();
539 >                lock.writeLock().unlock();
540 >            }});
541  
588        t1.start();
589        t2.start();
542          Thread.sleep(SHORT_DELAY_MS);
543          assertTrue(lock.isWriteLockedByCurrentThread());
544 <        assertTrue(lock.getWriteHoldCount() == 1);
544 >        assertEquals(1, lock.getWriteHoldCount());
545          lock.writeLock().lock();
546 <        assertTrue(lock.getWriteHoldCount() == 2);
546 >        assertEquals(2, lock.getWriteHoldCount());
547          lock.writeLock().unlock();
548          lock.writeLock().lock();
549          lock.writeLock().unlock();
550          lock.writeLock().unlock();
551 <        t1.join(MEDIUM_DELAY_MS);
552 <        t2.join(MEDIUM_DELAY_MS);
601 <        assertTrue(!t1.isAlive());
602 <        assertTrue(!t2.isAlive());
551 >        awaitTermination(t1, LONG_DELAY_MS);
552 >        awaitTermination(t2, LONG_DELAY_MS);
553      }
554  
555  
# Line 609 | Line 559 | public class ReentrantReadWriteLockTest
559      public void testTryLockWhenReadLocked() throws InterruptedException {
560          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
561          lock.readLock().lock();
562 <        Thread t = new Thread(new Runnable() {
563 <                public void run() {
564 <                    threadAssertTrue(lock.readLock().tryLock());
565 <                    lock.readLock().unlock();
566 <                }
617 <            });
562 >        Thread t = newStartedThread(new CheckedRunnable() {
563 >            public void realRun() {
564 >                assertTrue(lock.readLock().tryLock());
565 >                lock.readLock().unlock();
566 >            }});
567  
568 <        t.start();
620 <        t.join();
568 >        awaitTermination(t, LONG_DELAY_MS);
569          lock.readLock().unlock();
570      }
571  
624
625
572      /**
573       * write tryLock fails when readlocked
574       */
575      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
576          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
577          lock.readLock().lock();
578 <        Thread t = new Thread(new Runnable() {
579 <                public void run() {
580 <                    threadAssertFalse(lock.writeLock().tryLock());
581 <                }
636 <            });
578 >        Thread t = newStartedThread(new CheckedRunnable() {
579 >            public void realRun() {
580 >                assertFalse(lock.writeLock().tryLock());
581 >            }});
582  
583 <        t.start();
639 <        t.join();
583 >        awaitTermination(t, LONG_DELAY_MS);
584          lock.readLock().unlock();
585      }
586  
# Line 647 | Line 591 | public class ReentrantReadWriteLockTest
591      public void testTryLockWhenReadLockedFair() throws InterruptedException {
592          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
593          lock.readLock().lock();
594 <        Thread t = new Thread(new Runnable() {
595 <                public void run() {
596 <                    threadAssertTrue(lock.readLock().tryLock());
597 <                    lock.readLock().unlock();
598 <                }
655 <            });
594 >        Thread t = newStartedThread(new CheckedRunnable() {
595 >            public void realRun() {
596 >                assertTrue(lock.readLock().tryLock());
597 >                lock.readLock().unlock();
598 >            }});
599  
600 <        t.start();
658 <        t.join();
600 >        awaitTermination(t, LONG_DELAY_MS);
601          lock.readLock().unlock();
602      }
603  
# Line 667 | Line 609 | public class ReentrantReadWriteLockTest
609      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
610          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
611          lock.readLock().lock();
612 <        Thread t = new Thread(new Runnable() {
613 <                public void run() {
614 <                    threadAssertFalse(lock.writeLock().tryLock());
615 <                }
674 <            });
612 >        Thread t = newStartedThread(new CheckedRunnable() {
613 >            public void realRun() {
614 >                assertFalse(lock.writeLock().tryLock());
615 >            }});
616  
617 <        t.start();
677 <        t.join();
617 >        awaitTermination(t, LONG_DELAY_MS);
618          lock.readLock().unlock();
619      }
620  
# Line 686 | Line 626 | public class ReentrantReadWriteLockTest
626      public void testWriteTryLock_Timeout() throws InterruptedException {
627          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
628          lock.writeLock().lock();
629 <        Thread t = new Thread(new CheckedRunnable() {
629 >        Thread t = newStartedThread(new CheckedRunnable() {
630              public void realRun() throws InterruptedException {
631 <                threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
631 >                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
632              }});
633  
634 <        t.start();
695 <        t.join();
634 >        awaitTermination(t, LONG_DELAY_MS);
635          assertTrue(lock.writeLock().isHeldByCurrentThread());
636          lock.writeLock().unlock();
637      }
# Line 703 | Line 642 | public class ReentrantReadWriteLockTest
642      public void testReadTryLock_Timeout() throws InterruptedException {
643          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
644          lock.writeLock().lock();
645 <        Thread t = new Thread(new CheckedRunnable() {
645 >        Thread t = newStartedThread(new CheckedRunnable() {
646              public void realRun() throws InterruptedException {
647 <                threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
647 >                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
648              }});
649  
650 <        t.start();
712 <        t.join();
650 >        awaitTermination(t, LONG_DELAY_MS);
651          assertTrue(lock.writeLock().isHeldByCurrentThread());
652          lock.writeLock().unlock();
653      }
# Line 721 | Line 659 | public class ReentrantReadWriteLockTest
659      public void testWriteLockInterruptibly() throws InterruptedException {
660          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
661          lock.writeLock().lockInterruptibly();
662 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
662 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
663              public void realRun() throws InterruptedException {
664                  lock.writeLock().lockInterruptibly();
665              }});
666  
729        t.start();
667          Thread.sleep(SHORT_DELAY_MS);
668          t.interrupt();
669 <        Thread.sleep(SHORT_DELAY_MS);
670 <        t.join();
734 <        lock.writeLock().unlock();
669 >        awaitTermination(t, LONG_DELAY_MS);
670 >        releaseLock(lock.writeLock());
671      }
672  
673      /**
674 <     *  read lockInterruptibly succeeds if lock free else is interruptible
674 >     * read lockInterruptibly succeeds if lock free else is interruptible
675       */
676      public void testReadLockInterruptibly() throws InterruptedException {
677          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
678          lock.writeLock().lockInterruptibly();
679 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
679 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
680              public void realRun() throws InterruptedException {
681                  lock.readLock().lockInterruptibly();
682              }});
683  
748        t.start();
684          Thread.sleep(SHORT_DELAY_MS);
685          t.interrupt();
686 <        t.join();
687 <        lock.writeLock().unlock();
686 >        awaitTermination(t, LONG_DELAY_MS);
687 >        releaseLock(lock.writeLock());
688      }
689  
690      /**
# Line 791 | Line 726 | public class ReentrantReadWriteLockTest
726  
727  
728      /**
729 <     *  timed await without a signal times out
729 >     * timed await without a signal times out
730       */
731      public void testAwait_Timeout() throws InterruptedException {
732          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
733          final Condition c = lock.writeLock().newCondition();
734          lock.writeLock().lock();
735 <        assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
735 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
736          lock.writeLock().unlock();
737      }
738  
# Line 819 | Line 754 | public class ReentrantReadWriteLockTest
754      public void testAwait() throws InterruptedException {
755          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
756          final Condition c = lock.writeLock().newCondition();
757 <        Thread t = new Thread(new CheckedRunnable() {
757 >        Thread t = newStartedThread(new CheckedRunnable() {
758              public void realRun() throws InterruptedException {
759                  lock.writeLock().lock();
760                  c.await();
761                  lock.writeLock().unlock();
762              }});
763  
829        t.start();
764          Thread.sleep(SHORT_DELAY_MS);
765          lock.writeLock().lock();
766          c.signal();
767          lock.writeLock().unlock();
768 <        t.join(SHORT_DELAY_MS);
835 <        assertFalse(t.isAlive());
768 >        awaitTermination(t, LONG_DELAY_MS);
769      }
770  
771      /** A helper class for uninterruptible wait tests */
# Line 885 | Line 818 | public class ReentrantReadWriteLockTest
818              lock.writeLock().unlock();
819          }
820  
821 <        thread.join();
821 >        awaitTermination(thread, LONG_DELAY_MS);
822          assertTrue(thread.interrupted);
890        assertFalse(thread.isAlive());
823      }
824  
825      /**
# Line 896 | Line 828 | public class ReentrantReadWriteLockTest
828      public void testAwait_Interrupt() throws InterruptedException {
829          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
830          final Condition c = lock.writeLock().newCondition();
831 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
831 >        final CountDownLatch locked = new CountDownLatch(1);
832 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
833              public void realRun() throws InterruptedException {
834                  lock.writeLock().lock();
835 <                c.await();
836 <                lock.writeLock().unlock();
835 >                assertTrue(lock.isWriteLocked());
836 >                locked.countDown();
837 >                try { c.await(); }
838 >                finally { lock.writeLock().unlock(); }
839              }});
840  
841 <        t.start();
842 <        Thread.sleep(SHORT_DELAY_MS);
841 >        locked.await();
842 >        while (lock.isWriteLocked())
843 >            Thread.yield();
844          t.interrupt();
845 <        t.join(SHORT_DELAY_MS);
846 <        assertFalse(t.isAlive());
845 >        awaitTermination(t, LONG_DELAY_MS);
846 >        assertFalse(lock.isWriteLocked());
847      }
848  
849      /**
# Line 916 | Line 852 | public class ReentrantReadWriteLockTest
852      public void testAwaitNanos_Interrupt() throws InterruptedException {
853          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
854          final Condition c = lock.writeLock().newCondition();
855 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
855 >        final CountDownLatch locked = new CountDownLatch(1);
856 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
857              public void realRun() throws InterruptedException {
858                  lock.writeLock().lock();
859 <                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
860 <                lock.writeLock().unlock();
859 >                assertTrue(lock.isWriteLocked());
860 >                locked.countDown();
861 >                try { c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); }
862 >                finally { lock.writeLock().unlock(); }
863              }});
864  
865 <        t.start();
866 <        Thread.sleep(SHORT_DELAY_MS);
865 >        locked.await();
866 >        while (lock.isWriteLocked())
867 >            Thread.yield();
868          t.interrupt();
869 <        t.join(SHORT_DELAY_MS);
870 <        assertFalse(t.isAlive());
869 >        awaitTermination(t, LONG_DELAY_MS);
870 >        assertFalse(lock.isWriteLocked());
871      }
872  
873      /**
# Line 936 | Line 876 | public class ReentrantReadWriteLockTest
876      public void testAwaitUntil_Interrupt() throws InterruptedException {
877          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
878          final Condition c = lock.writeLock().newCondition();
879 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
879 >        final CountDownLatch locked = new CountDownLatch(1);
880 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
881              public void realRun() throws InterruptedException {
882                  lock.writeLock().lock();
883 +                assertTrue(lock.isWriteLocked());
884 +                locked.countDown();
885                  java.util.Date d = new java.util.Date();
886 <                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
887 <                lock.writeLock().unlock();
886 >                try { c.awaitUntil(new java.util.Date(d.getTime() + 10000)); }
887 >                finally { lock.writeLock().unlock(); }
888              }});
889  
890 <        t.start();
891 <        Thread.sleep(SHORT_DELAY_MS);
890 >        locked.await();
891 >        while (lock.isWriteLocked())
892 >            Thread.yield();
893          t.interrupt();
894 <        t.join(SHORT_DELAY_MS);
895 <        assertFalse(t.isAlive());
894 >        awaitTermination(t, LONG_DELAY_MS);
895 >        assertFalse(lock.isWriteLocked());
896      }
897  
898      /**
# Line 957 | Line 901 | public class ReentrantReadWriteLockTest
901      public void testSignalAll() throws InterruptedException {
902          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
903          final Condition c = lock.writeLock().newCondition();
904 <        Thread t1 = new Thread(new CheckedRunnable() {
904 >        Thread t1 = newStartedThread(new CheckedRunnable() {
905              public void realRun() throws InterruptedException {
906                  lock.writeLock().lock();
907                  c.await();
908                  lock.writeLock().unlock();
909              }});
910  
911 <        Thread t2 = new Thread(new CheckedRunnable() {
911 >        Thread t2 = newStartedThread(new CheckedRunnable() {
912              public void realRun() throws InterruptedException {
913                  lock.writeLock().lock();
914                  c.await();
915                  lock.writeLock().unlock();
916              }});
917  
974        t1.start();
975        t2.start();
918          Thread.sleep(SHORT_DELAY_MS);
919          lock.writeLock().lock();
920          c.signalAll();
921          lock.writeLock().unlock();
922 <        t1.join(SHORT_DELAY_MS);
923 <        t2.join(SHORT_DELAY_MS);
982 <        assertFalse(t1.isAlive());
983 <        assertFalse(t2.isAlive());
922 >        awaitTermination(t1, LONG_DELAY_MS);
923 >        awaitTermination(t2, LONG_DELAY_MS);
924      }
925  
926      /**
# Line 1024 | Line 964 | public class ReentrantReadWriteLockTest
964          lock.writeLock().unlock();
965          Thread.sleep(SHORT_DELAY_MS);
966          assertFalse(lock.hasQueuedThreads());
967 <        t1.join();
968 <        t2.join();
967 >        awaitTermination(t1, LONG_DELAY_MS);
968 >        awaitTermination(t2, LONG_DELAY_MS);
969      }
970  
971      /**
# Line 1065 | Line 1005 | public class ReentrantReadWriteLockTest
1005          assertFalse(sync.hasQueuedThread(t1));
1006          Thread.sleep(SHORT_DELAY_MS);
1007          assertFalse(sync.hasQueuedThread(t2));
1008 <        t1.join();
1009 <        t2.join();
1008 >        awaitTermination(t1, LONG_DELAY_MS);
1009 >        awaitTermination(t2, LONG_DELAY_MS);
1010      }
1011  
1012  
# Line 1091 | Line 1031 | public class ReentrantReadWriteLockTest
1031          lock.writeLock().unlock();
1032          Thread.sleep(SHORT_DELAY_MS);
1033          assertEquals(0, lock.getQueueLength());
1034 <        t1.join();
1035 <        t2.join();
1034 >        awaitTermination(t1, LONG_DELAY_MS);
1035 >        awaitTermination(t2, LONG_DELAY_MS);
1036      }
1037  
1038      /**
# Line 1119 | Line 1059 | public class ReentrantReadWriteLockTest
1059          lock.writeLock().unlock();
1060          Thread.sleep(SHORT_DELAY_MS);
1061          assertTrue(lock.getQueuedThreads().isEmpty());
1062 <        t1.join();
1063 <        t2.join();
1062 >        awaitTermination(t1, LONG_DELAY_MS);
1063 >        awaitTermination(t2, LONG_DELAY_MS);
1064      }
1065  
1066      /**
# Line 1241 | Line 1181 | public class ReentrantReadWriteLockTest
1181      public void testHasWaiters() throws InterruptedException {
1182          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1183          final Condition c = lock.writeLock().newCondition();
1184 <        Thread t = new Thread(new CheckedRunnable() {
1184 >        Thread t = newStartedThread(new CheckedRunnable() {
1185              public void realRun() throws InterruptedException {
1186                  lock.writeLock().lock();
1187 <                threadAssertFalse(lock.hasWaiters(c));
1188 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1187 >                assertFalse(lock.hasWaiters(c));
1188 >                assertEquals(0, lock.getWaitQueueLength(c));
1189                  c.await();
1190                  lock.writeLock().unlock();
1191              }});
1192  
1253        t.start();
1193          Thread.sleep(SHORT_DELAY_MS);
1194          lock.writeLock().lock();
1195          assertTrue(lock.hasWaiters(c));
# Line 1262 | Line 1201 | public class ReentrantReadWriteLockTest
1201          assertFalse(lock.hasWaiters(c));
1202          assertEquals(0, lock.getWaitQueueLength(c));
1203          lock.writeLock().unlock();
1204 <        t.join(SHORT_DELAY_MS);
1266 <        assertFalse(t.isAlive());
1204 >        awaitTermination(t, LONG_DELAY_MS);
1205      }
1206  
1207      /**
# Line 1272 | Line 1210 | public class ReentrantReadWriteLockTest
1210      public void testGetWaitQueueLength() throws InterruptedException {
1211          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1212          final Condition c = lock.writeLock().newCondition();
1213 <        Thread t = new Thread(new CheckedRunnable() {
1213 >        Thread t = newStartedThread(new CheckedRunnable() {
1214              public void realRun() throws InterruptedException {
1215                  lock.writeLock().lock();
1216 <                threadAssertFalse(lock.hasWaiters(c));
1217 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1216 >                assertFalse(lock.hasWaiters(c));
1217 >                assertEquals(0, lock.getWaitQueueLength(c));
1218                  c.await();
1219                  lock.writeLock().unlock();
1220              }});
1221  
1284        t.start();
1222          Thread.sleep(SHORT_DELAY_MS);
1223          lock.writeLock().lock();
1224          assertTrue(lock.hasWaiters(c));
# Line 1293 | Line 1230 | public class ReentrantReadWriteLockTest
1230          assertFalse(lock.hasWaiters(c));
1231          assertEquals(0, lock.getWaitQueueLength(c));
1232          lock.writeLock().unlock();
1233 <        t.join(SHORT_DELAY_MS);
1297 <        assertFalse(t.isAlive());
1233 >        awaitTermination(t, LONG_DELAY_MS);
1234      }
1235  
1236  
# Line 1307 | Line 1243 | public class ReentrantReadWriteLockTest
1243          Thread t1 = new Thread(new CheckedRunnable() {
1244              public void realRun() throws InterruptedException {
1245                  lock.writeLock().lock();
1246 <                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1246 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
1247                  c.await();
1248                  lock.writeLock().unlock();
1249              }});
# Line 1315 | Line 1251 | public class ReentrantReadWriteLockTest
1251          Thread t2 = new Thread(new CheckedRunnable() {
1252              public void realRun() throws InterruptedException {
1253                  lock.writeLock().lock();
1254 <                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1254 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
1255                  c.await();
1256                  lock.writeLock().unlock();
1257              }});
# Line 1338 | Line 1274 | public class ReentrantReadWriteLockTest
1274          assertFalse(lock.hasWaiters(c));
1275          assertTrue(lock.getWaitingThreads(c).isEmpty());
1276          lock.writeLock().unlock();
1277 <        t1.join(SHORT_DELAY_MS);
1278 <        t2.join(SHORT_DELAY_MS);
1343 <        assertFalse(t1.isAlive());
1344 <        assertFalse(t2.isAlive());
1277 >        awaitTermination(t1, LONG_DELAY_MS);
1278 >        awaitTermination(t2, LONG_DELAY_MS);
1279      }
1280  
1281      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines