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.42 by jsr166, Thu Sep 16 00:52:49 2010 UTC

# 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 215 | Line 215 | public class ReentrantReadWriteLockTest
215          lock.writeLock().lock();
216          Thread t = new Thread(new CheckedInterruptedRunnable() {
217              public void realRun() throws InterruptedException {
218 <                lock.writeLock().tryLock(1000,MILLISECONDS);
218 >                lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS);
219              }});
220  
221          t.start();
222 +        Thread.sleep(SHORT_DELAY_MS);
223          t.interrupt();
224          lock.writeLock().unlock();
225          t.join();
# Line 251 | Line 252 | public class ReentrantReadWriteLockTest
252          lock.writeLock().lock();
253          Thread t = new Thread(new CheckedInterruptedRunnable() {
254              public void realRun() throws InterruptedException {
255 <                lock.readLock().tryLock(1000,MILLISECONDS);
255 >                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
256              }});
257  
258          t.start();
259 +        Thread.sleep(SHORT_DELAY_MS);
260          t.interrupt();
261          t.join();
262      }
# Line 266 | Line 268 | public class ReentrantReadWriteLockTest
268      public void testWriteTryLockWhenLocked() throws InterruptedException {
269          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
270          lock.writeLock().lock();
271 <        Thread t = new Thread(new Runnable() {
272 <                public void run() {
273 <                    threadAssertFalse(lock.writeLock().tryLock());
274 <                }
273 <            });
271 >        Thread t = new Thread(new CheckedRunnable() {
272 >            public void realRun() {
273 >                assertFalse(lock.writeLock().tryLock());
274 >            }});
275  
276          t.start();
277          t.join();
# Line 283 | 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 <                }
290 <            });
287 >        Thread t = new Thread(new CheckedRunnable() {
288 >            public void realRun() {
289 >                assertFalse(lock.readLock().tryLock());
290 >            }});
291  
292          t.start();
293          t.join();
# 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 = new Thread(new CheckedRunnable() {
304 >            public void realRun() {
305 >                assertTrue(lock.readLock().tryLock());
306 >                lock.readLock().unlock();
307 >            }});
308  
309          t.start();
310          t.join();
# Line 318 | Line 317 | public class ReentrantReadWriteLockTest
317      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
318          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
319          lock.readLock().lock();
320 <        Thread t1 = new Thread(new Runnable() {
321 <                public void run() {
322 <                    lock.readLock().lock();
323 <                    lock.readLock().unlock();
324 <                }
325 <            });
326 <        Thread t2 = new Thread(new Runnable() {
327 <                public void run() {
328 <                    lock.writeLock().lock();
329 <                    lock.writeLock().unlock();
331 <                }
332 <            });
320 >        Thread t1 = new Thread(new CheckedRunnable() {
321 >            public void realRun() {
322 >                lock.readLock().lock();
323 >                lock.readLock().unlock();
324 >            }});
325 >        Thread t2 = new Thread(new CheckedRunnable() {
326 >            public void realRun() {
327 >                lock.writeLock().lock();
328 >                lock.writeLock().unlock();
329 >            }});
330  
331          t1.start();
332          t2.start();
# Line 347 | Line 344 | public class ReentrantReadWriteLockTest
344      public void testReadAfterWriteLock() throws InterruptedException {
345          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
346          lock.writeLock().lock();
347 <        Thread t1 = new Thread(new Runnable() {
348 <                public void run() {
349 <                    lock.readLock().lock();
350 <                    lock.readLock().unlock();
351 <                }
352 <            });
353 <        Thread t2 = new Thread(new Runnable() {
354 <                public void run() {
355 <                    lock.readLock().lock();
356 <                    lock.readLock().unlock();
360 <                }
361 <            });
347 >        Thread t1 = new Thread(new CheckedRunnable() {
348 >            public void realRun() {
349 >                lock.readLock().lock();
350 >                lock.readLock().unlock();
351 >            }});
352 >        Thread t2 = new Thread(new CheckedRunnable() {
353 >            public void realRun() {
354 >                lock.readLock().lock();
355 >                lock.readLock().unlock();
356 >            }});
357  
358          t1.start();
359          t2.start();
# Line 388 | Line 383 | public class ReentrantReadWriteLockTest
383      public void testReadHoldingWriteLock2() throws InterruptedException {
384          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
385          lock.writeLock().lock();
386 <        Thread t1 = new Thread(new Runnable() {
387 <                public void run() {
388 <                    lock.readLock().lock();
389 <                    lock.readLock().unlock();
390 <                }
391 <            });
392 <        Thread t2 = new Thread(new Runnable() {
393 <                public void run() {
394 <                    lock.readLock().lock();
395 <                    lock.readLock().unlock();
401 <                }
402 <            });
386 >        Thread t1 = new Thread(new CheckedRunnable() {
387 >            public void realRun() {
388 >                lock.readLock().lock();
389 >                lock.readLock().unlock();
390 >            }});
391 >        Thread t2 = new Thread(new CheckedRunnable() {
392 >            public void realRun() {
393 >                lock.readLock().lock();
394 >                lock.readLock().unlock();
395 >            }});
396  
397          t1.start();
398          t2.start();
# Line 422 | Line 415 | public class ReentrantReadWriteLockTest
415      public void testReadHoldingWriteLock3() throws InterruptedException {
416          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
417          lock.writeLock().lock();
418 <        Thread t1 = new Thread(new Runnable() {
419 <                public void run() {
420 <                    lock.writeLock().lock();
421 <                    lock.writeLock().unlock();
422 <                }
423 <            });
424 <        Thread t2 = new Thread(new Runnable() {
425 <                public void run() {
426 <                    lock.writeLock().lock();
427 <                    lock.writeLock().unlock();
435 <                }
436 <            });
418 >        Thread t1 = new Thread(new CheckedRunnable() {
419 >            public void realRun() {
420 >                lock.writeLock().lock();
421 >                lock.writeLock().unlock();
422 >            }});
423 >        Thread t2 = new Thread(new CheckedRunnable() {
424 >            public void realRun() {
425 >                lock.writeLock().lock();
426 >                lock.writeLock().unlock();
427 >            }});
428  
429          t1.start();
430          t2.start();
# Line 457 | Line 448 | public class ReentrantReadWriteLockTest
448      public void testWriteHoldingWriteLock4() throws InterruptedException {
449          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
450          lock.writeLock().lock();
451 <        Thread t1 = new Thread(new Runnable() {
452 <                public void run() {
453 <                    lock.writeLock().lock();
454 <                    lock.writeLock().unlock();
455 <                }
456 <            });
457 <        Thread t2 = new Thread(new Runnable() {
458 <                public void run() {
459 <                    lock.writeLock().lock();
460 <                    lock.writeLock().unlock();
470 <                }
471 <            });
451 >        Thread t1 = new Thread(new CheckedRunnable() {
452 >            public void realRun() {
453 >                lock.writeLock().lock();
454 >                lock.writeLock().unlock();
455 >            }});
456 >        Thread t2 = new Thread(new CheckedRunnable() {
457 >            public void realRun() {
458 >                lock.writeLock().lock();
459 >                lock.writeLock().unlock();
460 >            }});
461  
462          t1.start();
463          t2.start();
# Line 503 | Line 492 | public class ReentrantReadWriteLockTest
492      public void testReadHoldingWriteLockFair2() throws InterruptedException {
493          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
494          lock.writeLock().lock();
495 <        Thread t1 = new Thread(new Runnable() {
496 <                public void run() {
497 <                    lock.readLock().lock();
498 <                    lock.readLock().unlock();
499 <                }
500 <            });
501 <        Thread t2 = new Thread(new Runnable() {
502 <                public void run() {
503 <                    lock.readLock().lock();
504 <                    lock.readLock().unlock();
516 <                }
517 <            });
495 >        Thread t1 = new Thread(new CheckedRunnable() {
496 >            public void realRun() {
497 >                lock.readLock().lock();
498 >                lock.readLock().unlock();
499 >            }});
500 >        Thread t2 = new Thread(new CheckedRunnable() {
501 >            public void realRun() {
502 >                lock.readLock().lock();
503 >                lock.readLock().unlock();
504 >            }});
505  
506          t1.start();
507          t2.start();
# Line 538 | Line 525 | public class ReentrantReadWriteLockTest
525      public void testReadHoldingWriteLockFair3() throws InterruptedException {
526          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
527          lock.writeLock().lock();
528 <        Thread t1 = new Thread(new Runnable() {
529 <                public void run() {
530 <                    lock.writeLock().lock();
531 <                    lock.writeLock().unlock();
532 <                }
533 <            });
534 <        Thread t2 = new Thread(new Runnable() {
535 <                public void run() {
536 <                    lock.writeLock().lock();
537 <                    lock.writeLock().unlock();
551 <                }
552 <            });
528 >        Thread t1 = new Thread(new CheckedRunnable() {
529 >            public void realRun() {
530 >                lock.writeLock().lock();
531 >                lock.writeLock().unlock();
532 >            }});
533 >        Thread t2 = new Thread(new CheckedRunnable() {
534 >            public void realRun() {
535 >                lock.writeLock().lock();
536 >                lock.writeLock().unlock();
537 >            }});
538  
539          t1.start();
540          t2.start();
# Line 573 | Line 558 | public class ReentrantReadWriteLockTest
558      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
559          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
560          lock.writeLock().lock();
561 <        Thread t1 = new Thread(new Runnable() {
562 <                public void run() {
563 <                    lock.writeLock().lock();
564 <                    lock.writeLock().unlock();
565 <                }
566 <            });
567 <        Thread t2 = new Thread(new Runnable() {
568 <                public void run() {
569 <                    lock.writeLock().lock();
570 <                    lock.writeLock().unlock();
586 <                }
587 <            });
561 >        Thread t1 = new Thread(new CheckedRunnable() {
562 >            public void realRun() {
563 >                lock.writeLock().lock();
564 >                lock.writeLock().unlock();
565 >            }});
566 >        Thread t2 = new Thread(new CheckedRunnable() {
567 >            public void realRun() {
568 >                lock.writeLock().lock();
569 >                lock.writeLock().unlock();
570 >            }});
571  
572          t1.start();
573          t2.start();
574          Thread.sleep(SHORT_DELAY_MS);
575          assertTrue(lock.isWriteLockedByCurrentThread());
576 <        assertTrue(lock.getWriteHoldCount() == 1);
576 >        assertEquals(1, lock.getWriteHoldCount());
577          lock.writeLock().lock();
578 <        assertTrue(lock.getWriteHoldCount() == 2);
578 >        assertEquals(2, lock.getWriteHoldCount());
579          lock.writeLock().unlock();
580          lock.writeLock().lock();
581          lock.writeLock().unlock();
# Line 610 | Line 593 | public class ReentrantReadWriteLockTest
593      public void testTryLockWhenReadLocked() throws InterruptedException {
594          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
595          lock.readLock().lock();
596 <        Thread t = new Thread(new Runnable() {
597 <                public void run() {
598 <                    threadAssertTrue(lock.readLock().tryLock());
599 <                    lock.readLock().unlock();
600 <                }
618 <            });
596 >        Thread t = new Thread(new CheckedRunnable() {
597 >            public void realRun() {
598 >                assertTrue(lock.readLock().tryLock());
599 >                lock.readLock().unlock();
600 >            }});
601  
602          t.start();
603          t.join();
# Line 630 | Line 612 | public class ReentrantReadWriteLockTest
612      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
613          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
614          lock.readLock().lock();
615 <        Thread t = new Thread(new Runnable() {
616 <                public void run() {
617 <                    threadAssertFalse(lock.writeLock().tryLock());
618 <                }
637 <            });
615 >        Thread t = new Thread(new CheckedRunnable() {
616 >            public void realRun() {
617 >                assertFalse(lock.writeLock().tryLock());
618 >            }});
619  
620          t.start();
621          t.join();
# Line 648 | Line 629 | public class ReentrantReadWriteLockTest
629      public void testTryLockWhenReadLockedFair() throws InterruptedException {
630          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
631          lock.readLock().lock();
632 <        Thread t = new Thread(new Runnable() {
633 <                public void run() {
634 <                    threadAssertTrue(lock.readLock().tryLock());
635 <                    lock.readLock().unlock();
636 <                }
656 <            });
632 >        Thread t = new Thread(new CheckedRunnable() {
633 >            public void realRun() {
634 >                assertTrue(lock.readLock().tryLock());
635 >                lock.readLock().unlock();
636 >            }});
637  
638          t.start();
639          t.join();
# Line 668 | Line 648 | public class ReentrantReadWriteLockTest
648      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
649          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
650          lock.readLock().lock();
651 <        Thread t = new Thread(new Runnable() {
652 <                public void run() {
653 <                    threadAssertFalse(lock.writeLock().tryLock());
654 <                }
675 <            });
651 >        Thread t = new Thread(new CheckedRunnable() {
652 >            public void realRun() {
653 >                assertFalse(lock.writeLock().tryLock());
654 >            }});
655  
656          t.start();
657          t.join();
# Line 689 | Line 668 | public class ReentrantReadWriteLockTest
668          lock.writeLock().lock();
669          Thread t = new Thread(new CheckedRunnable() {
670              public void realRun() throws InterruptedException {
671 <                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
671 >                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
672              }});
673  
674          t.start();
# Line 706 | Line 685 | public class ReentrantReadWriteLockTest
685          lock.writeLock().lock();
686          Thread t = new Thread(new CheckedRunnable() {
687              public void realRun() throws InterruptedException {
688 <                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
688 >                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
689              }});
690  
691          t.start();
# Line 920 | Line 899 | public class ReentrantReadWriteLockTest
899          Thread t = new Thread(new CheckedInterruptedRunnable() {
900              public void realRun() throws InterruptedException {
901                  lock.writeLock().lock();
902 <                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
902 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
903                  lock.writeLock().unlock();
904              }});
905  
# Line 1245 | Line 1224 | public class ReentrantReadWriteLockTest
1224          Thread t = new Thread(new CheckedRunnable() {
1225              public void realRun() throws InterruptedException {
1226                  lock.writeLock().lock();
1227 <                threadAssertFalse(lock.hasWaiters(c));
1228 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1227 >                assertFalse(lock.hasWaiters(c));
1228 >                assertEquals(0, lock.getWaitQueueLength(c));
1229                  c.await();
1230                  lock.writeLock().unlock();
1231              }});
# Line 1276 | Line 1255 | public class ReentrantReadWriteLockTest
1255          Thread t = new Thread(new CheckedRunnable() {
1256              public void realRun() throws InterruptedException {
1257                  lock.writeLock().lock();
1258 <                threadAssertFalse(lock.hasWaiters(c));
1259 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1258 >                assertFalse(lock.hasWaiters(c));
1259 >                assertEquals(0, lock.getWaitQueueLength(c));
1260                  c.await();
1261                  lock.writeLock().unlock();
1262              }});
# Line 1308 | Line 1287 | public class ReentrantReadWriteLockTest
1287          Thread t1 = new Thread(new CheckedRunnable() {
1288              public void realRun() throws InterruptedException {
1289                  lock.writeLock().lock();
1290 <                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1290 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
1291                  c.await();
1292                  lock.writeLock().unlock();
1293              }});
# Line 1316 | Line 1295 | public class ReentrantReadWriteLockTest
1295          Thread t2 = new Thread(new CheckedRunnable() {
1296              public void realRun() throws InterruptedException {
1297                  lock.writeLock().lock();
1298 <                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1298 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
1299                  c.await();
1300                  lock.writeLock().unlock();
1301              }});

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines