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

Comparing jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java (file contents):
Revision 1.26 by jsr166, Wed Nov 18 08:22:57 2009 UTC vs.
Revision 1.32 by jsr166, Mon Nov 30 08:31:09 2009 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  
# Line 65 | Line 66 | public class AbstractQueuedSynchronizerT
66      }
67  
68      /**
69 <     * A runnable calling acquireInterruptibly
69 >     * A runnable calling acquireInterruptibly that does not expect to
70 >     * be interrupted.
71       */
72 <    class InterruptibleSyncRunnable implements Runnable {
72 >    class InterruptibleSyncRunnable extends CheckedRunnable {
73          final Mutex sync;
74          InterruptibleSyncRunnable(Mutex l) { sync = l; }
75 <        public void run() {
76 <            try {
75 <                sync.acquireInterruptibly(1);
76 <            } catch (InterruptedException success) {}
75 >        public void realRun() throws InterruptedException {
76 >            sync.acquireInterruptibly(1);
77          }
78      }
79  
80  
81      /**
82       * A runnable calling acquireInterruptibly that expects to be
83 <     * interrupted
83 >     * interrupted.
84       */
85 <    class InterruptedSyncRunnable implements Runnable {
85 >    class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
86          final Mutex sync;
87          InterruptedSyncRunnable(Mutex l) { sync = l; }
88 <        public void run() {
89 <            try {
90 <                sync.acquireInterruptibly(1);
91 <                threadShouldThrow();
92 <            } catch (InterruptedException success) {}
88 >        public void realRun() throws InterruptedException {
89 >            sync.acquireInterruptibly(1);
90          }
91      }
92  
# Line 97 | Line 94 | public class AbstractQueuedSynchronizerT
94       * isHeldExclusively is false upon construction
95       */
96      public void testIsHeldExclusively() {
97 <        Mutex rl = new Mutex();
97 >        Mutex rl = new Mutex();
98          assertFalse(rl.isHeldExclusively());
99      }
100  
# Line 105 | Line 102 | public class AbstractQueuedSynchronizerT
102       * acquiring released sync succeeds
103       */
104      public void testAcquire() {
105 <        Mutex rl = new Mutex();
105 >        Mutex rl = new Mutex();
106          rl.acquire(1);
107          assertTrue(rl.isHeldExclusively());
108          rl.release(1);
# Line 116 | Line 113 | public class AbstractQueuedSynchronizerT
113       * tryAcquire on an released sync succeeds
114       */
115      public void testTryAcquire() {
116 <        Mutex rl = new Mutex();
116 >        Mutex rl = new Mutex();
117          assertTrue(rl.tryAcquire(1));
118          assertTrue(rl.isHeldExclusively());
119          rl.release(1);
# Line 126 | Line 123 | public class AbstractQueuedSynchronizerT
123       * hasQueuedThreads reports whether there are waiting threads
124       */
125      public void testhasQueuedThreads() throws InterruptedException {
126 <        final Mutex sync = new Mutex();
126 >        final Mutex sync = new Mutex();
127          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
128          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
129          assertFalse(sync.hasQueuedThreads());
# Line 151 | Line 148 | public class AbstractQueuedSynchronizerT
148       * isQueued(null) throws NPE
149       */
150      public void testIsQueuedNPE() {
151 <        final Mutex sync = new Mutex();
151 >        final Mutex sync = new Mutex();
152          try {
153              sync.isQueued(null);
154              shouldThrow();
# Line 162 | Line 159 | public class AbstractQueuedSynchronizerT
159       * isQueued reports whether a thread is queued.
160       */
161      public void testIsQueued() throws InterruptedException {
162 <        final Mutex sync = new Mutex();
162 >        final Mutex sync = new Mutex();
163          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
164          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
165          assertFalse(sync.isQueued(t1));
# Line 192 | Line 189 | public class AbstractQueuedSynchronizerT
189       * getFirstQueuedThread returns first waiting thread or null if none
190       */
191      public void testGetFirstQueuedThread() throws InterruptedException {
192 <        final Mutex sync = new Mutex();
192 >        final Mutex sync = new Mutex();
193          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
194          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
195          assertNull(sync.getFirstQueuedThread());
# Line 219 | Line 216 | public class AbstractQueuedSynchronizerT
216       * hasContended reports false if no thread has ever blocked, else true
217       */
218      public void testHasContended() throws InterruptedException {
219 <        final Mutex sync = new Mutex();
219 >        final Mutex sync = new Mutex();
220          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
221          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
222          assertFalse(sync.hasContended());
# Line 244 | Line 241 | public class AbstractQueuedSynchronizerT
241       * getQueuedThreads includes waiting threads
242       */
243      public void testGetQueuedThreads() throws InterruptedException {
244 <        final Mutex sync = new Mutex();
244 >        final Mutex sync = new Mutex();
245          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
246          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
247          assertTrue(sync.getQueuedThreads().isEmpty());
# Line 272 | Line 269 | public class AbstractQueuedSynchronizerT
269       * getExclusiveQueuedThreads includes waiting threads
270       */
271      public void testGetExclusiveQueuedThreads() throws InterruptedException {
272 <        final Mutex sync = new Mutex();
272 >        final Mutex sync = new Mutex();
273          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
274          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
275          assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
# Line 300 | Line 297 | public class AbstractQueuedSynchronizerT
297       * getSharedQueuedThreads does not include exclusively waiting threads
298       */
299      public void testGetSharedQueuedThreads() throws InterruptedException {
300 <        final Mutex sync = new Mutex();
300 >        final Mutex sync = new Mutex();
301          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
302          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
303          assertTrue(sync.getSharedQueuedThreads().isEmpty());
# Line 325 | Line 322 | public class AbstractQueuedSynchronizerT
322      /**
323       * tryAcquireNanos is interruptible.
324       */
325 <    public void testInterruptedException2() {
326 <        final Mutex sync = new Mutex();
327 <        sync.acquire(1);
328 <        Thread t = new Thread(new Runnable() {
329 <                public void run() {
330 <                    try {
331 <                        sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
335 <                        threadShouldThrow();
336 <                    } catch (InterruptedException success) {}
337 <                }
338 <            });
325 >    public void testInterruptedException2() throws InterruptedException {
326 >        final Mutex sync = new Mutex();
327 >        sync.acquire(1);
328 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
329 >            public void realRun() throws InterruptedException {
330 >                sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000L * 1000L);
331 >            }});
332  
333          t.start();
334 +        Thread.sleep(SHORT_DELAY_MS);
335          t.interrupt();
336 +        t.join();
337      }
338  
339  
# Line 346 | Line 341 | public class AbstractQueuedSynchronizerT
341       * TryAcquire on exclusively held sync fails
342       */
343      public void testTryAcquireWhenSynced() throws InterruptedException {
344 <        final Mutex sync = new Mutex();
345 <        sync.acquire(1);
346 <        Thread t = new Thread(new Runnable() {
347 <                public void run() {
348 <                    threadAssertFalse(sync.tryAcquire(1));
349 <                }
355 <            });
344 >        final Mutex sync = new Mutex();
345 >        sync.acquire(1);
346 >        Thread t = new Thread(new CheckedRunnable() {
347 >            public void realRun() {
348 >                threadAssertFalse(sync.tryAcquire(1));
349 >            }});
350  
351          t.start();
352          t.join();
# Line 363 | Line 357 | public class AbstractQueuedSynchronizerT
357       * tryAcquireNanos on an exclusively held sync times out
358       */
359      public void testAcquireNanos_Timeout() throws InterruptedException {
360 <        final Mutex sync = new Mutex();
361 <        sync.acquire(1);
362 <        Thread t = new Thread(new Runnable() {
363 <                public void run() {
364 <                    try {
365 <                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
372 <                    } catch (Exception ex) {
373 <                        threadUnexpectedException();
374 <                    }
375 <                }
376 <            });
360 >        final Mutex sync = new Mutex();
361 >        sync.acquire(1);
362 >        Thread t = new Thread(new CheckedRunnable() {
363 >            public void realRun() throws InterruptedException {
364 >                threadAssertFalse(sync.tryAcquireNanos(1, SHORT_DELAY_MS * 1000L * 1000L));
365 >            }});
366  
367          t.start();
368          t.join();
# Line 385 | Line 374 | public class AbstractQueuedSynchronizerT
374       * getState is true when acquired and false when not
375       */
376      public void testGetState() throws InterruptedException {
377 <        final Mutex sync = new Mutex();
378 <        sync.acquire(1);
379 <        assertTrue(sync.isHeldExclusively());
380 <        sync.release(1);
381 <        assertFalse(sync.isHeldExclusively());
382 <        Thread t = new Thread(new Runnable() {
383 <                public void run() {
384 <                    sync.acquire(1);
385 <                    try {
386 <                        Thread.sleep(SMALL_DELAY_MS);
387 <                    }
399 <                    catch (Exception e) {
400 <                        threadUnexpectedException();
401 <                    }
402 <                    sync.release(1);
403 <                }
404 <            });
377 >        final Mutex sync = new Mutex();
378 >        sync.acquire(1);
379 >        assertTrue(sync.isHeldExclusively());
380 >        sync.release(1);
381 >        assertFalse(sync.isHeldExclusively());
382 >        Thread t = new Thread(new CheckedRunnable() {
383 >            public void realRun() throws InterruptedException {
384 >                sync.acquire(1);
385 >                Thread.sleep(SMALL_DELAY_MS);
386 >                sync.release(1);
387 >            }});
388  
389          t.start();
390          Thread.sleep(SHORT_DELAY_MS);
# Line 415 | Line 398 | public class AbstractQueuedSynchronizerT
398       * acquireInterruptibly is interruptible.
399       */
400      public void testAcquireInterruptibly1() throws InterruptedException {
401 <        final Mutex sync = new Mutex();
402 <        sync.acquire(1);
403 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
401 >        final Mutex sync = new Mutex();
402 >        sync.acquire(1);
403 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
404  
405          t.start();
406          Thread.sleep(SHORT_DELAY_MS);
# Line 431 | Line 414 | public class AbstractQueuedSynchronizerT
414       * acquireInterruptibly succeeds when released, else is interruptible
415       */
416      public void testAcquireInterruptibly2() throws InterruptedException {
417 <        final Mutex sync = new Mutex();
417 >        final Mutex sync = new Mutex();
418          sync.acquireInterruptibly(1);
419 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
419 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
420          t.start();
421 +        Thread.sleep(SHORT_DELAY_MS);
422          t.interrupt();
423          assertTrue(sync.isHeldExclusively());
424          t.join();
# Line 444 | Line 428 | public class AbstractQueuedSynchronizerT
428       * owns is true for a condition created by sync else false
429       */
430      public void testOwns() {
431 <        final Mutex sync = new Mutex();
431 >        final Mutex sync = new Mutex();
432          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
433          final Mutex sync2 = new Mutex();
434          assertTrue(sync.owns(c));
# Line 455 | Line 439 | public class AbstractQueuedSynchronizerT
439       * Calling await without holding sync throws IllegalMonitorStateException
440       */
441      public void testAwait_IllegalMonitor() throws InterruptedException {
442 <        final Mutex sync = new Mutex();
442 >        final Mutex sync = new Mutex();
443          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
444          try {
445              c.await();
446              shouldThrow();
447 <        }
464 <        catch (IllegalMonitorStateException success) {}
447 >        } catch (IllegalMonitorStateException success) {}
448      }
449  
450      /**
451       * Calling signal without holding sync throws IllegalMonitorStateException
452       */
453      public void testSignal_IllegalMonitor() {
454 <        final Mutex sync = new Mutex();
454 >        final Mutex sync = new Mutex();
455          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
456          try {
457              c.signal();
458              shouldThrow();
459 <        }
477 <        catch (IllegalMonitorStateException success) {}
459 >        } catch (IllegalMonitorStateException success) {}
460      }
461  
462      /**
463       * awaitNanos without a signal times out
464       */
465      public void testAwaitNanos_Timeout() throws InterruptedException {
466 <        final Mutex sync = new Mutex();
466 >        final Mutex sync = new Mutex();
467          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
468          sync.acquire(1);
469          long t = c.awaitNanos(100);
# Line 493 | Line 475 | public class AbstractQueuedSynchronizerT
475       *  Timed await without a signal times out
476       */
477      public void testAwait_Timeout() throws InterruptedException {
478 <        final Mutex sync = new Mutex();
478 >        final Mutex sync = new Mutex();
479          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
480          sync.acquire(1);
481 <        assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
481 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
482          sync.release(1);
483      }
484  
# Line 504 | Line 486 | public class AbstractQueuedSynchronizerT
486       * awaitUntil without a signal times out
487       */
488      public void testAwaitUntil_Timeout() throws InterruptedException {
489 <        final Mutex sync = new Mutex();
489 >        final Mutex sync = new Mutex();
490          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
491          sync.acquire(1);
492          java.util.Date d = new java.util.Date();
# Line 516 | Line 498 | public class AbstractQueuedSynchronizerT
498       * await returns when signalled
499       */
500      public void testAwait() throws InterruptedException {
501 <        final Mutex sync = new Mutex();
501 >        final Mutex sync = new Mutex();
502          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
503 <        Thread t = new Thread(new Runnable() {
504 <                public void run() {
505 <                    try {
506 <                        sync.acquire(1);
507 <                        c.await();
508 <                        sync.release(1);
527 <                    }
528 <                    catch (InterruptedException e) {
529 <                        threadUnexpectedException();
530 <                    }
531 <                }
532 <            });
503 >        Thread t = new Thread(new CheckedRunnable() {
504 >            public void realRun() throws InterruptedException {
505 >                sync.acquire(1);
506 >                c.await();
507 >                sync.release(1);
508 >            }});
509  
510          t.start();
511          Thread.sleep(SHORT_DELAY_MS);
# Line 546 | Line 522 | public class AbstractQueuedSynchronizerT
522       * hasWaiters throws NPE if null
523       */
524      public void testHasWaitersNPE() {
525 <        final Mutex sync = new Mutex();
525 >        final Mutex sync = new Mutex();
526          try {
527              sync.hasWaiters(null);
528              shouldThrow();
# Line 557 | Line 533 | public class AbstractQueuedSynchronizerT
533       * getWaitQueueLength throws NPE if null
534       */
535      public void testGetWaitQueueLengthNPE() {
536 <        final Mutex sync = new Mutex();
536 >        final Mutex sync = new Mutex();
537          try {
538              sync.getWaitQueueLength(null);
539              shouldThrow();
# Line 569 | Line 545 | public class AbstractQueuedSynchronizerT
545       * getWaitingThreads throws NPE if null
546       */
547      public void testGetWaitingThreadsNPE() {
548 <        final Mutex sync = new Mutex();
548 >        final Mutex sync = new Mutex();
549          try {
550              sync.getWaitingThreads(null);
551              shouldThrow();
# Line 581 | Line 557 | public class AbstractQueuedSynchronizerT
557       * hasWaiters throws IAE if not owned
558       */
559      public void testHasWaitersIAE() {
560 <        final Mutex sync = new Mutex();
560 >        final Mutex sync = new Mutex();
561          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
562 <        final Mutex sync2 = new Mutex();
562 >        final Mutex sync2 = new Mutex();
563          try {
564              sync2.hasWaiters(c);
565              shouldThrow();
# Line 594 | Line 570 | public class AbstractQueuedSynchronizerT
570       * hasWaiters throws IMSE if not synced
571       */
572      public void testHasWaitersIMSE() {
573 <        final Mutex sync = new Mutex();
573 >        final Mutex sync = new Mutex();
574          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
575          try {
576              sync.hasWaiters(c);
# Line 607 | Line 583 | public class AbstractQueuedSynchronizerT
583       * getWaitQueueLength throws IAE if not owned
584       */
585      public void testGetWaitQueueLengthIAE() {
586 <        final Mutex sync = new Mutex();
586 >        final Mutex sync = new Mutex();
587          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
588 <        final Mutex sync2 = new Mutex();
588 >        final Mutex sync2 = new Mutex();
589          try {
590              sync2.getWaitQueueLength(c);
591              shouldThrow();
# Line 620 | Line 596 | public class AbstractQueuedSynchronizerT
596       * getWaitQueueLength throws IMSE if not synced
597       */
598      public void testGetWaitQueueLengthIMSE() {
599 <        final Mutex sync = new Mutex();
599 >        final Mutex sync = new Mutex();
600          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
601          try {
602              sync.getWaitQueueLength(c);
# Line 633 | Line 609 | public class AbstractQueuedSynchronizerT
609       * getWaitingThreads throws IAE if not owned
610       */
611      public void testGetWaitingThreadsIAE() {
612 <        final Mutex sync = new Mutex();
612 >        final Mutex sync = new Mutex();
613          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
614 <        final Mutex sync2 = new Mutex();
614 >        final Mutex sync2 = new Mutex();
615          try {
616              sync2.getWaitingThreads(c);
617              shouldThrow();
# Line 646 | Line 622 | public class AbstractQueuedSynchronizerT
622       * getWaitingThreads throws IMSE if not synced
623       */
624      public void testGetWaitingThreadsIMSE() {
625 <        final Mutex sync = new Mutex();
625 >        final Mutex sync = new Mutex();
626          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
627          try {
628              sync.getWaitingThreads(c);
# Line 660 | Line 636 | public class AbstractQueuedSynchronizerT
636       * hasWaiters returns true when a thread is waiting, else false
637       */
638      public void testHasWaiters() throws InterruptedException {
639 <        final Mutex sync = new Mutex();
639 >        final Mutex sync = new Mutex();
640          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
641 <        Thread t = new Thread(new Runnable() {
642 <                public void run() {
643 <                    try {
644 <                        sync.acquire(1);
645 <                        threadAssertFalse(sync.hasWaiters(c));
646 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
647 <                        c.await();
648 <                        sync.release(1);
673 <                    }
674 <                    catch (InterruptedException e) {
675 <                        threadUnexpectedException();
676 <                    }
677 <                }
678 <            });
641 >        Thread t = new Thread(new CheckedRunnable() {
642 >            public void realRun() throws InterruptedException {
643 >                sync.acquire(1);
644 >                threadAssertFalse(sync.hasWaiters(c));
645 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
646 >                c.await();
647 >                sync.release(1);
648 >            }});
649  
650          t.start();
651          Thread.sleep(SHORT_DELAY_MS);
# Line 697 | Line 667 | public class AbstractQueuedSynchronizerT
667       * getWaitQueueLength returns number of waiting threads
668       */
669      public void testGetWaitQueueLength() throws InterruptedException {
670 <        final Mutex sync = new Mutex();
670 >        final Mutex sync = new Mutex();
671          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
672 <        Thread t1 = new Thread(new Runnable() {
673 <                public void run() {
674 <                    try {
675 <                        sync.acquire(1);
676 <                        threadAssertFalse(sync.hasWaiters(c));
677 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
678 <                        c.await();
679 <                        sync.release(1);
680 <                    }
681 <                    catch (InterruptedException e) {
682 <                        threadUnexpectedException();
683 <                    }
684 <                }
685 <            });
686 <
687 <        Thread t2 = new Thread(new Runnable() {
688 <                public void run() {
719 <                    try {
720 <                        sync.acquire(1);
721 <                        threadAssertTrue(sync.hasWaiters(c));
722 <                        threadAssertEquals(1, sync.getWaitQueueLength(c));
723 <                        c.await();
724 <                        sync.release(1);
725 <                    }
726 <                    catch (InterruptedException e) {
727 <                        threadUnexpectedException();
728 <                    }
729 <                }
730 <            });
672 >        Thread t1 = new Thread(new CheckedRunnable() {
673 >            public void realRun() throws InterruptedException {
674 >                sync.acquire(1);
675 >                threadAssertFalse(sync.hasWaiters(c));
676 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
677 >                c.await();
678 >                sync.release(1);
679 >            }});
680 >
681 >        Thread t2 = new Thread(new CheckedRunnable() {
682 >            public void realRun() throws InterruptedException {
683 >                sync.acquire(1);
684 >                threadAssertTrue(sync.hasWaiters(c));
685 >                threadAssertEquals(1, sync.getWaitQueueLength(c));
686 >                c.await();
687 >                sync.release(1);
688 >            }});
689  
690          t1.start();
691          Thread.sleep(SHORT_DELAY_MS);
# Line 753 | Line 711 | public class AbstractQueuedSynchronizerT
711       * getWaitingThreads returns only and all waiting threads
712       */
713      public void testGetWaitingThreads() throws InterruptedException {
714 <        final Mutex sync = new Mutex();
714 >        final Mutex sync = new Mutex();
715          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
716 <        Thread t1 = new Thread(new Runnable() {
717 <                public void run() {
718 <                    try {
719 <                        sync.acquire(1);
720 <                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
721 <                        c.await();
722 <                        sync.release(1);
723 <                    }
724 <                    catch (InterruptedException e) {
725 <                        threadUnexpectedException();
726 <                    }
727 <                }
728 <            });
729 <
730 <        Thread t2 = new Thread(new Runnable() {
773 <                public void run() {
774 <                    try {
775 <                        sync.acquire(1);
776 <                        threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
777 <                        c.await();
778 <                        sync.release(1);
779 <                    }
780 <                    catch (InterruptedException e) {
781 <                        threadUnexpectedException();
782 <                    }
783 <                }
784 <            });
716 >        Thread t1 = new Thread(new CheckedRunnable() {
717 >            public void realRun() throws InterruptedException {
718 >                sync.acquire(1);
719 >                threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
720 >                c.await();
721 >                sync.release(1);
722 >            }});
723 >
724 >        Thread t2 = new Thread(new CheckedRunnable() {
725 >            public void realRun() throws InterruptedException {
726 >                sync.acquire(1);
727 >                threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
728 >                c.await();
729 >                sync.release(1);
730 >            }});
731  
732          sync.acquire(1);
733          assertTrue(sync.getWaitingThreads(c).isEmpty());
# Line 813 | Line 759 | public class AbstractQueuedSynchronizerT
759       * awaitUninterruptibly doesn't abort on interrupt
760       */
761      public void testAwaitUninterruptibly() throws InterruptedException {
762 <        final Mutex sync = new Mutex();
762 >        final Mutex sync = new Mutex();
763          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
764 <        Thread t = new Thread(new Runnable() {
765 <                public void run() {
766 <                    sync.acquire(1);
767 <                    c.awaitUninterruptibly();
768 <                    sync.release(1);
769 <                }
824 <            });
764 >        Thread t = new Thread(new CheckedRunnable() {
765 >            public void realRun() {
766 >                sync.acquire(1);
767 >                c.awaitUninterruptibly();
768 >                sync.release(1);
769 >            }});
770  
771          t.start();
772          Thread.sleep(SHORT_DELAY_MS);
# Line 837 | Line 782 | public class AbstractQueuedSynchronizerT
782       * await is interruptible
783       */
784      public void testAwait_Interrupt() throws InterruptedException {
785 <        final Mutex sync = new Mutex();
785 >        final Mutex sync = new Mutex();
786          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
787 <        Thread t = new Thread(new Runnable() {
788 <                public void run() {
789 <                    try {
790 <                        sync.acquire(1);
791 <                        c.await();
847 <                        sync.release(1);
848 <                        threadShouldThrow();
849 <                    }
850 <                    catch (InterruptedException success) {
851 <                    }
852 <                }
853 <            });
787 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
788 >            public void realRun() throws InterruptedException {
789 >                sync.acquire(1);
790 >                c.await();
791 >            }});
792  
793          t.start();
794          Thread.sleep(SHORT_DELAY_MS);
# Line 863 | Line 801 | public class AbstractQueuedSynchronizerT
801       * awaitNanos is interruptible
802       */
803      public void testAwaitNanos_Interrupt() throws InterruptedException {
804 <        final Mutex sync = new Mutex();
804 >        final Mutex sync = new Mutex();
805          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
806 <        Thread t = new Thread(new Runnable() {
807 <                public void run() {
808 <                    try {
809 <                        sync.acquire(1);
810 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
873 <                        sync.release(1);
874 <                        threadShouldThrow();
875 <                    }
876 <                    catch (InterruptedException success) {
877 <                    }
878 <                }
879 <            });
806 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
807 >            public void realRun() throws InterruptedException {
808 >                sync.acquire(1);
809 >                c.awaitNanos(LONG_DELAY_MS * 1000L * 1000L);
810 >            }});
811  
812          t.start();
813          Thread.sleep(SHORT_DELAY_MS);
# Line 889 | Line 820 | public class AbstractQueuedSynchronizerT
820       * awaitUntil is interruptible
821       */
822      public void testAwaitUntil_Interrupt() throws InterruptedException {
823 <        final Mutex sync = new Mutex();
823 >        final Mutex sync = new Mutex();
824          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
825 <        Thread t = new Thread(new Runnable() {
826 <                public void run() {
827 <                    try {
828 <                        sync.acquire(1);
829 <                        java.util.Date d = new java.util.Date();
830 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
900 <                        sync.release(1);
901 <                        threadShouldThrow();
902 <                    }
903 <                    catch (InterruptedException success) {
904 <                    }
905 <                }
906 <            });
825 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
826 >            public void realRun() throws InterruptedException {
827 >                sync.acquire(1);
828 >                java.util.Date d = new java.util.Date();
829 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
830 >            }});
831  
832          t.start();
833          Thread.sleep(SHORT_DELAY_MS);
# Line 916 | Line 840 | public class AbstractQueuedSynchronizerT
840       * signalAll wakes up all threads
841       */
842      public void testSignalAll() throws InterruptedException {
843 <        final Mutex sync = new Mutex();
843 >        final Mutex sync = new Mutex();
844          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
845 <        Thread t1 = new Thread(new Runnable() {
846 <                public void run() {
847 <                    try {
848 <                        sync.acquire(1);
849 <                        c.await();
850 <                        sync.release(1);
851 <                    }
852 <                    catch (InterruptedException e) {
853 <                        threadUnexpectedException();
854 <                    }
855 <                }
856 <            });
857 <
934 <        Thread t2 = new Thread(new Runnable() {
935 <                public void run() {
936 <                    try {
937 <                        sync.acquire(1);
938 <                        c.await();
939 <                        sync.release(1);
940 <                    }
941 <                    catch (InterruptedException e) {
942 <                        threadUnexpectedException();
943 <                    }
944 <                }
945 <            });
845 >        Thread t1 = new Thread(new CheckedRunnable() {
846 >            public void realRun() throws InterruptedException {
847 >                sync.acquire(1);
848 >                c.await();
849 >                sync.release(1);
850 >            }});
851 >
852 >        Thread t2 = new Thread(new CheckedRunnable() {
853 >            public void realRun() throws InterruptedException {
854 >                sync.acquire(1);
855 >                c.await();
856 >                sync.release(1);
857 >            }});
858  
859          t1.start();
860          t2.start();
# Line 993 | Line 905 | public class AbstractQueuedSynchronizerT
905       * tryReleaseShared setting state changes getState
906       */
907      public void testGetStateWithReleaseShared() {
908 <        final BooleanLatch l = new BooleanLatch();
909 <        assertFalse(l.isSignalled());
910 <        l.releaseShared(0);
911 <        assertTrue(l.isSignalled());
908 >        final BooleanLatch l = new BooleanLatch();
909 >        assertFalse(l.isSignalled());
910 >        l.releaseShared(0);
911 >        assertTrue(l.isSignalled());
912      }
913  
914      /**
915       * releaseShared has no effect when already signalled
916       */
917      public void testReleaseShared() {
918 <        final BooleanLatch l = new BooleanLatch();
919 <        assertFalse(l.isSignalled());
920 <        l.releaseShared(0);
921 <        assertTrue(l.isSignalled());
922 <        l.releaseShared(0);
923 <        assertTrue(l.isSignalled());
918 >        final BooleanLatch l = new BooleanLatch();
919 >        assertFalse(l.isSignalled());
920 >        l.releaseShared(0);
921 >        assertTrue(l.isSignalled());
922 >        l.releaseShared(0);
923 >        assertTrue(l.isSignalled());
924      }
925  
926      /**
927       * acquireSharedInterruptibly returns after release, but not before
928       */
929      public void testAcquireSharedInterruptibly() throws InterruptedException {
930 <        final BooleanLatch l = new BooleanLatch();
930 >        final BooleanLatch l = new BooleanLatch();
931  
932 <        Thread t = new Thread(new Runnable() {
933 <                public void run() {
934 <                    try {
935 <                        threadAssertFalse(l.isSignalled());
936 <                        l.acquireSharedInterruptibly(0);
937 <                        threadAssertTrue(l.isSignalled());
1026 <                    } catch (InterruptedException e) {
1027 <                        threadUnexpectedException();
1028 <                    }
1029 <                }
1030 <            });
932 >        Thread t = new Thread(new CheckedRunnable() {
933 >            public void realRun() throws InterruptedException {
934 >                threadAssertFalse(l.isSignalled());
935 >                l.acquireSharedInterruptibly(0);
936 >                threadAssertTrue(l.isSignalled());
937 >            }});
938  
939          t.start();
940          assertFalse(l.isSignalled());
# Line 1042 | Line 949 | public class AbstractQueuedSynchronizerT
949       * acquireSharedTimed returns after release
950       */
951      public void testAsquireSharedTimed() throws InterruptedException {
952 <        final BooleanLatch l = new BooleanLatch();
952 >        final BooleanLatch l = new BooleanLatch();
953  
954 <        Thread t = new Thread(new Runnable() {
955 <                public void run() {
956 <                    try {
957 <                        threadAssertFalse(l.isSignalled());
958 <                        threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
959 <                        threadAssertTrue(l.isSignalled());
1053 <
1054 <                    } catch (InterruptedException e) {
1055 <                        threadUnexpectedException();
1056 <                    }
1057 <                }
1058 <            });
954 >        Thread t = new Thread(new CheckedRunnable() {
955 >            public void realRun() throws InterruptedException {
956 >                threadAssertFalse(l.isSignalled());
957 >                threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS * 1000L * 1000L));
958 >                threadAssertTrue(l.isSignalled());
959 >            }});
960  
961          t.start();
962          assertFalse(l.isSignalled());
# Line 1070 | Line 971 | public class AbstractQueuedSynchronizerT
971       */
972      public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
973          final BooleanLatch l = new BooleanLatch();
974 <        Thread t = new Thread(new Runnable() {
975 <                public void run() {
976 <                    try {
977 <                        threadAssertFalse(l.isSignalled());
978 <                        l.acquireSharedInterruptibly(0);
979 <                        threadShouldThrow();
980 <                    } catch (InterruptedException success) {}
1080 <                }
1081 <            });
1082 <        t.start();
974 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
975 >            public void realRun() throws InterruptedException {
976 >                threadAssertFalse(l.isSignalled());
977 >                l.acquireSharedInterruptibly(0);
978 >            }});
979 >
980 >        t.start();
981          assertFalse(l.isSignalled());
982          t.interrupt();
983          t.join();
# Line 1090 | Line 988 | public class AbstractQueuedSynchronizerT
988       */
989      public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
990          final BooleanLatch l = new BooleanLatch();
991 <        Thread t = new Thread(new Runnable() {
992 <                public void run() {
993 <                    try {
994 <                        threadAssertFalse(l.isSignalled());
995 <                        l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
996 <                        threadShouldThrow();
1099 <                    } catch (InterruptedException success) {}
1100 <                }
1101 <            });
991 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
992 >            public void realRun() throws InterruptedException {
993 >                threadAssertFalse(l.isSignalled());
994 >                l.tryAcquireSharedNanos(0, SMALL_DELAY_MS * 1000L * 1000L);
995 >            }});
996 >
997          t.start();
998          Thread.sleep(SHORT_DELAY_MS);
999          assertFalse(l.isSignalled());
# Line 1111 | Line 1006 | public class AbstractQueuedSynchronizerT
1006       */
1007      public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1008          final BooleanLatch l = new BooleanLatch();
1009 <        Thread t = new Thread(new Runnable() {
1010 <                public void run() {
1011 <                    try {
1012 <                        threadAssertFalse(l.isSignalled());
1013 <                        threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1014 <                    } catch (InterruptedException ie) {
1120 <                        threadUnexpectedException();
1121 <                    }
1122 <                }
1123 <            });
1009 >        Thread t = new Thread(new CheckedRunnable() {
1010 >            public void realRun() throws InterruptedException {
1011 >                threadAssertFalse(l.isSignalled());
1012 >                threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS * 1000L * 1000L));
1013 >            }});
1014 >
1015          t.start();
1016          Thread.sleep(SHORT_DELAY_MS);
1017          assertFalse(l.isSignalled());
1018          t.join();
1019      }
1020  
1130
1021   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines