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 |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
13 |
|
|
14 |
|
public class SemaphoreTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(SemaphoreTest.class); |
24 |
|
*/ |
25 |
|
static class PublicSemaphore extends Semaphore { |
26 |
|
PublicSemaphore(int p, boolean f) { super(p, f); } |
27 |
< |
public Collection<Thread> getQueuedThreads() { |
28 |
< |
return super.getQueuedThreads(); |
27 |
> |
public Collection<Thread> getQueuedThreads() { |
28 |
> |
return super.getQueuedThreads(); |
29 |
|
} |
30 |
< |
public void reducePermits(int p) { |
30 |
> |
public void reducePermits(int p) { |
31 |
|
super.reducePermits(p); |
32 |
|
} |
33 |
|
} |
282 |
|
unexpectedException(); |
283 |
|
} |
284 |
|
} |
285 |
< |
|
285 |
> |
|
286 |
|
/** |
287 |
|
* A waiting timed acquire blocks interruptibly |
288 |
|
*/ |
310 |
|
/** |
311 |
|
* hasQueuedThreads reports whether there are waiting threads |
312 |
|
*/ |
313 |
< |
public void testHasQueuedThreads() { |
313 |
> |
public void testHasQueuedThreads() { |
314 |
|
final Semaphore lock = new Semaphore(1, false); |
315 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
316 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
334 |
|
} catch(Exception e){ |
335 |
|
unexpectedException(); |
336 |
|
} |
337 |
< |
} |
337 |
> |
} |
338 |
|
|
339 |
|
/** |
340 |
|
* getQueueLength reports number of waiting threads |
341 |
|
*/ |
342 |
< |
public void testGetQueueLength() { |
342 |
> |
public void testGetQueueLength() { |
343 |
|
final Semaphore lock = new Semaphore(1, false); |
344 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
345 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
363 |
|
} catch(Exception e){ |
364 |
|
unexpectedException(); |
365 |
|
} |
366 |
< |
} |
366 |
> |
} |
367 |
|
|
368 |
|
/** |
369 |
|
* getQueuedThreads includes waiting threads |
370 |
|
*/ |
371 |
< |
public void testGetQueuedThreads() { |
371 |
> |
public void testGetQueuedThreads() { |
372 |
|
final PublicSemaphore lock = new PublicSemaphore(1, false); |
373 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
374 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
395 |
|
} catch(Exception e){ |
396 |
|
unexpectedException(); |
397 |
|
} |
398 |
< |
} |
398 |
> |
} |
399 |
|
|
400 |
|
/** |
401 |
|
* drainPermits reports and removes given number of permits |
794 |
|
unexpectedException(); |
795 |
|
} |
796 |
|
} |
797 |
< |
|
797 |
> |
|
798 |
|
/** |
799 |
|
* A waiting tryAcquire blocks interruptibly |
800 |
|
*/ |
846 |
|
/** |
847 |
|
* getQueueLength reports number of waiting threads |
848 |
|
*/ |
849 |
< |
public void testGetQueueLength_fair() { |
849 |
> |
public void testGetQueueLength_fair() { |
850 |
|
final Semaphore lock = new Semaphore(1, true); |
851 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
852 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
870 |
|
} catch(Exception e){ |
871 |
|
unexpectedException(); |
872 |
|
} |
873 |
< |
} |
873 |
> |
} |
874 |
|
|
875 |
|
|
876 |
|
/** |