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.31 by jsr166, Sat Nov 21 17:54:04 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 * 1000 * 1000);
331 >            }});
332  
333          t.start();
334          t.interrupt();
335 +        t.join();
336      }
337  
338  
# Line 346 | Line 340 | public class AbstractQueuedSynchronizerT
340       * TryAcquire on exclusively held sync fails
341       */
342      public void testTryAcquireWhenSynced() throws InterruptedException {
343 <        final Mutex sync = new Mutex();
344 <        sync.acquire(1);
345 <        Thread t = new Thread(new Runnable() {
346 <                public void run() {
347 <                    threadAssertFalse(sync.tryAcquire(1));
348 <                }
355 <            });
343 >        final Mutex sync = new Mutex();
344 >        sync.acquire(1);
345 >        Thread t = new Thread(new CheckedRunnable() {
346 >            public void realRun() {
347 >                threadAssertFalse(sync.tryAcquire(1));
348 >            }});
349  
350          t.start();
351          t.join();
# Line 363 | Line 356 | public class AbstractQueuedSynchronizerT
356       * tryAcquireNanos on an exclusively held sync times out
357       */
358      public void testAcquireNanos_Timeout() throws InterruptedException {
359 <        final Mutex sync = new Mutex();
360 <        sync.acquire(1);
361 <        Thread t = new Thread(new Runnable() {
362 <                public void run() {
363 <                    try {
364 <                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
372 <                    } catch (Exception ex) {
373 <                        threadUnexpectedException();
374 <                    }
375 <                }
376 <            });
359 >        final Mutex sync = new Mutex();
360 >        sync.acquire(1);
361 >        Thread t = new Thread(new CheckedRunnable() {
362 >            public void realRun() throws InterruptedException {
363 >                threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
364 >            }});
365  
366          t.start();
367          t.join();
# Line 385 | Line 373 | public class AbstractQueuedSynchronizerT
373       * getState is true when acquired and false when not
374       */
375      public void testGetState() throws InterruptedException {
376 <        final Mutex sync = new Mutex();
377 <        sync.acquire(1);
378 <        assertTrue(sync.isHeldExclusively());
379 <        sync.release(1);
380 <        assertFalse(sync.isHeldExclusively());
381 <        Thread t = new Thread(new Runnable() {
382 <                public void run() {
383 <                    sync.acquire(1);
384 <                    try {
385 <                        Thread.sleep(SMALL_DELAY_MS);
386 <                    }
399 <                    catch (Exception e) {
400 <                        threadUnexpectedException();
401 <                    }
402 <                    sync.release(1);
403 <                }
404 <            });
376 >        final Mutex sync = new Mutex();
377 >        sync.acquire(1);
378 >        assertTrue(sync.isHeldExclusively());
379 >        sync.release(1);
380 >        assertFalse(sync.isHeldExclusively());
381 >        Thread t = new Thread(new CheckedRunnable() {
382 >            public void realRun() throws InterruptedException {
383 >                sync.acquire(1);
384 >                Thread.sleep(SMALL_DELAY_MS);
385 >                sync.release(1);
386 >            }});
387  
388          t.start();
389          Thread.sleep(SHORT_DELAY_MS);
# Line 415 | Line 397 | public class AbstractQueuedSynchronizerT
397       * acquireInterruptibly is interruptible.
398       */
399      public void testAcquireInterruptibly1() throws InterruptedException {
400 <        final Mutex sync = new Mutex();
401 <        sync.acquire(1);
402 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
400 >        final Mutex sync = new Mutex();
401 >        sync.acquire(1);
402 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
403  
404          t.start();
405          Thread.sleep(SHORT_DELAY_MS);
# Line 431 | Line 413 | public class AbstractQueuedSynchronizerT
413       * acquireInterruptibly succeeds when released, else is interruptible
414       */
415      public void testAcquireInterruptibly2() throws InterruptedException {
416 <        final Mutex sync = new Mutex();
416 >        final Mutex sync = new Mutex();
417          sync.acquireInterruptibly(1);
418 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
418 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
419          t.start();
420          t.interrupt();
421          assertTrue(sync.isHeldExclusively());
# Line 444 | Line 426 | public class AbstractQueuedSynchronizerT
426       * owns is true for a condition created by sync else false
427       */
428      public void testOwns() {
429 <        final Mutex sync = new Mutex();
429 >        final Mutex sync = new Mutex();
430          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
431          final Mutex sync2 = new Mutex();
432          assertTrue(sync.owns(c));
# Line 455 | Line 437 | public class AbstractQueuedSynchronizerT
437       * Calling await without holding sync throws IllegalMonitorStateException
438       */
439      public void testAwait_IllegalMonitor() throws InterruptedException {
440 <        final Mutex sync = new Mutex();
440 >        final Mutex sync = new Mutex();
441          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
442          try {
443              c.await();
444              shouldThrow();
445 <        }
464 <        catch (IllegalMonitorStateException success) {}
445 >        } catch (IllegalMonitorStateException success) {}
446      }
447  
448      /**
449       * Calling signal without holding sync throws IllegalMonitorStateException
450       */
451      public void testSignal_IllegalMonitor() {
452 <        final Mutex sync = new Mutex();
452 >        final Mutex sync = new Mutex();
453          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
454          try {
455              c.signal();
456              shouldThrow();
457 <        }
477 <        catch (IllegalMonitorStateException success) {}
457 >        } catch (IllegalMonitorStateException success) {}
458      }
459  
460      /**
461       * awaitNanos without a signal times out
462       */
463      public void testAwaitNanos_Timeout() throws InterruptedException {
464 <        final Mutex sync = new Mutex();
464 >        final Mutex sync = new Mutex();
465          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
466          sync.acquire(1);
467          long t = c.awaitNanos(100);
# Line 493 | Line 473 | public class AbstractQueuedSynchronizerT
473       *  Timed await without a signal times out
474       */
475      public void testAwait_Timeout() throws InterruptedException {
476 <        final Mutex sync = new Mutex();
476 >        final Mutex sync = new Mutex();
477          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
478          sync.acquire(1);
479 <        assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
479 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
480          sync.release(1);
481      }
482  
# Line 504 | Line 484 | public class AbstractQueuedSynchronizerT
484       * awaitUntil without a signal times out
485       */
486      public void testAwaitUntil_Timeout() throws InterruptedException {
487 <        final Mutex sync = new Mutex();
487 >        final Mutex sync = new Mutex();
488          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
489          sync.acquire(1);
490          java.util.Date d = new java.util.Date();
# Line 516 | Line 496 | public class AbstractQueuedSynchronizerT
496       * await returns when signalled
497       */
498      public void testAwait() throws InterruptedException {
499 <        final Mutex sync = new Mutex();
499 >        final Mutex sync = new Mutex();
500          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
501 <        Thread t = new Thread(new Runnable() {
502 <                public void run() {
503 <                    try {
504 <                        sync.acquire(1);
505 <                        c.await();
506 <                        sync.release(1);
527 <                    }
528 <                    catch (InterruptedException e) {
529 <                        threadUnexpectedException();
530 <                    }
531 <                }
532 <            });
501 >        Thread t = new Thread(new CheckedRunnable() {
502 >            public void realRun() throws InterruptedException {
503 >                sync.acquire(1);
504 >                c.await();
505 >                sync.release(1);
506 >            }});
507  
508          t.start();
509          Thread.sleep(SHORT_DELAY_MS);
# Line 546 | Line 520 | public class AbstractQueuedSynchronizerT
520       * hasWaiters throws NPE if null
521       */
522      public void testHasWaitersNPE() {
523 <        final Mutex sync = new Mutex();
523 >        final Mutex sync = new Mutex();
524          try {
525              sync.hasWaiters(null);
526              shouldThrow();
# Line 557 | Line 531 | public class AbstractQueuedSynchronizerT
531       * getWaitQueueLength throws NPE if null
532       */
533      public void testGetWaitQueueLengthNPE() {
534 <        final Mutex sync = new Mutex();
534 >        final Mutex sync = new Mutex();
535          try {
536              sync.getWaitQueueLength(null);
537              shouldThrow();
# Line 569 | Line 543 | public class AbstractQueuedSynchronizerT
543       * getWaitingThreads throws NPE if null
544       */
545      public void testGetWaitingThreadsNPE() {
546 <        final Mutex sync = new Mutex();
546 >        final Mutex sync = new Mutex();
547          try {
548              sync.getWaitingThreads(null);
549              shouldThrow();
# Line 581 | Line 555 | public class AbstractQueuedSynchronizerT
555       * hasWaiters throws IAE if not owned
556       */
557      public void testHasWaitersIAE() {
558 <        final Mutex sync = new Mutex();
558 >        final Mutex sync = new Mutex();
559          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
560 <        final Mutex sync2 = new Mutex();
560 >        final Mutex sync2 = new Mutex();
561          try {
562              sync2.hasWaiters(c);
563              shouldThrow();
# Line 594 | Line 568 | public class AbstractQueuedSynchronizerT
568       * hasWaiters throws IMSE if not synced
569       */
570      public void testHasWaitersIMSE() {
571 <        final Mutex sync = new Mutex();
571 >        final Mutex sync = new Mutex();
572          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
573          try {
574              sync.hasWaiters(c);
# Line 607 | Line 581 | public class AbstractQueuedSynchronizerT
581       * getWaitQueueLength throws IAE if not owned
582       */
583      public void testGetWaitQueueLengthIAE() {
584 <        final Mutex sync = new Mutex();
584 >        final Mutex sync = new Mutex();
585          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
586 <        final Mutex sync2 = new Mutex();
586 >        final Mutex sync2 = new Mutex();
587          try {
588              sync2.getWaitQueueLength(c);
589              shouldThrow();
# Line 620 | Line 594 | public class AbstractQueuedSynchronizerT
594       * getWaitQueueLength throws IMSE if not synced
595       */
596      public void testGetWaitQueueLengthIMSE() {
597 <        final Mutex sync = new Mutex();
597 >        final Mutex sync = new Mutex();
598          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
599          try {
600              sync.getWaitQueueLength(c);
# Line 633 | Line 607 | public class AbstractQueuedSynchronizerT
607       * getWaitingThreads throws IAE if not owned
608       */
609      public void testGetWaitingThreadsIAE() {
610 <        final Mutex sync = new Mutex();
610 >        final Mutex sync = new Mutex();
611          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
612 <        final Mutex sync2 = new Mutex();
612 >        final Mutex sync2 = new Mutex();
613          try {
614              sync2.getWaitingThreads(c);
615              shouldThrow();
# Line 646 | Line 620 | public class AbstractQueuedSynchronizerT
620       * getWaitingThreads throws IMSE if not synced
621       */
622      public void testGetWaitingThreadsIMSE() {
623 <        final Mutex sync = new Mutex();
623 >        final Mutex sync = new Mutex();
624          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
625          try {
626              sync.getWaitingThreads(c);
# Line 660 | Line 634 | public class AbstractQueuedSynchronizerT
634       * hasWaiters returns true when a thread is waiting, else false
635       */
636      public void testHasWaiters() throws InterruptedException {
637 <        final Mutex sync = new Mutex();
637 >        final Mutex sync = new Mutex();
638          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
639 <        Thread t = new Thread(new Runnable() {
640 <                public void run() {
641 <                    try {
642 <                        sync.acquire(1);
643 <                        threadAssertFalse(sync.hasWaiters(c));
644 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
645 <                        c.await();
646 <                        sync.release(1);
673 <                    }
674 <                    catch (InterruptedException e) {
675 <                        threadUnexpectedException();
676 <                    }
677 <                }
678 <            });
639 >        Thread t = new Thread(new CheckedRunnable() {
640 >            public void realRun() throws InterruptedException {
641 >                sync.acquire(1);
642 >                threadAssertFalse(sync.hasWaiters(c));
643 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
644 >                c.await();
645 >                sync.release(1);
646 >            }});
647  
648          t.start();
649          Thread.sleep(SHORT_DELAY_MS);
# Line 697 | Line 665 | public class AbstractQueuedSynchronizerT
665       * getWaitQueueLength returns number of waiting threads
666       */
667      public void testGetWaitQueueLength() throws InterruptedException {
668 <        final Mutex sync = new Mutex();
668 >        final Mutex sync = new Mutex();
669          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
670 <        Thread t1 = new Thread(new Runnable() {
671 <                public void run() {
672 <                    try {
673 <                        sync.acquire(1);
674 <                        threadAssertFalse(sync.hasWaiters(c));
675 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
676 <                        c.await();
677 <                        sync.release(1);
678 <                    }
679 <                    catch (InterruptedException e) {
680 <                        threadUnexpectedException();
681 <                    }
682 <                }
683 <            });
684 <
685 <        Thread t2 = new Thread(new Runnable() {
686 <                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 <            });
670 >        Thread t1 = new Thread(new CheckedRunnable() {
671 >            public void realRun() throws InterruptedException {
672 >                sync.acquire(1);
673 >                threadAssertFalse(sync.hasWaiters(c));
674 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
675 >                c.await();
676 >                sync.release(1);
677 >            }});
678 >
679 >        Thread t2 = new Thread(new CheckedRunnable() {
680 >            public void realRun() throws InterruptedException {
681 >                sync.acquire(1);
682 >                threadAssertTrue(sync.hasWaiters(c));
683 >                threadAssertEquals(1, sync.getWaitQueueLength(c));
684 >                c.await();
685 >                sync.release(1);
686 >            }});
687  
688          t1.start();
689          Thread.sleep(SHORT_DELAY_MS);
# Line 753 | Line 709 | public class AbstractQueuedSynchronizerT
709       * getWaitingThreads returns only and all waiting threads
710       */
711      public void testGetWaitingThreads() throws InterruptedException {
712 <        final Mutex sync = new Mutex();
712 >        final Mutex sync = new Mutex();
713          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
714 <        Thread t1 = new Thread(new Runnable() {
715 <                public void run() {
716 <                    try {
717 <                        sync.acquire(1);
718 <                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
719 <                        c.await();
720 <                        sync.release(1);
721 <                    }
722 <                    catch (InterruptedException e) {
723 <                        threadUnexpectedException();
724 <                    }
725 <                }
726 <            });
727 <
728 <        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 <            });
714 >        Thread t1 = new Thread(new CheckedRunnable() {
715 >            public void realRun() throws InterruptedException {
716 >                sync.acquire(1);
717 >                threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
718 >                c.await();
719 >                sync.release(1);
720 >            }});
721 >
722 >        Thread t2 = new Thread(new CheckedRunnable() {
723 >            public void realRun() throws InterruptedException {
724 >                sync.acquire(1);
725 >                threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
726 >                c.await();
727 >                sync.release(1);
728 >            }});
729  
730          sync.acquire(1);
731          assertTrue(sync.getWaitingThreads(c).isEmpty());
# Line 813 | Line 757 | public class AbstractQueuedSynchronizerT
757       * awaitUninterruptibly doesn't abort on interrupt
758       */
759      public void testAwaitUninterruptibly() throws InterruptedException {
760 <        final Mutex sync = new Mutex();
760 >        final Mutex sync = new Mutex();
761          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
762 <        Thread t = new Thread(new Runnable() {
763 <                public void run() {
764 <                    sync.acquire(1);
765 <                    c.awaitUninterruptibly();
766 <                    sync.release(1);
767 <                }
824 <            });
762 >        Thread t = new Thread(new CheckedRunnable() {
763 >            public void realRun() {
764 >                sync.acquire(1);
765 >                c.awaitUninterruptibly();
766 >                sync.release(1);
767 >            }});
768  
769          t.start();
770          Thread.sleep(SHORT_DELAY_MS);
# Line 837 | Line 780 | public class AbstractQueuedSynchronizerT
780       * await is interruptible
781       */
782      public void testAwait_Interrupt() throws InterruptedException {
783 <        final Mutex sync = new Mutex();
783 >        final Mutex sync = new Mutex();
784          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
785 <        Thread t = new Thread(new Runnable() {
786 <                public void run() {
787 <                    try {
788 <                        sync.acquire(1);
789 <                        c.await();
847 <                        sync.release(1);
848 <                        threadShouldThrow();
849 <                    }
850 <                    catch (InterruptedException success) {
851 <                    }
852 <                }
853 <            });
785 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
786 >            public void realRun() throws InterruptedException {
787 >                sync.acquire(1);
788 >                c.await();
789 >            }});
790  
791          t.start();
792          Thread.sleep(SHORT_DELAY_MS);
# Line 863 | 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
873 <                        sync.release(1);
874 <                        threadShouldThrow();
875 <                    }
876 <                    catch (InterruptedException success) {
877 <                    }
878 <                }
879 <            });
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 >            }});
809  
810          t.start();
811          Thread.sleep(SHORT_DELAY_MS);
# Line 889 | Line 818 | public class AbstractQueuedSynchronizerT
818       * awaitUntil is interruptible
819       */
820      public void testAwaitUntil_Interrupt() throws InterruptedException {
821 <        final Mutex sync = new Mutex();
821 >        final Mutex sync = new Mutex();
822          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
823 <        Thread t = new Thread(new Runnable() {
824 <                public void run() {
825 <                    try {
826 <                        sync.acquire(1);
827 <                        java.util.Date d = new java.util.Date();
828 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
900 <                        sync.release(1);
901 <                        threadShouldThrow();
902 <                    }
903 <                    catch (InterruptedException success) {
904 <                    }
905 <                }
906 <            });
823 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
824 >            public void realRun() throws InterruptedException {
825 >                sync.acquire(1);
826 >                java.util.Date d = new java.util.Date();
827 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
828 >            }});
829  
830          t.start();
831          Thread.sleep(SHORT_DELAY_MS);
# Line 916 | Line 838 | public class AbstractQueuedSynchronizerT
838       * signalAll wakes up all threads
839       */
840      public void testSignalAll() throws InterruptedException {
841 <        final Mutex sync = new Mutex();
841 >        final Mutex sync = new Mutex();
842          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
843 <        Thread t1 = new Thread(new Runnable() {
844 <                public void run() {
845 <                    try {
846 <                        sync.acquire(1);
847 <                        c.await();
848 <                        sync.release(1);
849 <                    }
850 <                    catch (InterruptedException e) {
851 <                        threadUnexpectedException();
852 <                    }
853 <                }
854 <            });
855 <
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 <            });
843 >        Thread t1 = new Thread(new CheckedRunnable() {
844 >            public void realRun() throws InterruptedException {
845 >                sync.acquire(1);
846 >                c.await();
847 >                sync.release(1);
848 >            }});
849 >
850 >        Thread t2 = new Thread(new CheckedRunnable() {
851 >            public void realRun() throws InterruptedException {
852 >                sync.acquire(1);
853 >                c.await();
854 >                sync.release(1);
855 >            }});
856  
857          t1.start();
858          t2.start();
# Line 993 | Line 903 | public class AbstractQueuedSynchronizerT
903       * tryReleaseShared setting state changes getState
904       */
905      public void testGetStateWithReleaseShared() {
906 <        final BooleanLatch l = new BooleanLatch();
907 <        assertFalse(l.isSignalled());
908 <        l.releaseShared(0);
909 <        assertTrue(l.isSignalled());
906 >        final BooleanLatch l = new BooleanLatch();
907 >        assertFalse(l.isSignalled());
908 >        l.releaseShared(0);
909 >        assertTrue(l.isSignalled());
910      }
911  
912      /**
913       * releaseShared has no effect when already signalled
914       */
915      public void testReleaseShared() {
916 <        final BooleanLatch l = new BooleanLatch();
917 <        assertFalse(l.isSignalled());
918 <        l.releaseShared(0);
919 <        assertTrue(l.isSignalled());
920 <        l.releaseShared(0);
921 <        assertTrue(l.isSignalled());
916 >        final BooleanLatch l = new BooleanLatch();
917 >        assertFalse(l.isSignalled());
918 >        l.releaseShared(0);
919 >        assertTrue(l.isSignalled());
920 >        l.releaseShared(0);
921 >        assertTrue(l.isSignalled());
922      }
923  
924      /**
925       * acquireSharedInterruptibly returns after release, but not before
926       */
927      public void testAcquireSharedInterruptibly() throws InterruptedException {
928 <        final BooleanLatch l = new BooleanLatch();
928 >        final BooleanLatch l = new BooleanLatch();
929  
930 <        Thread t = new Thread(new Runnable() {
931 <                public void run() {
932 <                    try {
933 <                        threadAssertFalse(l.isSignalled());
934 <                        l.acquireSharedInterruptibly(0);
935 <                        threadAssertTrue(l.isSignalled());
1026 <                    } catch (InterruptedException e) {
1027 <                        threadUnexpectedException();
1028 <                    }
1029 <                }
1030 <            });
930 >        Thread t = new Thread(new CheckedRunnable() {
931 >            public void realRun() throws InterruptedException {
932 >                threadAssertFalse(l.isSignalled());
933 >                l.acquireSharedInterruptibly(0);
934 >                threadAssertTrue(l.isSignalled());
935 >            }});
936  
937          t.start();
938          assertFalse(l.isSignalled());
# Line 1042 | Line 947 | public class AbstractQueuedSynchronizerT
947       * acquireSharedTimed returns after release
948       */
949      public void testAsquireSharedTimed() throws InterruptedException {
950 <        final BooleanLatch l = new BooleanLatch();
950 >        final BooleanLatch l = new BooleanLatch();
951  
952 <        Thread t = new Thread(new Runnable() {
953 <                public void run() {
954 <                    try {
955 <                        threadAssertFalse(l.isSignalled());
956 <                        threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
957 <                        threadAssertTrue(l.isSignalled());
1053 <
1054 <                    } catch (InterruptedException e) {
1055 <                        threadUnexpectedException();
1056 <                    }
1057 <                }
1058 <            });
952 >        Thread t = new Thread(new CheckedRunnable() {
953 >            public void realRun() throws InterruptedException {
954 >                threadAssertFalse(l.isSignalled());
955 >                threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
956 >                threadAssertTrue(l.isSignalled());
957 >            }});
958  
959          t.start();
960          assertFalse(l.isSignalled());
# Line 1070 | Line 969 | public class AbstractQueuedSynchronizerT
969       */
970      public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
971          final BooleanLatch l = new BooleanLatch();
972 <        Thread t = new Thread(new Runnable() {
973 <                public void run() {
974 <                    try {
975 <                        threadAssertFalse(l.isSignalled());
976 <                        l.acquireSharedInterruptibly(0);
977 <                        threadShouldThrow();
978 <                    } catch (InterruptedException success) {}
1080 <                }
1081 <            });
1082 <        t.start();
972 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
973 >            public void realRun() throws InterruptedException {
974 >                threadAssertFalse(l.isSignalled());
975 >                l.acquireSharedInterruptibly(0);
976 >            }});
977 >
978 >        t.start();
979          assertFalse(l.isSignalled());
980          t.interrupt();
981          t.join();
# Line 1090 | Line 986 | public class AbstractQueuedSynchronizerT
986       */
987      public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
988          final BooleanLatch l = new BooleanLatch();
989 <        Thread t = new Thread(new Runnable() {
990 <                public void run() {
991 <                    try {
992 <                        threadAssertFalse(l.isSignalled());
993 <                        l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
994 <                        threadShouldThrow();
1099 <                    } catch (InterruptedException success) {}
1100 <                }
1101 <            });
989 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
990 >            public void realRun() throws InterruptedException {
991 >                threadAssertFalse(l.isSignalled());
992 >                l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
993 >            }});
994 >
995          t.start();
996          Thread.sleep(SHORT_DELAY_MS);
997          assertFalse(l.isSignalled());
# Line 1111 | Line 1004 | public class AbstractQueuedSynchronizerT
1004       */
1005      public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1006          final BooleanLatch l = new BooleanLatch();
1007 <        Thread t = new Thread(new Runnable() {
1008 <                public void run() {
1009 <                    try {
1010 <                        threadAssertFalse(l.isSignalled());
1011 <                        threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1012 <                    } catch (InterruptedException ie) {
1120 <                        threadUnexpectedException();
1121 <                    }
1122 <                }
1123 <            });
1007 >        Thread t = new Thread(new CheckedRunnable() {
1008 >            public void realRun() throws InterruptedException {
1009 >                threadAssertFalse(l.isSignalled());
1010 >                threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1011 >            }});
1012 >
1013          t.start();
1014          Thread.sleep(SHORT_DELAY_MS);
1015          assertFalse(l.isSignalled());
1016          t.join();
1017      }
1018  
1130
1019   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines