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 |
|
/** |
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(); |
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(); |
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)); |
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(); |
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(); |
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()); |
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()); |
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()); |
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(); |
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(); |
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); |
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)); |
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(); |
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)); |
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(); |
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(); |
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(); |
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(); |
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()); |