1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
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); |
169 |
|
}}); |
170 |
|
|
171 |
|
t.start(); |
172 |
< |
Thread.sleep(SHORT_DELAY_MS); |
172 |
> |
delay(SHORT_DELAY_MS); |
173 |
|
s.release(); |
174 |
|
s.release(); |
175 |
|
s.acquire(); |
193 |
|
}}); |
194 |
|
|
195 |
|
t.start(); |
196 |
< |
Thread.sleep(SHORT_DELAY_MS); |
196 |
> |
delay(SHORT_DELAY_MS); |
197 |
|
s.release(); |
198 |
|
s.release(); |
199 |
|
s.acquireUninterruptibly(); |
204 |
|
|
205 |
|
|
206 |
|
/** |
207 |
< |
* A release in one thread enables a timed acquire in another thread |
207 |
> |
* A release in one thread enables a timed acquire in another thread |
208 |
|
*/ |
209 |
|
public void testTimedAcquireReleaseInDifferentThreads() |
210 |
|
throws InterruptedException { |
212 |
|
Thread t = new Thread(new CheckedRunnable() { |
213 |
|
public void realRun() throws InterruptedException { |
214 |
|
s.release(); |
215 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
215 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
216 |
|
s.release(); |
217 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
217 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
218 |
|
}}); |
219 |
|
|
220 |
|
t.start(); |
238 |
|
}}); |
239 |
|
|
240 |
|
t.start(); |
241 |
< |
Thread.sleep(SHORT_DELAY_MS); |
241 |
> |
delay(SHORT_DELAY_MS); |
242 |
|
t.interrupt(); |
243 |
|
t.join(); |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* A waiting timed acquire blocks interruptibly |
247 |
> |
* A waiting timed acquire blocks interruptibly |
248 |
|
*/ |
249 |
|
public void testTryAcquire_InterruptedException() |
250 |
|
throws InterruptedException { |
255 |
|
}}); |
256 |
|
|
257 |
|
t.start(); |
258 |
< |
Thread.sleep(SHORT_DELAY_MS); |
258 |
> |
delay(SHORT_DELAY_MS); |
259 |
|
t.interrupt(); |
260 |
|
t.join(); |
261 |
|
} |
270 |
|
assertFalse(lock.hasQueuedThreads()); |
271 |
|
lock.acquireUninterruptibly(); |
272 |
|
t1.start(); |
273 |
< |
Thread.sleep(SHORT_DELAY_MS); |
273 |
> |
delay(SHORT_DELAY_MS); |
274 |
|
assertTrue(lock.hasQueuedThreads()); |
275 |
|
t2.start(); |
276 |
< |
Thread.sleep(SHORT_DELAY_MS); |
276 |
> |
delay(SHORT_DELAY_MS); |
277 |
|
assertTrue(lock.hasQueuedThreads()); |
278 |
|
t1.interrupt(); |
279 |
< |
Thread.sleep(SHORT_DELAY_MS); |
279 |
> |
delay(SHORT_DELAY_MS); |
280 |
|
assertTrue(lock.hasQueuedThreads()); |
281 |
|
lock.release(); |
282 |
< |
Thread.sleep(SHORT_DELAY_MS); |
282 |
> |
delay(SHORT_DELAY_MS); |
283 |
|
assertFalse(lock.hasQueuedThreads()); |
284 |
|
t1.join(); |
285 |
|
t2.join(); |
295 |
|
assertEquals(0, lock.getQueueLength()); |
296 |
|
lock.acquireUninterruptibly(); |
297 |
|
t1.start(); |
298 |
< |
Thread.sleep(SHORT_DELAY_MS); |
298 |
> |
delay(SHORT_DELAY_MS); |
299 |
|
assertEquals(1, lock.getQueueLength()); |
300 |
|
t2.start(); |
301 |
< |
Thread.sleep(SHORT_DELAY_MS); |
301 |
> |
delay(SHORT_DELAY_MS); |
302 |
|
assertEquals(2, lock.getQueueLength()); |
303 |
|
t1.interrupt(); |
304 |
< |
Thread.sleep(SHORT_DELAY_MS); |
304 |
> |
delay(SHORT_DELAY_MS); |
305 |
|
assertEquals(1, lock.getQueueLength()); |
306 |
|
lock.release(); |
307 |
< |
Thread.sleep(SHORT_DELAY_MS); |
307 |
> |
delay(SHORT_DELAY_MS); |
308 |
|
assertEquals(0, lock.getQueueLength()); |
309 |
|
t1.join(); |
310 |
|
t2.join(); |
321 |
|
lock.acquireUninterruptibly(); |
322 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
323 |
|
t1.start(); |
324 |
< |
Thread.sleep(SHORT_DELAY_MS); |
324 |
> |
delay(SHORT_DELAY_MS); |
325 |
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
326 |
|
t2.start(); |
327 |
< |
Thread.sleep(SHORT_DELAY_MS); |
327 |
> |
delay(SHORT_DELAY_MS); |
328 |
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
329 |
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
330 |
|
t1.interrupt(); |
331 |
< |
Thread.sleep(SHORT_DELAY_MS); |
331 |
> |
delay(SHORT_DELAY_MS); |
332 |
|
assertFalse(lock.getQueuedThreads().contains(t1)); |
333 |
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
334 |
|
lock.release(); |
335 |
< |
Thread.sleep(SHORT_DELAY_MS); |
335 |
> |
delay(SHORT_DELAY_MS); |
336 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
337 |
|
t1.join(); |
338 |
|
t2.join(); |
531 |
|
}}); |
532 |
|
|
533 |
|
t.start(); |
534 |
< |
Thread.sleep(SHORT_DELAY_MS); |
534 |
> |
delay(SHORT_DELAY_MS); |
535 |
|
s.release(); |
536 |
|
s.release(); |
537 |
|
s.release(); |
556 |
|
}}); |
557 |
|
|
558 |
|
t.start(); |
559 |
< |
Thread.sleep(SHORT_DELAY_MS); |
559 |
> |
delay(SHORT_DELAY_MS); |
560 |
|
s.release(2); |
561 |
|
s.acquire(2); |
562 |
|
s.release(1); |
577 |
|
}}); |
578 |
|
|
579 |
|
t.start(); |
580 |
< |
Thread.sleep(SHORT_DELAY_MS); |
580 |
> |
delay(SHORT_DELAY_MS); |
581 |
|
s.release(6); |
582 |
|
s.acquire(2); |
583 |
|
s.acquire(2); |
594 |
|
final Semaphore s = new Semaphore(1, true); |
595 |
|
Thread t = new Thread(new CheckedRunnable() { |
596 |
|
public void realRun() throws InterruptedException { |
597 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
598 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
599 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
600 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
601 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
597 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
598 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
599 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
600 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
601 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
602 |
|
}}); |
603 |
|
|
604 |
|
t.start(); |
618 |
|
final Semaphore s = new Semaphore(2, true); |
619 |
|
Thread t = new Thread(new CheckedRunnable() { |
620 |
|
public void realRun() throws InterruptedException { |
621 |
< |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
621 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
622 |
|
s.release(2); |
623 |
< |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
623 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
624 |
|
s.release(2); |
625 |
|
}}); |
626 |
|
|
644 |
|
}}); |
645 |
|
|
646 |
|
t.start(); |
647 |
< |
Thread.sleep(SHORT_DELAY_MS); |
647 |
> |
delay(SHORT_DELAY_MS); |
648 |
|
t.interrupt(); |
649 |
|
t.join(); |
650 |
|
} |
661 |
|
}}); |
662 |
|
|
663 |
|
t.start(); |
664 |
< |
Thread.sleep(SHORT_DELAY_MS); |
664 |
> |
delay(SHORT_DELAY_MS); |
665 |
|
t.interrupt(); |
666 |
|
t.join(); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* A waiting tryAcquire blocks interruptibly |
670 |
> |
* A waiting tryAcquire blocks interruptibly |
671 |
|
*/ |
672 |
|
public void testTryAcquire_InterruptedException_fair() |
673 |
|
throws InterruptedException { |
678 |
|
}}); |
679 |
|
|
680 |
|
t.start(); |
681 |
< |
Thread.sleep(SHORT_DELAY_MS); |
681 |
> |
delay(SHORT_DELAY_MS); |
682 |
|
t.interrupt(); |
683 |
|
t.join(); |
684 |
|
} |
685 |
|
|
686 |
|
/** |
687 |
< |
* A waiting tryAcquire(n) blocks interruptibly |
687 |
> |
* A waiting tryAcquire(n) blocks interruptibly |
688 |
|
*/ |
689 |
|
public void testTryAcquireN_InterruptedException_fair() |
690 |
|
throws InterruptedException { |
695 |
|
}}); |
696 |
|
|
697 |
|
t.start(); |
698 |
< |
Thread.sleep(SHORT_DELAY_MS); |
698 |
> |
delay(SHORT_DELAY_MS); |
699 |
|
t.interrupt(); |
700 |
|
t.join(); |
701 |
|
} |
710 |
|
assertEquals(0, lock.getQueueLength()); |
711 |
|
lock.acquireUninterruptibly(); |
712 |
|
t1.start(); |
713 |
< |
Thread.sleep(SHORT_DELAY_MS); |
713 |
> |
delay(SHORT_DELAY_MS); |
714 |
|
assertEquals(1, lock.getQueueLength()); |
715 |
|
t2.start(); |
716 |
< |
Thread.sleep(SHORT_DELAY_MS); |
716 |
> |
delay(SHORT_DELAY_MS); |
717 |
|
assertEquals(2, lock.getQueueLength()); |
718 |
|
t1.interrupt(); |
719 |
< |
Thread.sleep(SHORT_DELAY_MS); |
719 |
> |
delay(SHORT_DELAY_MS); |
720 |
|
assertEquals(1, lock.getQueueLength()); |
721 |
|
lock.release(); |
722 |
< |
Thread.sleep(SHORT_DELAY_MS); |
722 |
> |
delay(SHORT_DELAY_MS); |
723 |
|
assertEquals(0, lock.getQueueLength()); |
724 |
|
t1.join(); |
725 |
|
t2.join(); |