13 |
|
import java.util.concurrent.CyclicBarrier; |
14 |
|
import java.util.concurrent.TimeoutException; |
15 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
17 |
|
|
18 |
|
import junit.framework.Test; |
19 |
|
import junit.framework.TestSuite; |
26 |
|
return new TestSuite(CyclicBarrierTest.class); |
27 |
|
} |
28 |
|
|
28 |
– |
private volatile int countAction; |
29 |
– |
private class MyAction implements Runnable { |
30 |
– |
public void run() { ++countAction; } |
31 |
– |
} |
32 |
– |
|
29 |
|
/** |
30 |
|
* Spin-waits till the number of waiters == numberOfWaiters. |
31 |
|
*/ |
83 |
|
* The supplied barrier action is run at barrier |
84 |
|
*/ |
85 |
|
public void testBarrierAction() throws Exception { |
86 |
< |
countAction = 0; |
87 |
< |
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
86 |
> |
final AtomicInteger count = new AtomicInteger(0); |
87 |
> |
final Runnable incCount = new Runnable() { public void run() { |
88 |
> |
count.getAndIncrement(); }}; |
89 |
> |
CyclicBarrier b = new CyclicBarrier(1, incCount); |
90 |
|
assertEquals(1, b.getParties()); |
91 |
|
assertEquals(0, b.getNumberWaiting()); |
92 |
|
b.await(); |
93 |
|
b.await(); |
94 |
|
assertEquals(0, b.getNumberWaiting()); |
95 |
< |
assertEquals(2, countAction); |
95 |
> |
assertEquals(2, count.get()); |
96 |
|
} |
97 |
|
|
98 |
|
/** |