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 |
+ |
|
17 |
+ |
import junit.framework.Test; |
18 |
+ |
import junit.framework.TestSuite; |
19 |
+ |
|
20 |
|
public class CyclicBarrierTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
22 |
|
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
|
|
33 |
|
/** |
34 |
+ |
* Spin-waits till the number of waiters == numberOfWaiters. |
35 |
+ |
*/ |
36 |
+ |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
37 |
+ |
long startTime = System.nanoTime(); |
38 |
+ |
while (barrier.getNumberWaiting() != numberOfWaiters) { |
39 |
+ |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
40 |
+ |
fail("timed out"); |
41 |
+ |
Thread.yield(); |
42 |
+ |
} |
43 |
+ |
} |
44 |
+ |
|
45 |
+ |
/** |
46 |
|
* Creating with negative parties throws IAE |
47 |
|
*/ |
48 |
|
public void testConstructor1() { |
94 |
|
b.await(); |
95 |
|
b.await(); |
96 |
|
assertEquals(0, b.getNumberWaiting()); |
97 |
< |
assertEquals(countAction, 2); |
97 |
> |
assertEquals(2, countAction); |
98 |
|
} |
99 |
|
|
100 |
|
/** |
202 |
|
}}); |
203 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
204 |
|
public void realRun() throws Exception { |
205 |
< |
while (c.getNumberWaiting() == 0) |
190 |
< |
Thread.yield(); |
205 |
> |
awaitNumberWaiting(c, 1); |
206 |
|
long startTime = System.nanoTime(); |
207 |
|
try { |
208 |
|
c.await(timeoutMillis(), MILLISECONDS); |
230 |
|
}}); |
231 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
232 |
|
public void realRun() throws Exception { |
233 |
< |
while (c.getNumberWaiting() == 0) |
219 |
< |
Thread.yield(); |
233 |
> |
awaitNumberWaiting(c, 1); |
234 |
|
long startTime = System.nanoTime(); |
235 |
|
try { |
236 |
|
c.await(timeoutMillis(), MILLISECONDS); |
264 |
|
t1.start(); |
265 |
|
t2.start(); |
266 |
|
await(pleaseReset); |
267 |
+ |
|
268 |
+ |
awaitNumberWaiting(c, 2); |
269 |
|
c.reset(); |
270 |
|
awaitTermination(t1); |
271 |
|
awaitTermination(t2); |
402 |
|
}}); |
403 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
404 |
|
public void realRun() throws Exception { |
405 |
< |
while (barrier.getNumberWaiting() == 0) |
390 |
< |
Thread.yield(); |
405 |
> |
awaitNumberWaiting(barrier, 1); |
406 |
|
long startTime = System.nanoTime(); |
407 |
|
try { |
408 |
|
barrier.await(timeoutMillis(), MILLISECONDS); |
447 |
|
t1.start(); |
448 |
|
t2.start(); |
449 |
|
start.await(); |
450 |
< |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
450 |
> |
awaitNumberWaiting(barrier, 2); |
451 |
|
try { |
452 |
|
barrier.await(); |
453 |
|
shouldThrow(); |