8 |
|
import java.util.ArrayList; |
9 |
|
import java.util.List; |
10 |
|
import java.util.concurrent.atomic.AtomicInteger; |
11 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import junit.framework.Test; |
15 |
|
import junit.framework.TestSuite; |
16 |
|
|
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* Arrive() on a registered phaser increments phase. |
181 |
> |
* Arrive() on a registered phaser increments phase. |
182 |
|
*/ |
183 |
|
public void testArrive1() { |
184 |
|
Phaser phaser = new Phaser(1); |
237 |
|
phaser.register(); |
238 |
|
phaser.arrive(); |
239 |
|
int p = phaser.getArrivedParties(); |
240 |
< |
assertTrue(p == 1); |
240 |
> |
assertEquals(1, p); |
241 |
|
phaser.arriveAndDeregister(); |
242 |
|
assertTrue(phaser.getArrivedParties() < p); |
243 |
|
} |
254 |
|
assertTrue(parent.getUnarrivedParties() > 0); |
255 |
|
assertTrue(root.getUnarrivedParties() > 0); |
256 |
|
root.arriveAndDeregister(); |
257 |
< |
assertTrue(parent.getUnarrivedParties() == 0); |
258 |
< |
assertTrue(root.getUnarrivedParties() == 0); |
257 |
> |
assertEquals(0, parent.getUnarrivedParties()); |
258 |
> |
assertEquals(0, root.getUnarrivedParties()); |
259 |
|
assertTrue(root.isTerminated() && parent.isTerminated()); |
260 |
|
} |
261 |
|
|
285 |
|
assertTrue(child.getUnarrivedParties() > 0); |
286 |
|
root.register(); |
287 |
|
root.arriveAndDeregister(); |
288 |
< |
assertTrue(parent.getUnarrivedParties() == 0); |
289 |
< |
assertTrue(child.getUnarrivedParties() == 0); |
288 |
> |
assertEquals(0, parent.getUnarrivedParties()); |
289 |
> |
assertEquals(0, child.getUnarrivedParties()); |
290 |
|
assertTrue(root.isTerminated()); |
291 |
|
} |
292 |
|
|
330 |
|
public void testAwaitAdvance3() throws InterruptedException { |
331 |
|
final Phaser phaser = new Phaser(); |
332 |
|
phaser.register(); |
333 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
334 |
|
|
335 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
336 |
|
public void realRun() throws InterruptedException { |
337 |
|
phaser.register(); |
338 |
< |
sleepTillInterrupted(LONG_DELAY_MS); |
338 |
> |
threadStarted.countDown(); |
339 |
|
phaser.awaitAdvance(phaser.arrive()); |
340 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
341 |
|
}}); |
342 |
< |
Thread.sleep(SMALL_DELAY_MS); |
342 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
343 |
|
t.interrupt(); |
340 |
– |
Thread.sleep(SMALL_DELAY_MS); |
344 |
|
phaser.arrive(); |
345 |
< |
assertFalse(t.isInterrupted()); |
343 |
< |
t.join(); |
345 |
> |
awaitTermination(t, SMALL_DELAY_MS); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
447 |
|
*/ |
448 |
|
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
449 |
|
final Phaser phaser = new Phaser(2); |
450 |
< |
Thread th = newStartedThread(new CheckedRunnable() { |
451 |
< |
public void realRun() { |
450 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
451 |
> |
final AtomicBoolean advanced = new AtomicBoolean(false); |
452 |
> |
final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false); |
453 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
454 |
> |
public void realRun() throws InterruptedException { |
455 |
> |
threadStarted.countDown(); |
456 |
|
phaser.arriveAndAwaitAdvance(); |
457 |
+ |
advanced.set(true); |
458 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
459 |
+ |
while (!checkedInterruptStatus.get()) |
460 |
+ |
Thread.yield(); |
461 |
|
}}); |
462 |
|
|
463 |
< |
Thread.sleep(SMALL_DELAY_MS); |
464 |
< |
th.interrupt(); |
455 |
< |
Thread.sleep(SMALL_DELAY_MS); |
463 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
464 |
> |
t.interrupt(); |
465 |
|
phaser.arrive(); |
466 |
< |
assertFalse(th.isInterrupted()); |
467 |
< |
th.join(); |
466 |
> |
while (!advanced.get()) |
467 |
> |
Thread.yield(); |
468 |
> |
assertTrue(t.isInterrupted()); |
469 |
> |
checkedInterruptStatus.set(true); |
470 |
> |
awaitTermination(t, SMALL_DELAY_MS); |
471 |
|
} |
472 |
|
|
473 |
|
/** |