6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
12 |
– |
import java.util.concurrent.locks.*; |
13 |
– |
import java.util.concurrent.atomic.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.concurrent.BrokenBarrierException; |
12 |
+ |
import java.util.concurrent.CountDownLatch; |
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; |
20 |
+ |
|
21 |
|
public class CyclicBarrierTest extends JSR166TestCase { |
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
public static Test suite() { |
26 |
|
return new TestSuite(CyclicBarrierTest.class); |
27 |
|
} |
28 |
|
|
29 |
< |
private volatile int countAction; |
30 |
< |
private class MyAction implements Runnable { |
31 |
< |
public void run() { ++countAction; } |
29 |
> |
/** |
30 |
> |
* Spin-waits till the number of waiters == numberOfWaiters. |
31 |
> |
*/ |
32 |
> |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
33 |
> |
long startTime = System.nanoTime(); |
34 |
> |
while (barrier.getNumberWaiting() != numberOfWaiters) { |
35 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
36 |
> |
fail("timed out"); |
37 |
> |
Thread.yield(); |
38 |
> |
} |
39 |
|
} |
40 |
|
|
41 |
|
/** |
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(countAction, 2); |
95 |
> |
assertEquals(2, count.get()); |
96 |
|
} |
97 |
|
|
98 |
|
/** |
200 |
|
}}); |
201 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
202 |
|
public void realRun() throws Exception { |
203 |
< |
while (c.getNumberWaiting() == 0) |
190 |
< |
Thread.yield(); |
203 |
> |
awaitNumberWaiting(c, 1); |
204 |
|
long startTime = System.nanoTime(); |
205 |
|
try { |
206 |
|
c.await(timeoutMillis(), MILLISECONDS); |
228 |
|
}}); |
229 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
230 |
|
public void realRun() throws Exception { |
231 |
< |
while (c.getNumberWaiting() == 0) |
219 |
< |
Thread.yield(); |
231 |
> |
awaitNumberWaiting(c, 1); |
232 |
|
long startTime = System.nanoTime(); |
233 |
|
try { |
234 |
|
c.await(timeoutMillis(), MILLISECONDS); |
262 |
|
t1.start(); |
263 |
|
t2.start(); |
264 |
|
await(pleaseReset); |
265 |
+ |
|
266 |
+ |
awaitNumberWaiting(c, 2); |
267 |
|
c.reset(); |
268 |
|
awaitTermination(t1); |
269 |
|
awaitTermination(t2); |
400 |
|
}}); |
401 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
402 |
|
public void realRun() throws Exception { |
403 |
< |
while (barrier.getNumberWaiting() == 0) |
390 |
< |
Thread.yield(); |
403 |
> |
awaitNumberWaiting(barrier, 1); |
404 |
|
long startTime = System.nanoTime(); |
405 |
|
try { |
406 |
|
barrier.await(timeoutMillis(), MILLISECONDS); |
445 |
|
t1.start(); |
446 |
|
t2.start(); |
447 |
|
start.await(); |
448 |
< |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
448 |
> |
awaitNumberWaiting(barrier, 2); |
449 |
|
try { |
450 |
|
barrier.await(); |
451 |
|
shouldThrow(); |