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.27 by jsr166, Wed Nov 18 08:26:24 2009 UTC vs.
Revision 1.29 by jsr166, Sat Nov 21 02:07:26 2009 UTC

# Line 65 | Line 65 | public class AbstractQueuedSynchronizerT
65      }
66  
67      /**
68 <     * A runnable calling acquireInterruptibly
68 >     * A runnable calling acquireInterruptibly that does not expect to
69 >     * be interrupted.
70       */
71 <    class InterruptibleSyncRunnable implements Runnable {
71 >    class InterruptibleSyncRunnable extends CheckedRunnable {
72          final Mutex sync;
73          InterruptibleSyncRunnable(Mutex l) { sync = l; }
74 <        public void run() {
75 <            try {
75 <                sync.acquireInterruptibly(1);
76 <            } catch (InterruptedException success) {}
74 >        public void realRun() throws InterruptedException {
75 >            sync.acquireInterruptibly(1);
76          }
77      }
78  
79  
80      /**
81       * A runnable calling acquireInterruptibly that expects to be
82 <     * interrupted
82 >     * interrupted.
83       */
84 <    class InterruptedSyncRunnable implements Runnable {
84 >    class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
85          final Mutex sync;
86          InterruptedSyncRunnable(Mutex l) { sync = l; }
87 <        public void run() {
88 <            try {
90 <                sync.acquireInterruptibly(1);
91 <                threadShouldThrow();
92 <            } catch (InterruptedException success) {}
87 >        public void realRun() throws InterruptedException {
88 >            sync.acquireInterruptibly(1);
89          }
90      }
91  
# Line 97 | Line 93 | public class AbstractQueuedSynchronizerT
93       * isHeldExclusively is false upon construction
94       */
95      public void testIsHeldExclusively() {
96 <        Mutex rl = new Mutex();
96 >        Mutex rl = new Mutex();
97          assertFalse(rl.isHeldExclusively());
98      }
99  
# Line 105 | Line 101 | public class AbstractQueuedSynchronizerT
101       * acquiring released sync succeeds
102       */
103      public void testAcquire() {
104 <        Mutex rl = new Mutex();
104 >        Mutex rl = new Mutex();
105          rl.acquire(1);
106          assertTrue(rl.isHeldExclusively());
107          rl.release(1);
# Line 116 | Line 112 | public class AbstractQueuedSynchronizerT
112       * tryAcquire on an released sync succeeds
113       */
114      public void testTryAcquire() {
115 <        Mutex rl = new Mutex();
115 >        Mutex rl = new Mutex();
116          assertTrue(rl.tryAcquire(1));
117          assertTrue(rl.isHeldExclusively());
118          rl.release(1);
# Line 126 | Line 122 | public class AbstractQueuedSynchronizerT
122       * hasQueuedThreads reports whether there are waiting threads
123       */
124      public void testhasQueuedThreads() throws InterruptedException {
125 <        final Mutex sync = new Mutex();
125 >        final Mutex sync = new Mutex();
126          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
127          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
128          assertFalse(sync.hasQueuedThreads());
# Line 151 | Line 147 | public class AbstractQueuedSynchronizerT
147       * isQueued(null) throws NPE
148       */
149      public void testIsQueuedNPE() {
150 <        final Mutex sync = new Mutex();
150 >        final Mutex sync = new Mutex();
151          try {
152              sync.isQueued(null);
153              shouldThrow();
# Line 162 | Line 158 | public class AbstractQueuedSynchronizerT
158       * isQueued reports whether a thread is queued.
159       */
160      public void testIsQueued() throws InterruptedException {
161 <        final Mutex sync = new Mutex();
161 >        final Mutex sync = new Mutex();
162          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
163          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
164          assertFalse(sync.isQueued(t1));
# Line 192 | Line 188 | public class AbstractQueuedSynchronizerT
188       * getFirstQueuedThread returns first waiting thread or null if none
189       */
190      public void testGetFirstQueuedThread() throws InterruptedException {
191 <        final Mutex sync = new Mutex();
191 >        final Mutex sync = new Mutex();
192          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
193          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
194          assertNull(sync.getFirstQueuedThread());
# Line 219 | Line 215 | public class AbstractQueuedSynchronizerT
215       * hasContended reports false if no thread has ever blocked, else true
216       */
217      public void testHasContended() throws InterruptedException {
218 <        final Mutex sync = new Mutex();
218 >        final Mutex sync = new Mutex();
219          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
220          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
221          assertFalse(sync.hasContended());
# Line 244 | Line 240 | public class AbstractQueuedSynchronizerT
240       * getQueuedThreads includes waiting threads
241       */
242      public void testGetQueuedThreads() throws InterruptedException {
243 <        final Mutex sync = new Mutex();
243 >        final Mutex sync = new Mutex();
244          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
245          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
246          assertTrue(sync.getQueuedThreads().isEmpty());
# Line 272 | Line 268 | public class AbstractQueuedSynchronizerT
268       * getExclusiveQueuedThreads includes waiting threads
269       */
270      public void testGetExclusiveQueuedThreads() throws InterruptedException {
271 <        final Mutex sync = new Mutex();
271 >        final Mutex sync = new Mutex();
272          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
273          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
274          assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
# Line 300 | Line 296 | public class AbstractQueuedSynchronizerT
296       * getSharedQueuedThreads does not include exclusively waiting threads
297       */
298      public void testGetSharedQueuedThreads() throws InterruptedException {
299 <        final Mutex sync = new Mutex();
299 >        final Mutex sync = new Mutex();
300          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
301          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
302          assertTrue(sync.getSharedQueuedThreads().isEmpty());
# Line 326 | Line 322 | public class AbstractQueuedSynchronizerT
322       * tryAcquireNanos is interruptible.
323       */
324      public void testInterruptedException2() throws InterruptedException {
325 <        final Mutex sync = new Mutex();
326 <        sync.acquire(1);
327 <        Thread t = new Thread(new Runnable() {
328 <                public void run() {
329 <                    try {
330 <                        sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
335 <                        threadShouldThrow();
336 <                    } catch (InterruptedException success) {}
337 <                }
338 <            });
325 >        final Mutex sync = new Mutex();
326 >        sync.acquire(1);
327 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
328 >            public void realRun() throws InterruptedException {
329 >                sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
330 >            }});
331  
332          t.start();
333          t.interrupt();
# Line 347 | Line 339 | public class AbstractQueuedSynchronizerT
339       * TryAcquire on exclusively held sync fails
340       */
341      public void testTryAcquireWhenSynced() throws InterruptedException {
342 <        final Mutex sync = new Mutex();
343 <        sync.acquire(1);
344 <        Thread t = new Thread(new Runnable() {
345 <                public void run() {
346 <                    threadAssertFalse(sync.tryAcquire(1));
347 <                }
356 <            });
342 >        final Mutex sync = new Mutex();
343 >        sync.acquire(1);
344 >        Thread t = new Thread(new CheckedRunnable() {
345 >            public void realRun() {
346 >                threadAssertFalse(sync.tryAcquire(1));
347 >            }});
348  
349          t.start();
350          t.join();
# Line 364 | Line 355 | public class AbstractQueuedSynchronizerT
355       * tryAcquireNanos on an exclusively held sync times out
356       */
357      public void testAcquireNanos_Timeout() throws InterruptedException {
358 <        final Mutex sync = new Mutex();
359 <        sync.acquire(1);
360 <        Thread t = new Thread(new Runnable() {
361 <                public void run() {
362 <                    try {
363 <                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
373 <                    } catch (Exception ex) {
374 <                        threadUnexpectedException();
375 <                    }
376 <                }
377 <            });
358 >        final Mutex sync = new Mutex();
359 >        sync.acquire(1);
360 >        Thread t = new Thread(new CheckedRunnable() {
361 >            public void realRun() throws InterruptedException {
362 >                threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
363 >            }});
364  
365          t.start();
366          t.join();
# Line 386 | Line 372 | public class AbstractQueuedSynchronizerT
372       * getState is true when acquired and false when not
373       */
374      public void testGetState() throws InterruptedException {
375 <        final Mutex sync = new Mutex();
376 <        sync.acquire(1);
377 <        assertTrue(sync.isHeldExclusively());
378 <        sync.release(1);
379 <        assertFalse(sync.isHeldExclusively());
380 <        Thread t = new Thread(new Runnable() {
381 <                public void run() {
382 <                    sync.acquire(1);
383 <                    try {
384 <                        Thread.sleep(SMALL_DELAY_MS);
385 <                    }
400 <                    catch (Exception e) {
401 <                        threadUnexpectedException();
402 <                    }
403 <                    sync.release(1);
404 <                }
405 <            });
375 >        final Mutex sync = new Mutex();
376 >        sync.acquire(1);
377 >        assertTrue(sync.isHeldExclusively());
378 >        sync.release(1);
379 >        assertFalse(sync.isHeldExclusively());
380 >        Thread t = new Thread(new CheckedRunnable() {
381 >            public void realRun() throws InterruptedException {
382 >                sync.acquire(1);
383 >                Thread.sleep(SMALL_DELAY_MS);
384 >                sync.release(1);
385 >            }});
386  
387          t.start();
388          Thread.sleep(SHORT_DELAY_MS);
# Line 416 | Line 396 | public class AbstractQueuedSynchronizerT
396       * acquireInterruptibly is interruptible.
397       */
398      public void testAcquireInterruptibly1() throws InterruptedException {
399 <        final Mutex sync = new Mutex();
400 <        sync.acquire(1);
401 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
399 >        final Mutex sync = new Mutex();
400 >        sync.acquire(1);
401 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
402  
403          t.start();
404          Thread.sleep(SHORT_DELAY_MS);
# Line 432 | Line 412 | public class AbstractQueuedSynchronizerT
412       * acquireInterruptibly succeeds when released, else is interruptible
413       */
414      public void testAcquireInterruptibly2() throws InterruptedException {
415 <        final Mutex sync = new Mutex();
415 >        final Mutex sync = new Mutex();
416          sync.acquireInterruptibly(1);
417 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
417 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
418          t.start();
419          t.interrupt();
420          assertTrue(sync.isHeldExclusively());
# Line 445 | Line 425 | public class AbstractQueuedSynchronizerT
425       * owns is true for a condition created by sync else false
426       */
427      public void testOwns() {
428 <        final Mutex sync = new Mutex();
428 >        final Mutex sync = new Mutex();
429          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
430          final Mutex sync2 = new Mutex();
431          assertTrue(sync.owns(c));
# Line 456 | Line 436 | public class AbstractQueuedSynchronizerT
436       * Calling await without holding sync throws IllegalMonitorStateException
437       */
438      public void testAwait_IllegalMonitor() throws InterruptedException {
439 <        final Mutex sync = new Mutex();
439 >        final Mutex sync = new Mutex();
440          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
441          try {
442              c.await();
443              shouldThrow();
444 <        }
465 <        catch (IllegalMonitorStateException success) {}
444 >        } catch (IllegalMonitorStateException success) {}
445      }
446  
447      /**
448       * Calling signal without holding sync throws IllegalMonitorStateException
449       */
450      public void testSignal_IllegalMonitor() {
451 <        final Mutex sync = new Mutex();
451 >        final Mutex sync = new Mutex();
452          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
453          try {
454              c.signal();
455              shouldThrow();
456 <        }
478 <        catch (IllegalMonitorStateException success) {}
456 >        } catch (IllegalMonitorStateException success) {}
457      }
458  
459      /**
460       * awaitNanos without a signal times out
461       */
462      public void testAwaitNanos_Timeout() throws InterruptedException {
463 <        final Mutex sync = new Mutex();
463 >        final Mutex sync = new Mutex();
464          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
465          sync.acquire(1);
466          long t = c.awaitNanos(100);
# Line 494 | Line 472 | public class AbstractQueuedSynchronizerT
472       *  Timed await without a signal times out
473       */
474      public void testAwait_Timeout() throws InterruptedException {
475 <        final Mutex sync = new Mutex();
475 >        final Mutex sync = new Mutex();
476          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
477          sync.acquire(1);
478          assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 505 | Line 483 | public class AbstractQueuedSynchronizerT
483       * awaitUntil without a signal times out
484       */
485      public void testAwaitUntil_Timeout() throws InterruptedException {
486 <        final Mutex sync = new Mutex();
486 >        final Mutex sync = new Mutex();
487          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
488          sync.acquire(1);
489          java.util.Date d = new java.util.Date();
# Line 517 | Line 495 | public class AbstractQueuedSynchronizerT
495       * await returns when signalled
496       */
497      public void testAwait() throws InterruptedException {
498 <        final Mutex sync = new Mutex();
498 >        final Mutex sync = new Mutex();
499          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
500 <        Thread t = new Thread(new Runnable() {
501 <                public void run() {
502 <                    try {
503 <                        sync.acquire(1);
504 <                        c.await();
505 <                        sync.release(1);
528 <                    }
529 <                    catch (InterruptedException e) {
530 <                        threadUnexpectedException();
531 <                    }
532 <                }
533 <            });
500 >        Thread t = new Thread(new CheckedRunnable() {
501 >            public void realRun() throws InterruptedException {
502 >                sync.acquire(1);
503 >                c.await();
504 >                sync.release(1);
505 >            }});
506  
507          t.start();
508          Thread.sleep(SHORT_DELAY_MS);
# Line 547 | Line 519 | public class AbstractQueuedSynchronizerT
519       * hasWaiters throws NPE if null
520       */
521      public void testHasWaitersNPE() {
522 <        final Mutex sync = new Mutex();
522 >        final Mutex sync = new Mutex();
523          try {
524              sync.hasWaiters(null);
525              shouldThrow();
# Line 558 | Line 530 | public class AbstractQueuedSynchronizerT
530       * getWaitQueueLength throws NPE if null
531       */
532      public void testGetWaitQueueLengthNPE() {
533 <        final Mutex sync = new Mutex();
533 >        final Mutex sync = new Mutex();
534          try {
535              sync.getWaitQueueLength(null);
536              shouldThrow();
# Line 570 | Line 542 | public class AbstractQueuedSynchronizerT
542       * getWaitingThreads throws NPE if null
543       */
544      public void testGetWaitingThreadsNPE() {
545 <        final Mutex sync = new Mutex();
545 >        final Mutex sync = new Mutex();
546          try {
547              sync.getWaitingThreads(null);
548              shouldThrow();
# Line 582 | Line 554 | public class AbstractQueuedSynchronizerT
554       * hasWaiters throws IAE if not owned
555       */
556      public void testHasWaitersIAE() {
557 <        final Mutex sync = new Mutex();
557 >        final Mutex sync = new Mutex();
558          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
559 <        final Mutex sync2 = new Mutex();
559 >        final Mutex sync2 = new Mutex();
560          try {
561              sync2.hasWaiters(c);
562              shouldThrow();
# Line 595 | Line 567 | public class AbstractQueuedSynchronizerT
567       * hasWaiters throws IMSE if not synced
568       */
569      public void testHasWaitersIMSE() {
570 <        final Mutex sync = new Mutex();
570 >        final Mutex sync = new Mutex();
571          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
572          try {
573              sync.hasWaiters(c);
# Line 608 | Line 580 | public class AbstractQueuedSynchronizerT
580       * getWaitQueueLength throws IAE if not owned
581       */
582      public void testGetWaitQueueLengthIAE() {
583 <        final Mutex sync = new Mutex();
583 >        final Mutex sync = new Mutex();
584          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
585 <        final Mutex sync2 = new Mutex();
585 >        final Mutex sync2 = new Mutex();
586          try {
587              sync2.getWaitQueueLength(c);
588              shouldThrow();
# Line 621 | Line 593 | public class AbstractQueuedSynchronizerT
593       * getWaitQueueLength throws IMSE if not synced
594       */
595      public void testGetWaitQueueLengthIMSE() {
596 <        final Mutex sync = new Mutex();
596 >        final Mutex sync = new Mutex();
597          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
598          try {
599              sync.getWaitQueueLength(c);
# Line 634 | Line 606 | public class AbstractQueuedSynchronizerT
606       * getWaitingThreads throws IAE if not owned
607       */
608      public void testGetWaitingThreadsIAE() {
609 <        final Mutex sync = new Mutex();
609 >        final Mutex sync = new Mutex();
610          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
611 <        final Mutex sync2 = new Mutex();
611 >        final Mutex sync2 = new Mutex();
612          try {
613              sync2.getWaitingThreads(c);
614              shouldThrow();
# Line 647 | Line 619 | public class AbstractQueuedSynchronizerT
619       * getWaitingThreads throws IMSE if not synced
620       */
621      public void testGetWaitingThreadsIMSE() {
622 <        final Mutex sync = new Mutex();
622 >        final Mutex sync = new Mutex();
623          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
624          try {
625              sync.getWaitingThreads(c);
# Line 661 | Line 633 | public class AbstractQueuedSynchronizerT
633       * hasWaiters returns true when a thread is waiting, else false
634       */
635      public void testHasWaiters() throws InterruptedException {
636 <        final Mutex sync = new Mutex();
636 >        final Mutex sync = new Mutex();
637          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
638 <        Thread t = new Thread(new Runnable() {
639 <                public void run() {
640 <                    try {
641 <                        sync.acquire(1);
642 <                        threadAssertFalse(sync.hasWaiters(c));
643 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
644 <                        c.await();
645 <                        sync.release(1);
674 <                    }
675 <                    catch (InterruptedException e) {
676 <                        threadUnexpectedException();
677 <                    }
678 <                }
679 <            });
638 >        Thread t = new Thread(new CheckedRunnable() {
639 >            public void realRun() throws InterruptedException {
640 >                sync.acquire(1);
641 >                threadAssertFalse(sync.hasWaiters(c));
642 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
643 >                c.await();
644 >                sync.release(1);
645 >            }});
646  
647          t.start();
648          Thread.sleep(SHORT_DELAY_MS);
# Line 698 | Line 664 | public class AbstractQueuedSynchronizerT
664       * getWaitQueueLength returns number of waiting threads
665       */
666      public void testGetWaitQueueLength() throws InterruptedException {
667 <        final Mutex sync = new Mutex();
667 >        final Mutex sync = new Mutex();
668          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
669 <        Thread t1 = new Thread(new Runnable() {
670 <                public void run() {
671 <                    try {
672 <                        sync.acquire(1);
673 <                        threadAssertFalse(sync.hasWaiters(c));
674 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
675 <                        c.await();
676 <                        sync.release(1);
677 <                    }
678 <                    catch (InterruptedException e) {
679 <                        threadUnexpectedException();
680 <                    }
681 <                }
682 <            });
683 <
684 <        Thread t2 = new Thread(new Runnable() {
685 <                public void run() {
720 <                    try {
721 <                        sync.acquire(1);
722 <                        threadAssertTrue(sync.hasWaiters(c));
723 <                        threadAssertEquals(1, sync.getWaitQueueLength(c));
724 <                        c.await();
725 <                        sync.release(1);
726 <                    }
727 <                    catch (InterruptedException e) {
728 <                        threadUnexpectedException();
729 <                    }
730 <                }
731 <            });
669 >        Thread t1 = new Thread(new CheckedRunnable() {
670 >            public void realRun() throws InterruptedException {
671 >                sync.acquire(1);
672 >                threadAssertFalse(sync.hasWaiters(c));
673 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
674 >                c.await();
675 >                sync.release(1);
676 >            }});
677 >
678 >        Thread t2 = new Thread(new CheckedRunnable() {
679 >            public void realRun() throws InterruptedException {
680 >                sync.acquire(1);
681 >                threadAssertTrue(sync.hasWaiters(c));
682 >                threadAssertEquals(1, sync.getWaitQueueLength(c));
683 >                c.await();
684 >                sync.release(1);
685 >            }});
686  
687          t1.start();
688          Thread.sleep(SHORT_DELAY_MS);
# Line 754 | Line 708 | public class AbstractQueuedSynchronizerT
708       * getWaitingThreads returns only and all waiting threads
709       */
710      public void testGetWaitingThreads() throws InterruptedException {
711 <        final Mutex sync = new Mutex();
711 >        final Mutex sync = new Mutex();
712          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
713 <        Thread t1 = new Thread(new Runnable() {
714 <                public void run() {
715 <                    try {
716 <                        sync.acquire(1);
717 <                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
718 <                        c.await();
719 <                        sync.release(1);
720 <                    }
721 <                    catch (InterruptedException e) {
722 <                        threadUnexpectedException();
723 <                    }
724 <                }
725 <            });
726 <
727 <        Thread t2 = new Thread(new Runnable() {
774 <                public void run() {
775 <                    try {
776 <                        sync.acquire(1);
777 <                        threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
778 <                        c.await();
779 <                        sync.release(1);
780 <                    }
781 <                    catch (InterruptedException e) {
782 <                        threadUnexpectedException();
783 <                    }
784 <                }
785 <            });
713 >        Thread t1 = new Thread(new CheckedRunnable() {
714 >            public void realRun() throws InterruptedException {
715 >                sync.acquire(1);
716 >                threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
717 >                c.await();
718 >                sync.release(1);
719 >            }});
720 >
721 >        Thread t2 = new Thread(new CheckedRunnable() {
722 >            public void realRun() throws InterruptedException {
723 >                sync.acquire(1);
724 >                threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
725 >                c.await();
726 >                sync.release(1);
727 >            }});
728  
729          sync.acquire(1);
730          assertTrue(sync.getWaitingThreads(c).isEmpty());
# Line 814 | Line 756 | public class AbstractQueuedSynchronizerT
756       * awaitUninterruptibly doesn't abort on interrupt
757       */
758      public void testAwaitUninterruptibly() throws InterruptedException {
759 <        final Mutex sync = new Mutex();
759 >        final Mutex sync = new Mutex();
760          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
761 <        Thread t = new Thread(new Runnable() {
762 <                public void run() {
763 <                    sync.acquire(1);
764 <                    c.awaitUninterruptibly();
765 <                    sync.release(1);
766 <                }
825 <            });
761 >        Thread t = new Thread(new CheckedRunnable() {
762 >            public void realRun() {
763 >                sync.acquire(1);
764 >                c.awaitUninterruptibly();
765 >                sync.release(1);
766 >            }});
767  
768          t.start();
769          Thread.sleep(SHORT_DELAY_MS);
# Line 838 | Line 779 | public class AbstractQueuedSynchronizerT
779       * await is interruptible
780       */
781      public void testAwait_Interrupt() throws InterruptedException {
782 <        final Mutex sync = new Mutex();
782 >        final Mutex sync = new Mutex();
783          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
784 <        Thread t = new Thread(new Runnable() {
785 <                public void run() {
786 <                    try {
787 <                        sync.acquire(1);
788 <                        c.await();
789 <                        sync.release(1);
849 <                        threadShouldThrow();
850 <                    }
851 <                    catch (InterruptedException success) {
852 <                    }
853 <                }
854 <            });
784 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
785 >            public void realRun() throws InterruptedException {
786 >                sync.acquire(1);
787 >                c.await();
788 >                sync.release(1);
789 >            }});
790  
791          t.start();
792          Thread.sleep(SHORT_DELAY_MS);
# Line 864 | Line 799 | public class AbstractQueuedSynchronizerT
799       * awaitNanos is interruptible
800       */
801      public void testAwaitNanos_Interrupt() throws InterruptedException {
802 <        final Mutex sync = new Mutex();
802 >        final Mutex sync = new Mutex();
803          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
804 <        Thread t = new Thread(new Runnable() {
805 <                public void run() {
806 <                    try {
807 <                        sync.acquire(1);
808 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
809 <                        sync.release(1);
875 <                        threadShouldThrow();
876 <                    }
877 <                    catch (InterruptedException success) {
878 <                    }
879 <                }
880 <            });
804 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
805 >            public void realRun() throws InterruptedException {
806 >                sync.acquire(1);
807 >                c.awaitNanos(1000 * 1000 * 1000); // 1 sec
808 >                sync.release(1);
809 >            }});
810  
811          t.start();
812          Thread.sleep(SHORT_DELAY_MS);
# Line 890 | Line 819 | public class AbstractQueuedSynchronizerT
819       * awaitUntil is interruptible
820       */
821      public void testAwaitUntil_Interrupt() throws InterruptedException {
822 <        final Mutex sync = new Mutex();
822 >        final Mutex sync = new Mutex();
823          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
824 <        Thread t = new Thread(new Runnable() {
825 <                public void run() {
826 <                    try {
827 <                        sync.acquire(1);
828 <                        java.util.Date d = new java.util.Date();
829 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
830 <                        sync.release(1);
902 <                        threadShouldThrow();
903 <                    }
904 <                    catch (InterruptedException success) {
905 <                    }
906 <                }
907 <            });
824 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
825 >            public void realRun() throws InterruptedException {
826 >                sync.acquire(1);
827 >                java.util.Date d = new java.util.Date();
828 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
829 >                sync.release(1);
830 >            }});
831  
832          t.start();
833          Thread.sleep(SHORT_DELAY_MS);
# Line 917 | 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 <
935 <        Thread t2 = new Thread(new Runnable() {
936 <                public void run() {
937 <                    try {
938 <                        sync.acquire(1);
939 <                        c.await();
940 <                        sync.release(1);
941 <                    }
942 <                    catch (InterruptedException e) {
943 <                        threadUnexpectedException();
944 <                    }
945 <                }
946 <            });
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 994 | 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());
1027 <                    } catch (InterruptedException e) {
1028 <                        threadUnexpectedException();
1029 <                    }
1030 <                }
1031 <            });
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 1043 | 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());
1054 <
1055 <                    } catch (InterruptedException e) {
1056 <                        threadUnexpectedException();
1057 <                    }
1058 <                }
1059 <            });
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* 1000 * 1000));
958 >                threadAssertTrue(l.isSignalled());
959 >            }});
960  
961          t.start();
962          assertFalse(l.isSignalled());
# Line 1071 | 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) {}
1081 <                }
1082 <            });
1083 <        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 1091 | 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();
1100 <                    } catch (InterruptedException success) {}
1101 <                }
1102 <            });
991 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
992 >            public void realRun() throws InterruptedException {
993 >                threadAssertFalse(l.isSignalled());
994 >                l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
995 >            }});
996 >
997          t.start();
998          Thread.sleep(SHORT_DELAY_MS);
999          assertFalse(l.isSignalled());
# Line 1112 | 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) {
1121 <                        threadUnexpectedException();
1122 <                    }
1123 <                }
1124 <            });
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* 1000 * 1000));
1013 >            }});
1014 >
1015          t.start();
1016          Thread.sleep(SHORT_DELAY_MS);
1017          assertFalse(l.isSignalled());
1018          t.join();
1019      }
1020  
1131
1021   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines