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.*; |
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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
17 |
|
|
18 |
|
public class CyclicBarrierTest extends JSR166TestCase { |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
+ |
* Spin-waits till the number of waiters == numberOfWaiters. |
33 |
+ |
*/ |
34 |
+ |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
35 |
+ |
long startTime = System.nanoTime(); |
36 |
+ |
while (barrier.getNumberWaiting() != numberOfWaiters) { |
37 |
+ |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
38 |
+ |
fail("timed out"); |
39 |
+ |
Thread.yield(); |
40 |
+ |
} |
41 |
+ |
} |
42 |
+ |
|
43 |
+ |
/** |
44 |
|
* Creating with negative parties throws IAE |
45 |
|
*/ |
46 |
|
public void testConstructor1() { |
92 |
|
b.await(); |
93 |
|
b.await(); |
94 |
|
assertEquals(0, b.getNumberWaiting()); |
95 |
< |
assertEquals(countAction, 2); |
95 |
> |
assertEquals(2, countAction); |
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(); |