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); |
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(); |
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 { |
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(); |
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 { |
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 { |