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

Comparing jsr166/src/test/tck/SemaphoreTest.java (file contents):
Revision 1.19 by jsr166, Tue Nov 17 02:22:50 2009 UTC vs.
Revision 1.20 by jsr166, Sat Nov 21 02:07:27 2009 UTC

# Line 14 | Line 14 | import java.io.*;
14  
15   public class SemaphoreTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(SemaphoreTest.class);
20 >        return new TestSuite(SemaphoreTest.class);
21      }
22  
23      /**
# Line 161 | Line 161 | public class SemaphoreTest extends JSR16
161      public void testAcquireReleaseInDifferentThreads()
162          throws InterruptedException {
163          final Semaphore s = new Semaphore(0, false);
164 <        Thread t = new Thread(new CheckedRunnable() {
165 <            public void realRun() throws InterruptedException {
164 >        Thread t = new Thread(new CheckedRunnable() {
165 >            public void realRun() throws InterruptedException {
166                  s.acquire();
167                  s.release();
168                  s.release();
# Line 185 | Line 185 | public class SemaphoreTest extends JSR16
185      public void testUninterruptibleAcquireReleaseInDifferentThreads()
186          throws InterruptedException {
187          final Semaphore s = new Semaphore(0, false);
188 <        Thread t = new Thread(new CheckedRunnable() {
188 >        Thread t = new Thread(new CheckedRunnable() {
189              public void realRun() throws InterruptedException {
190                  s.acquireUninterruptibly();
191                  s.release();
# Line 210 | Line 210 | public class SemaphoreTest extends JSR16
210      public void testTimedAcquireReleaseInDifferentThreads()
211          throws InterruptedException {
212          final Semaphore s = new Semaphore(1, false);
213 <        Thread t = new Thread(new CheckedRunnable() {
213 >        Thread t = new Thread(new CheckedRunnable() {
214              public void realRun() throws InterruptedException {
215                  s.release();
216                  threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
# Line 232 | Line 232 | public class SemaphoreTest extends JSR16
232       */
233      public void testAcquire_InterruptedException()
234          throws InterruptedException {
235 <        final Semaphore s = new Semaphore(0, false);
236 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
237 <            public void realRun() throws InterruptedException {
235 >        final Semaphore s = new Semaphore(0, false);
236 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
237 >            public void realRun() throws InterruptedException {
238                  s.acquire();
239              }});
240  
241 <        t.start();
241 >        t.start();
242          Thread.sleep(SHORT_DELAY_MS);
243          t.interrupt();
244          t.join();
# Line 249 | Line 249 | public class SemaphoreTest extends JSR16
249       */
250      public void testTryAcquire_InterruptedException()
251          throws InterruptedException {
252 <        final Semaphore s = new Semaphore(0, false);
253 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
254 <            public void realRun() throws InterruptedException {
252 >        final Semaphore s = new Semaphore(0, false);
253 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
254 >            public void realRun() throws InterruptedException {
255                  s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS);
256              }});
257  
258 <        t.start();
258 >        t.start();
259          Thread.sleep(SHORT_DELAY_MS);
260          t.interrupt();
261          t.join();
# Line 265 | Line 265 | public class SemaphoreTest extends JSR16
265       * hasQueuedThreads reports whether there are waiting threads
266       */
267      public void testHasQueuedThreads() throws InterruptedException {
268 <        final Semaphore lock = new Semaphore(1, false);
268 >        final Semaphore lock = new Semaphore(1, false);
269          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
270          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
271          assertFalse(lock.hasQueuedThreads());
# Line 290 | Line 290 | public class SemaphoreTest extends JSR16
290       * getQueueLength reports number of waiting threads
291       */
292      public void testGetQueueLength() throws InterruptedException {
293 <        final Semaphore lock = new Semaphore(1, false);
293 >        final Semaphore lock = new Semaphore(1, false);
294          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
295          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
296          assertEquals(0, lock.getQueueLength());
# Line 315 | Line 315 | public class SemaphoreTest extends JSR16
315       * getQueuedThreads includes waiting threads
316       */
317      public void testGetQueuedThreads() throws InterruptedException {
318 <        final PublicSemaphore lock = new PublicSemaphore(1, false);
318 >        final PublicSemaphore lock = new PublicSemaphore(1, false);
319          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
320          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
321          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 523 | Line 523 | public class SemaphoreTest extends JSR16
523      public void testAcquireReleaseInDifferentThreads_fair()
524          throws InterruptedException {
525          final Semaphore s = new Semaphore(0, true);
526 <        Thread t = new Thread(new CheckedRunnable() {
527 <            public void realRun() throws InterruptedException {
526 >        Thread t = new Thread(new CheckedRunnable() {
527 >            public void realRun() throws InterruptedException {
528                  s.acquire();
529                  s.acquire();
530                  s.acquire();
# Line 549 | Line 549 | public class SemaphoreTest extends JSR16
549      public void testAcquireReleaseNInDifferentThreads_fair()
550          throws InterruptedException {
551          final Semaphore s = new Semaphore(0, true);
552 <        Thread t = new Thread(new CheckedRunnable() {
553 <            public void realRun() throws InterruptedException {
552 >        Thread t = new Thread(new CheckedRunnable() {
553 >            public void realRun() throws InterruptedException {
554                  s.acquire();
555                  s.release(2);
556                  s.acquire();
# Line 570 | Line 570 | public class SemaphoreTest extends JSR16
570      public void testAcquireReleaseNInDifferentThreads_fair2()
571          throws InterruptedException {
572          final Semaphore s = new Semaphore(0, true);
573 <        Thread t = new Thread(new CheckedRunnable() {
574 <            public void realRun() throws InterruptedException {
573 >        Thread t = new Thread(new CheckedRunnable() {
574 >            public void realRun() throws InterruptedException {
575                  s.acquire(2);
576                  s.acquire(2);
577                  s.release(4);
# Line 593 | Line 593 | public class SemaphoreTest extends JSR16
593      public void testTimedAcquireReleaseInDifferentThreads_fair()
594          throws InterruptedException {
595          final Semaphore s = new Semaphore(1, true);
596 <        Thread t = new Thread(new CheckedRunnable() {
597 <            public void realRun() throws InterruptedException {
596 >        Thread t = new Thread(new CheckedRunnable() {
597 >            public void realRun() throws InterruptedException {
598                  threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
599                  threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
600                  threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
# Line 602 | Line 602 | public class SemaphoreTest extends JSR16
602                  threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
603              }});
604  
605 <        t.start();
605 >        t.start();
606          s.release();
607          s.release();
608          s.release();
# Line 617 | Line 617 | public class SemaphoreTest extends JSR16
617      public void testTimedAcquireReleaseNInDifferentThreads_fair()
618          throws InterruptedException {
619          final Semaphore s = new Semaphore(2, true);
620 <        Thread t = new Thread(new CheckedRunnable() {
621 <            public void realRun() throws InterruptedException {
620 >        Thread t = new Thread(new CheckedRunnable() {
621 >            public void realRun() throws InterruptedException {
622                  threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
623                  s.release(2);
624                  threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
625                  s.release(2);
626              }});
627  
628 <        t.start();
628 >        t.start();
629          assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
630          s.release(2);
631          assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
# Line 638 | Line 638 | public class SemaphoreTest extends JSR16
638       */
639      public void testAcquire_InterruptedException_fair()
640          throws InterruptedException {
641 <        final Semaphore s = new Semaphore(0, true);
642 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
643 <            public void realRun() throws InterruptedException {
641 >        final Semaphore s = new Semaphore(0, true);
642 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
643 >            public void realRun() throws InterruptedException {
644                  s.acquire();
645              }});
646  
647 <        t.start();
647 >        t.start();
648          Thread.sleep(SHORT_DELAY_MS);
649          t.interrupt();
650          t.join();
# Line 655 | Line 655 | public class SemaphoreTest extends JSR16
655       */
656      public void testAcquireN_InterruptedException_fair()
657          throws InterruptedException {
658 <        final Semaphore s = new Semaphore(2, true);
659 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
660 <            public void realRun() throws InterruptedException {
658 >        final Semaphore s = new Semaphore(2, true);
659 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
660 >            public void realRun() throws InterruptedException {
661                  s.acquire(3);
662              }});
663  
664 <        t.start();
664 >        t.start();
665          Thread.sleep(SHORT_DELAY_MS);
666          t.interrupt();
667          t.join();
# Line 672 | Line 672 | public class SemaphoreTest extends JSR16
672       */
673      public void testTryAcquire_InterruptedException_fair()
674          throws InterruptedException {
675 <        final Semaphore s = new Semaphore(0, true);
676 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
677 <            public void realRun() throws InterruptedException {
675 >        final Semaphore s = new Semaphore(0, true);
676 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
677 >            public void realRun() throws InterruptedException {
678                  s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS);
679              }});
680  
681 <        t.start();
681 >        t.start();
682          Thread.sleep(SHORT_DELAY_MS);
683          t.interrupt();
684          t.join();
# Line 689 | Line 689 | public class SemaphoreTest extends JSR16
689       */
690      public void testTryAcquireN_InterruptedException_fair()
691          throws InterruptedException {
692 <        final Semaphore s = new Semaphore(1, true);
693 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
694 <            public void realRun() throws InterruptedException {
692 >        final Semaphore s = new Semaphore(1, true);
693 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
694 >            public void realRun() throws InterruptedException {
695                  s.tryAcquire(4, MEDIUM_DELAY_MS, MILLISECONDS);
696              }});
697  
698 <        t.start();
698 >        t.start();
699          Thread.sleep(SHORT_DELAY_MS);
700          t.interrupt();
701          t.join();
# Line 705 | Line 705 | public class SemaphoreTest extends JSR16
705       * getQueueLength reports number of waiting threads
706       */
707      public void testGetQueueLength_fair() throws InterruptedException {
708 <        final Semaphore lock = new Semaphore(1, true);
708 >        final Semaphore lock = new Semaphore(1, true);
709          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
710          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
711          assertEquals(0, lock.getQueueLength());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines