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

Comparing jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java (file contents):
Revision 1.9 by jsr166, Wed Nov 18 08:22:57 2009 UTC vs.
Revision 1.16 by jsr166, Wed Aug 25 00:07:02 2010 UTC

# Line 10 | Line 10
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.util.concurrent.locks.*;
15   import java.io.*;
16  
17   public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22          return new TestSuite(AbstractQueuedLongSynchronizerTest.class);
# Line 68 | Line 69 | public class AbstractQueuedLongSynchroni
69      }
70  
71      /**
72 <     * A runnable calling acquireInterruptibly
72 >     * A runnable calling acquireInterruptibly that does not expect to
73 >     * be interrupted.
74       */
75 <    class InterruptibleSyncRunnable implements Runnable {
75 >    class InterruptibleSyncRunnable extends CheckedRunnable {
76          final Mutex sync;
77          InterruptibleSyncRunnable(Mutex l) { sync = l; }
78 <        public void run() {
79 <            try {
78 <                sync.acquireInterruptibly(1);
79 <            } catch (InterruptedException success) {}
78 >        public void realRun() throws InterruptedException {
79 >            sync.acquireInterruptibly(1);
80          }
81      }
82  
83  
84      /**
85       * A runnable calling acquireInterruptibly that expects to be
86 <     * interrupted
86 >     * interrupted.
87       */
88 <    class InterruptedSyncRunnable implements Runnable {
88 >    class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
89          final Mutex sync;
90          InterruptedSyncRunnable(Mutex l) { sync = l; }
91 <        public void run() {
92 <            try {
93 <                sync.acquireInterruptibly(1);
94 <                threadShouldThrow();
95 <            } catch (InterruptedException success) {}
91 >        public void realRun() throws InterruptedException {
92 >            sync.acquireInterruptibly(1);
93          }
94      }
95  
# Line 100 | Line 97 | public class AbstractQueuedLongSynchroni
97       * isHeldExclusively is false upon construction
98       */
99      public void testIsHeldExclusively() {
100 <        Mutex rl = new Mutex();
100 >        Mutex rl = new Mutex();
101          assertFalse(rl.isHeldExclusively());
102      }
103  
# Line 108 | Line 105 | public class AbstractQueuedLongSynchroni
105       * acquiring released sync succeeds
106       */
107      public void testAcquire() {
108 <        Mutex rl = new Mutex();
108 >        Mutex rl = new Mutex();
109          rl.acquire(1);
110          assertTrue(rl.isHeldExclusively());
111          rl.release(1);
# Line 119 | Line 116 | public class AbstractQueuedLongSynchroni
116       * tryAcquire on an released sync succeeds
117       */
118      public void testTryAcquire() {
119 <        Mutex rl = new Mutex();
119 >        Mutex rl = new Mutex();
120          assertTrue(rl.tryAcquire(1));
121          assertTrue(rl.isHeldExclusively());
122          rl.release(1);
# Line 129 | Line 126 | public class AbstractQueuedLongSynchroni
126       * hasQueuedThreads reports whether there are waiting threads
127       */
128      public void testhasQueuedThreads() throws InterruptedException {
129 <        final Mutex sync = new Mutex();
129 >        final Mutex sync = new Mutex();
130          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
131          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
132          assertFalse(sync.hasQueuedThreads());
# Line 154 | Line 151 | public class AbstractQueuedLongSynchroni
151       * isQueued(null) throws NPE
152       */
153      public void testIsQueuedNPE() {
154 <        final Mutex sync = new Mutex();
154 >        final Mutex sync = new Mutex();
155          try {
156              sync.isQueued(null);
157              shouldThrow();
# Line 165 | Line 162 | public class AbstractQueuedLongSynchroni
162       * isQueued reports whether a thread is queued.
163       */
164      public void testIsQueued() throws InterruptedException {
165 <        final Mutex sync = new Mutex();
165 >        final Mutex sync = new Mutex();
166          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
167          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
168          assertFalse(sync.isQueued(t1));
# Line 195 | Line 192 | public class AbstractQueuedLongSynchroni
192       * getFirstQueuedThread returns first waiting thread or null if none
193       */
194      public void testGetFirstQueuedThread() throws InterruptedException {
195 <        final Mutex sync = new Mutex();
195 >        final Mutex sync = new Mutex();
196          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
197          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
198          assertNull(sync.getFirstQueuedThread());
# Line 222 | Line 219 | public class AbstractQueuedLongSynchroni
219       * hasContended reports false if no thread has ever blocked, else true
220       */
221      public void testHasContended() throws InterruptedException {
222 <        final Mutex sync = new Mutex();
222 >        final Mutex sync = new Mutex();
223          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
224          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
225          assertFalse(sync.hasContended());
# Line 247 | Line 244 | public class AbstractQueuedLongSynchroni
244       * getQueuedThreads includes waiting threads
245       */
246      public void testGetQueuedThreads() throws InterruptedException {
247 <        final Mutex sync = new Mutex();
247 >        final Mutex sync = new Mutex();
248          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
249          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
250          assertTrue(sync.getQueuedThreads().isEmpty());
# Line 275 | Line 272 | public class AbstractQueuedLongSynchroni
272       * getExclusiveQueuedThreads includes waiting threads
273       */
274      public void testGetExclusiveQueuedThreads() throws InterruptedException {
275 <        final Mutex sync = new Mutex();
275 >        final Mutex sync = new Mutex();
276          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
277          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
278          assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
# Line 303 | Line 300 | public class AbstractQueuedLongSynchroni
300       * getSharedQueuedThreads does not include exclusively waiting threads
301       */
302      public void testGetSharedQueuedThreads() throws InterruptedException {
303 <        final Mutex sync = new Mutex();
303 >        final Mutex sync = new Mutex();
304          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
305          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
306          assertTrue(sync.getSharedQueuedThreads().isEmpty());
# Line 329 | Line 326 | public class AbstractQueuedLongSynchroni
326       * tryAcquireNanos is interruptible.
327       */
328      public void testInterruptedException2() throws InterruptedException {
329 <        final Mutex sync = new Mutex();
330 <        sync.acquire(1);
331 <        Thread t = new Thread(new Runnable() {
332 <                public void run() {
333 <                    try {
334 <                        sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
338 <                        threadShouldThrow();
339 <                    } catch (InterruptedException success) {}
340 <                }
341 <            });
329 >        final Mutex sync = new Mutex();
330 >        sync.acquire(1);
331 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
332 >            public void realRun() throws InterruptedException {
333 >                sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS));
334 >            }});
335  
336          t.start();
337 +        Thread.sleep(SHORT_DELAY_MS);
338          t.interrupt();
339          t.join();
340      }
# Line 350 | Line 344 | public class AbstractQueuedLongSynchroni
344       * TryAcquire on exclusively held sync fails
345       */
346      public void testTryAcquireWhenSynced() throws InterruptedException {
347 <        final Mutex sync = new Mutex();
348 <        sync.acquire(1);
349 <        Thread t = new Thread(new Runnable() {
350 <                public void run() {
351 <                    threadAssertFalse(sync.tryAcquire(1));
352 <                }
359 <            });
347 >        final Mutex sync = new Mutex();
348 >        sync.acquire(1);
349 >        Thread t = new Thread(new CheckedRunnable() {
350 >            public void realRun() {
351 >                threadAssertFalse(sync.tryAcquire(1));
352 >            }});
353  
354          t.start();
355          t.join();
# Line 367 | Line 360 | public class AbstractQueuedLongSynchroni
360       * tryAcquireNanos on an exclusively held sync times out
361       */
362      public void testAcquireNanos_Timeout() throws InterruptedException {
363 <        final Mutex sync = new Mutex();
364 <        sync.acquire(1);
365 <        Thread t = new Thread(new Runnable() {
366 <                public void run() {
367 <                    try {
368 <                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
369 <                    } catch (Exception ex) {
377 <                        threadUnexpectedException(ex);
378 <                    }
379 <                }
380 <            });
363 >        final Mutex sync = new Mutex();
364 >        sync.acquire(1);
365 >        Thread t = new Thread(new CheckedRunnable() {
366 >            public void realRun() throws InterruptedException {
367 >                long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS);
368 >                assertFalse(sync.tryAcquireNanos(1, nanos));
369 >            }});
370  
371          t.start();
372          t.join();
# Line 389 | Line 378 | public class AbstractQueuedLongSynchroni
378       * getState is true when acquired and false when not
379       */
380      public void testGetState() throws InterruptedException {
381 <        final Mutex sync = new Mutex();
382 <        sync.acquire(1);
383 <        assertTrue(sync.isHeldExclusively());
384 <        sync.release(1);
385 <        assertFalse(sync.isHeldExclusively());
386 <        Thread t = new Thread(new Runnable() {
387 <                public void run() {
388 <                    sync.acquire(1);
389 <                    try {
390 <                        Thread.sleep(SMALL_DELAY_MS);
391 <                    }
403 <                    catch (Exception e) {
404 <                        threadUnexpectedException(e);
405 <                    }
406 <                    sync.release(1);
407 <                }
408 <            });
381 >        final Mutex sync = new Mutex();
382 >        sync.acquire(1);
383 >        assertTrue(sync.isHeldExclusively());
384 >        sync.release(1);
385 >        assertFalse(sync.isHeldExclusively());
386 >        Thread t = new Thread(new CheckedRunnable() {
387 >            public void realRun() throws InterruptedException {
388 >                sync.acquire(1);
389 >                Thread.sleep(SMALL_DELAY_MS);
390 >                sync.release(1);
391 >            }});
392  
393          t.start();
394          Thread.sleep(SHORT_DELAY_MS);
# Line 419 | Line 402 | public class AbstractQueuedLongSynchroni
402       * acquireInterruptibly is interruptible.
403       */
404      public void testAcquireInterruptibly1() throws InterruptedException {
405 <        final Mutex sync = new Mutex();
406 <        sync.acquire(1);
407 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
405 >        final Mutex sync = new Mutex();
406 >        sync.acquire(1);
407 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
408          t.start();
409          Thread.sleep(SHORT_DELAY_MS);
410          t.interrupt();
# Line 434 | Line 417 | public class AbstractQueuedLongSynchroni
417       * acquireInterruptibly succeeds when released, else is interruptible
418       */
419      public void testAcquireInterruptibly2() throws InterruptedException {
420 <        final Mutex sync = new Mutex();
420 >        final Mutex sync = new Mutex();
421          sync.acquireInterruptibly(1);
422 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
422 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
423          t.start();
424 +        Thread.sleep(SHORT_DELAY_MS);
425          t.interrupt();
426          assertTrue(sync.isHeldExclusively());
427          t.join();
# Line 447 | Line 431 | public class AbstractQueuedLongSynchroni
431       * owns is true for a condition created by sync else false
432       */
433      public void testOwns() {
434 <        final Mutex sync = new Mutex();
434 >        final Mutex sync = new Mutex();
435          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
436          final Mutex sync2 = new Mutex();
437          assertTrue(sync.owns(c));
# Line 458 | Line 442 | public class AbstractQueuedLongSynchroni
442       * Calling await without holding sync throws IllegalMonitorStateException
443       */
444      public void testAwait_IllegalMonitor() throws InterruptedException {
445 <        final Mutex sync = new Mutex();
445 >        final Mutex sync = new Mutex();
446          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
447          try {
448              c.await();
449              shouldThrow();
450 <        }
467 <        catch (IllegalMonitorStateException success) {
468 <        }
450 >        } catch (IllegalMonitorStateException success) {}
451      }
452  
453      /**
454       * Calling signal without holding sync throws IllegalMonitorStateException
455       */
456      public void testSignal_IllegalMonitor() throws InterruptedException {
457 <        final Mutex sync = new Mutex();
457 >        final Mutex sync = new Mutex();
458          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
459          try {
460              c.signal();
461              shouldThrow();
462 <        }
481 <        catch (IllegalMonitorStateException success) {}
462 >        } catch (IllegalMonitorStateException success) {}
463      }
464  
465      /**
466       * awaitNanos without a signal times out
467       */
468      public void testAwaitNanos_Timeout() throws InterruptedException {
469 <        final Mutex sync = new Mutex();
469 >        final Mutex sync = new Mutex();
470          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
471          sync.acquire(1);
472          long t = c.awaitNanos(100);
# Line 497 | Line 478 | public class AbstractQueuedLongSynchroni
478       *  Timed await without a signal times out
479       */
480      public void testAwait_Timeout() throws InterruptedException {
481 <        final Mutex sync = new Mutex();
481 >        final Mutex sync = new Mutex();
482          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
483          sync.acquire(1);
484 <        assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
484 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
485          sync.release(1);
486      }
487  
# Line 508 | Line 489 | public class AbstractQueuedLongSynchroni
489       * awaitUntil without a signal times out
490       */
491      public void testAwaitUntil_Timeout() throws InterruptedException {
492 <        final Mutex sync = new Mutex();
492 >        final Mutex sync = new Mutex();
493          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
494          sync.acquire(1);
495          java.util.Date d = new java.util.Date();
# Line 520 | Line 501 | public class AbstractQueuedLongSynchroni
501       * await returns when signalled
502       */
503      public void testAwait() throws InterruptedException {
504 <        final Mutex sync = new Mutex();
504 >        final Mutex sync = new Mutex();
505          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
506 <        Thread t = new Thread(new Runnable() {
507 <                public void run() {
508 <                    try {
509 <                        sync.acquire(1);
510 <                        c.await();
511 <                        sync.release(1);
531 <                    }
532 <                    catch (InterruptedException e) {
533 <                        threadUnexpectedException(e);
534 <                    }
535 <                }
536 <            });
506 >        Thread t = new Thread(new CheckedRunnable() {
507 >            public void realRun() throws InterruptedException {
508 >                sync.acquire(1);
509 >                c.await();
510 >                sync.release(1);
511 >            }});
512  
513          t.start();
514          Thread.sleep(SHORT_DELAY_MS);
# Line 550 | Line 525 | public class AbstractQueuedLongSynchroni
525       * hasWaiters throws NPE if null
526       */
527      public void testHasWaitersNPE() {
528 <        final Mutex sync = new Mutex();
528 >        final Mutex sync = new Mutex();
529          try {
530              sync.hasWaiters(null);
531              shouldThrow();
# Line 561 | Line 536 | public class AbstractQueuedLongSynchroni
536       * getWaitQueueLength throws NPE if null
537       */
538      public void testGetWaitQueueLengthNPE() {
539 <        final Mutex sync = new Mutex();
539 >        final Mutex sync = new Mutex();
540          try {
541              sync.getWaitQueueLength(null);
542              shouldThrow();
# Line 573 | Line 548 | public class AbstractQueuedLongSynchroni
548       * getWaitingThreads throws NPE if null
549       */
550      public void testGetWaitingThreadsNPE() {
551 <        final Mutex sync = new Mutex();
551 >        final Mutex sync = new Mutex();
552          try {
553              sync.getWaitingThreads(null);
554              shouldThrow();
# Line 585 | Line 560 | public class AbstractQueuedLongSynchroni
560       * hasWaiters throws IAE if not owned
561       */
562      public void testHasWaitersIAE() {
563 <        final Mutex sync = new Mutex();
563 >        final Mutex sync = new Mutex();
564          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
565 <        final Mutex sync2 = new Mutex();
565 >        final Mutex sync2 = new Mutex();
566          try {
567              sync2.hasWaiters(c);
568              shouldThrow();
# Line 598 | Line 573 | public class AbstractQueuedLongSynchroni
573       * hasWaiters throws IMSE if not synced
574       */
575      public void testHasWaitersIMSE() {
576 <        final Mutex sync = new Mutex();
576 >        final Mutex sync = new Mutex();
577          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
578          try {
579              sync.hasWaiters(c);
# Line 611 | Line 586 | public class AbstractQueuedLongSynchroni
586       * getWaitQueueLength throws IAE if not owned
587       */
588      public void testGetWaitQueueLengthIAE() {
589 <        final Mutex sync = new Mutex();
589 >        final Mutex sync = new Mutex();
590          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
591 <        final Mutex sync2 = new Mutex();
591 >        final Mutex sync2 = new Mutex();
592          try {
593              sync2.getWaitQueueLength(c);
594              shouldThrow();
# Line 624 | Line 599 | public class AbstractQueuedLongSynchroni
599       * getWaitQueueLength throws IMSE if not synced
600       */
601      public void testGetWaitQueueLengthIMSE() {
602 <        final Mutex sync = new Mutex();
602 >        final Mutex sync = new Mutex();
603          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
604          try {
605              sync.getWaitQueueLength(c);
# Line 637 | Line 612 | public class AbstractQueuedLongSynchroni
612       * getWaitingThreads throws IAE if not owned
613       */
614      public void testGetWaitingThreadsIAE() {
615 <        final Mutex sync = new Mutex();
615 >        final Mutex sync = new Mutex();
616          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
617 <        final Mutex sync2 = new Mutex();
617 >        final Mutex sync2 = new Mutex();
618          try {
619              sync2.getWaitingThreads(c);
620              shouldThrow();
# Line 650 | Line 625 | public class AbstractQueuedLongSynchroni
625       * getWaitingThreads throws IMSE if not synced
626       */
627      public void testGetWaitingThreadsIMSE() {
628 <        final Mutex sync = new Mutex();
628 >        final Mutex sync = new Mutex();
629          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
630          try {
631              sync.getWaitingThreads(c);
# Line 664 | Line 639 | public class AbstractQueuedLongSynchroni
639       * hasWaiters returns true when a thread is waiting, else false
640       */
641      public void testHasWaiters() throws InterruptedException {
642 <        final Mutex sync = new Mutex();
642 >        final Mutex sync = new Mutex();
643          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
644 <        Thread t = new Thread(new Runnable() {
645 <                public void run() {
646 <                    try {
647 <                        sync.acquire(1);
648 <                        threadAssertFalse(sync.hasWaiters(c));
649 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
650 <                        c.await();
651 <                        sync.release(1);
677 <                    }
678 <                    catch (InterruptedException e) {
679 <                        threadUnexpectedException(e);
680 <                    }
681 <                }
682 <            });
644 >        Thread t = new Thread(new CheckedRunnable() {
645 >            public void realRun() throws InterruptedException {
646 >                sync.acquire(1);
647 >                threadAssertFalse(sync.hasWaiters(c));
648 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
649 >                c.await();
650 >                sync.release(1);
651 >            }});
652  
653          t.start();
654          Thread.sleep(SHORT_DELAY_MS);
# Line 701 | Line 670 | public class AbstractQueuedLongSynchroni
670       * getWaitQueueLength returns number of waiting threads
671       */
672      public void testGetWaitQueueLength() throws InterruptedException {
673 <        final Mutex sync = new Mutex();
673 >        final Mutex sync = new Mutex();
674          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
675 <        Thread t1 = new Thread(new Runnable() {
676 <                public void run() {
677 <                    try {
678 <                        sync.acquire(1);
679 <                        threadAssertFalse(sync.hasWaiters(c));
680 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
681 <                        c.await();
682 <                        sync.release(1);
683 <                    }
684 <                    catch (InterruptedException e) {
685 <                        threadUnexpectedException(e);
686 <                    }
687 <                }
688 <            });
689 <
690 <        Thread t2 = new Thread(new Runnable() {
691 <                public void run() {
723 <                    try {
724 <                        sync.acquire(1);
725 <                        threadAssertTrue(sync.hasWaiters(c));
726 <                        threadAssertEquals(1, sync.getWaitQueueLength(c));
727 <                        c.await();
728 <                        sync.release(1);
729 <                    }
730 <                    catch (InterruptedException e) {
731 <                        threadUnexpectedException(e);
732 <                    }
733 <                }
734 <            });
675 >        Thread t1 = new Thread(new CheckedRunnable() {
676 >            public void realRun() throws InterruptedException {
677 >                sync.acquire(1);
678 >                threadAssertFalse(sync.hasWaiters(c));
679 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
680 >                c.await();
681 >                sync.release(1);
682 >            }});
683 >
684 >        Thread t2 = new Thread(new CheckedRunnable() {
685 >            public void realRun() throws InterruptedException {
686 >                sync.acquire(1);
687 >                threadAssertTrue(sync.hasWaiters(c));
688 >                threadAssertEquals(1, sync.getWaitQueueLength(c));
689 >                c.await();
690 >                sync.release(1);
691 >            }});
692  
693          t1.start();
694          Thread.sleep(SHORT_DELAY_MS);
# Line 757 | Line 714 | public class AbstractQueuedLongSynchroni
714       * getWaitingThreads returns only and all waiting threads
715       */
716      public void testGetWaitingThreads() throws InterruptedException {
717 <        final Mutex sync = new Mutex();
717 >        final Mutex sync = new Mutex();
718          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
719 <        Thread t1 = new Thread(new Runnable() {
720 <                public void run() {
721 <                    try {
722 <                        sync.acquire(1);
723 <                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
724 <                        c.await();
725 <                        sync.release(1);
726 <                    }
727 <                    catch (InterruptedException e) {
728 <                        threadUnexpectedException(e);
729 <                    }
730 <                }
731 <            });
732 <
733 <        Thread t2 = new Thread(new Runnable() {
777 <                public void run() {
778 <                    try {
779 <                        sync.acquire(1);
780 <                        threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
781 <                        c.await();
782 <                        sync.release(1);
783 <                    }
784 <                    catch (InterruptedException e) {
785 <                        threadUnexpectedException(e);
786 <                    }
787 <                }
788 <            });
719 >        Thread t1 = new Thread(new CheckedRunnable() {
720 >            public void realRun() throws InterruptedException {
721 >                sync.acquire(1);
722 >                threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
723 >                c.await();
724 >                sync.release(1);
725 >            }});
726 >
727 >        Thread t2 = new Thread(new CheckedRunnable() {
728 >            public void realRun() throws InterruptedException {
729 >                sync.acquire(1);
730 >                threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
731 >                c.await();
732 >                sync.release(1);
733 >            }});
734  
735              sync.acquire(1);
736              assertTrue(sync.getWaitingThreads(c).isEmpty());
# Line 817 | Line 762 | public class AbstractQueuedLongSynchroni
762       * awaitUninterruptibly doesn't abort on interrupt
763       */
764      public void testAwaitUninterruptibly() throws InterruptedException {
765 <        final Mutex sync = new Mutex();
765 >        final Mutex sync = new Mutex();
766          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
767 <        Thread t = new Thread(new Runnable() {
768 <                public void run() {
769 <                    sync.acquire(1);
770 <                    c.awaitUninterruptibly();
771 <                    sync.release(1);
772 <                }
828 <            });
767 >        Thread t = new Thread(new CheckedRunnable() {
768 >            public void realRun() {
769 >                sync.acquire(1);
770 >                c.awaitUninterruptibly();
771 >                sync.release(1);
772 >            }});
773  
774          t.start();
775          Thread.sleep(SHORT_DELAY_MS);
# Line 841 | Line 785 | public class AbstractQueuedLongSynchroni
785       * await is interruptible
786       */
787      public void testAwait_Interrupt() throws InterruptedException {
788 <        final Mutex sync = new Mutex();
788 >        final Mutex sync = new Mutex();
789          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
790 <        Thread t = new Thread(new Runnable() {
791 <                public void run() {
792 <                    try {
793 <                        sync.acquire(1);
794 <                        c.await();
851 <                        sync.release(1);
852 <                        threadShouldThrow();
853 <                    }
854 <                    catch (InterruptedException success) {
855 <                    }
856 <                }
857 <            });
790 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
791 >            public void realRun() throws InterruptedException {
792 >                sync.acquire(1);
793 >                c.await();
794 >            }});
795  
796          t.start();
797          Thread.sleep(SHORT_DELAY_MS);
# Line 867 | Line 804 | public class AbstractQueuedLongSynchroni
804       * awaitNanos is interruptible
805       */
806      public void testAwaitNanos_Interrupt() throws InterruptedException {
807 <        final Mutex sync = new Mutex();
807 >        final Mutex sync = new Mutex();
808          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
809 <        Thread t = new Thread(new Runnable() {
810 <                public void run() {
811 <                    try {
812 <                        sync.acquire(1);
813 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
877 <                        sync.release(1);
878 <                        threadShouldThrow();
879 <                    }
880 <                    catch (InterruptedException success) {
881 <                    }
882 <                }
883 <            });
809 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
810 >            public void realRun() throws InterruptedException {
811 >                sync.acquire(1);
812 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
813 >            }});
814  
815          t.start();
816          Thread.sleep(SHORT_DELAY_MS);
# Line 893 | Line 823 | public class AbstractQueuedLongSynchroni
823       * awaitUntil is interruptible
824       */
825      public void testAwaitUntil_Interrupt() throws InterruptedException {
826 <        final Mutex sync = new Mutex();
826 >        final Mutex sync = new Mutex();
827          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
828 <        Thread t = new Thread(new Runnable() {
829 <                public void run() {
830 <                    try {
831 <                        sync.acquire(1);
832 <                        java.util.Date d = new java.util.Date();
833 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
904 <                        sync.release(1);
905 <                        threadShouldThrow();
906 <                    }
907 <                    catch (InterruptedException success) {
908 <                    }
909 <                }
910 <            });
828 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
829 >            public void realRun() throws InterruptedException {
830 >                sync.acquire(1);
831 >                java.util.Date d = new java.util.Date();
832 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
833 >            }});
834  
835          t.start();
836          Thread.sleep(SHORT_DELAY_MS);
# Line 920 | Line 843 | public class AbstractQueuedLongSynchroni
843       * signalAll wakes up all threads
844       */
845      public void testSignalAll() throws InterruptedException {
846 <        final Mutex sync = new Mutex();
846 >        final Mutex sync = new Mutex();
847          final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition();
848 <        Thread t1 = new Thread(new Runnable() {
849 <                public void run() {
850 <                    try {
851 <                        sync.acquire(1);
852 <                        c.await();
853 <                        sync.release(1);
854 <                    }
855 <                    catch (InterruptedException e) {
856 <                        threadUnexpectedException();
857 <                    }
858 <                }
859 <            });
860 <
938 <        Thread t2 = new Thread(new Runnable() {
939 <                public void run() {
940 <                    try {
941 <                        sync.acquire(1);
942 <                        c.await();
943 <                        sync.release(1);
944 <                    }
945 <                    catch (InterruptedException e) {
946 <                        threadUnexpectedException();
947 <                    }
948 <                }
949 <            });
848 >        Thread t1 = new Thread(new CheckedRunnable() {
849 >            public void realRun() throws InterruptedException {
850 >                sync.acquire(1);
851 >                c.await();
852 >                sync.release(1);
853 >            }});
854 >
855 >        Thread t2 = new Thread(new CheckedRunnable() {
856 >            public void realRun() throws InterruptedException {
857 >                sync.acquire(1);
858 >                c.await();
859 >                sync.release(1);
860 >            }});
861  
862          t1.start();
863          t2.start();
# Line 997 | Line 908 | public class AbstractQueuedLongSynchroni
908       * tryReleaseShared setting state changes getState
909       */
910      public void testGetStateWithReleaseShared() {
911 <        final BooleanLatch l = new BooleanLatch();
912 <        assertFalse(l.isSignalled());
913 <        l.releaseShared(0);
914 <        assertTrue(l.isSignalled());
911 >        final BooleanLatch l = new BooleanLatch();
912 >        assertFalse(l.isSignalled());
913 >        l.releaseShared(0);
914 >        assertTrue(l.isSignalled());
915      }
916  
917      /**
918       * releaseShared has no effect when already signalled
919       */
920      public void testReleaseShared() {
921 <        final BooleanLatch l = new BooleanLatch();
922 <        assertFalse(l.isSignalled());
923 <        l.releaseShared(0);
924 <        assertTrue(l.isSignalled());
925 <        l.releaseShared(0);
926 <        assertTrue(l.isSignalled());
921 >        final BooleanLatch l = new BooleanLatch();
922 >        assertFalse(l.isSignalled());
923 >        l.releaseShared(0);
924 >        assertTrue(l.isSignalled());
925 >        l.releaseShared(0);
926 >        assertTrue(l.isSignalled());
927      }
928  
929      /**
930       * acquireSharedInterruptibly returns after release, but not before
931       */
932      public void testAcquireSharedInterruptibly() throws InterruptedException {
933 <        final BooleanLatch l = new BooleanLatch();
933 >        final BooleanLatch l = new BooleanLatch();
934  
935 <        Thread t = new Thread(new Runnable() {
936 <                public void run() {
937 <                    try {
938 <                        threadAssertFalse(l.isSignalled());
939 <                        l.acquireSharedInterruptibly(0);
940 <                        threadAssertTrue(l.isSignalled());
1030 <                    } catch (InterruptedException e) {
1031 <                        threadUnexpectedException();
1032 <                    }
1033 <                }
1034 <            });
935 >        Thread t = new Thread(new CheckedRunnable() {
936 >            public void realRun() throws InterruptedException {
937 >                threadAssertFalse(l.isSignalled());
938 >                l.acquireSharedInterruptibly(0);
939 >                threadAssertTrue(l.isSignalled());
940 >            }});
941  
942          t.start();
943          assertFalse(l.isSignalled());
# Line 1046 | Line 952 | public class AbstractQueuedLongSynchroni
952       * acquireSharedTimed returns after release
953       */
954      public void testAsquireSharedTimed() throws InterruptedException {
955 <        final BooleanLatch l = new BooleanLatch();
955 >        final BooleanLatch l = new BooleanLatch();
956  
957 <        Thread t = new Thread(new Runnable() {
958 <                public void run() {
959 <                    try {
960 <                        threadAssertFalse(l.isSignalled());
961 <                        threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
962 <                        threadAssertTrue(l.isSignalled());
963 <
1058 <                    } catch (InterruptedException e) {
1059 <                        threadUnexpectedException();
1060 <                    }
1061 <                }
1062 <            });
957 >        Thread t = new Thread(new CheckedRunnable() {
958 >            public void realRun() throws InterruptedException {
959 >                assertFalse(l.isSignalled());
960 >                long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS);
961 >                assertTrue(l.tryAcquireSharedNanos(0, nanos));
962 >                assertTrue(l.isSignalled());
963 >            }});
964  
965          t.start();
966          assertFalse(l.isSignalled());
# Line 1074 | Line 975 | public class AbstractQueuedLongSynchroni
975       */
976      public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
977          final BooleanLatch l = new BooleanLatch();
978 <        Thread t = new Thread(new Runnable() {
979 <                public void run() {
980 <                    try {
981 <                        threadAssertFalse(l.isSignalled());
982 <                        l.acquireSharedInterruptibly(0);
1082 <                        threadShouldThrow();
1083 <                    } catch (InterruptedException success) {}
1084 <                }
1085 <            });
978 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
979 >            public void realRun() throws InterruptedException {
980 >                threadAssertFalse(l.isSignalled());
981 >                l.acquireSharedInterruptibly(0);
982 >            }});
983  
984 <        t.start();
984 >        t.start();
985          assertFalse(l.isSignalled());
986          t.interrupt();
987          t.join();
# Line 1095 | Line 992 | public class AbstractQueuedLongSynchroni
992       */
993      public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
994          final BooleanLatch l = new BooleanLatch();
995 <        Thread t = new Thread(new Runnable() {
996 <                public void run() {
997 <                    try {
998 <                        threadAssertFalse(l.isSignalled());
999 <                        l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
1000 <                        threadShouldThrow();
1104 <                    } catch (InterruptedException success) {}
1105 <                }
1106 <            });
995 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
996 >            public void realRun() throws InterruptedException {
997 >                assertFalse(l.isSignalled());
998 >                long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS);
999 >                l.tryAcquireSharedNanos(0, nanos);
1000 >            }});
1001  
1002          t.start();
1003          Thread.sleep(SHORT_DELAY_MS);
# Line 1117 | Line 1011 | public class AbstractQueuedLongSynchroni
1011       */
1012      public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1013          final BooleanLatch l = new BooleanLatch();
1014 <        Thread t = new Thread(new Runnable() {
1015 <                public void run() {
1016 <                    try {
1017 <                        threadAssertFalse(l.isSignalled());
1018 <                        threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1019 <                    } catch (InterruptedException ie) {
1126 <                        threadUnexpectedException();
1127 <                    }
1128 <                }
1129 <            });
1014 >        Thread t = new Thread(new CheckedRunnable() {
1015 >            public void realRun() throws InterruptedException {
1016 >                assertFalse(l.isSignalled());
1017 >                long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS);
1018 >                assertFalse(l.tryAcquireSharedNanos(0, nanos));
1019 >            }});
1020  
1021          t.start();
1022          Thread.sleep(SHORT_DELAY_MS);
# Line 1134 | Line 1024 | public class AbstractQueuedLongSynchroni
1024          t.join();
1025      }
1026  
1137
1027   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines