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 |
|
/** |
360 |
|
phaseCount.incrementAndGet(); |
361 |
|
sleepTillInterrupted(SMALL_DELAY_MS); |
362 |
|
phaser.awaitAdvance(phase); |
363 |
< |
threadAssertTrue(phaseCount.get() == 4); |
363 |
> |
assertEquals(phaseCount.get(), 4); |
364 |
|
}})); |
365 |
|
} |
366 |
|
for (Thread thread : threads) |
377 |
|
phaser.register(); |
378 |
|
List<Thread> threads = new ArrayList<Thread>(); |
379 |
|
for (int i = 0; i < 8; i++) { |
380 |
+ |
final CountDownLatch latch = new CountDownLatch(1); |
381 |
+ |
final boolean goesFirst = ((i & 1) == 0); |
382 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
383 |
< |
public void realRun() { |
384 |
< |
sleepTillInterrupted(SHORT_DELAY_MS); |
383 |
> |
public void realRun() throws InterruptedException { |
384 |
> |
if (goesFirst) |
385 |
> |
latch.countDown(); |
386 |
> |
else |
387 |
> |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
388 |
|
phaser.arrive(); |
389 |
|
}})); |
390 |
+ |
if (goesFirst) |
391 |
+ |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
392 |
+ |
else |
393 |
+ |
latch.countDown(); |
394 |
|
phase = phaser.awaitAdvance(phaser.arrive()); |
395 |
< |
threadAssertEquals(phase, phaser.getPhase()); |
395 |
> |
assertEquals(phase, phaser.getPhase()); |
396 |
|
} |
397 |
|
for (Thread thread : threads) |
398 |
< |
thread.join(); |
398 |
> |
awaitTermination(thread, SMALL_DELAY_MS); |
399 |
|
} |
400 |
|
|
401 |
|
/** |
403 |
|
*/ |
404 |
|
public void testAwaitAdvance6() throws InterruptedException { |
405 |
|
final Phaser phaser = new Phaser(3); |
406 |
< |
/* |
407 |
< |
* Start new thread. This thread waits a small amount of time |
408 |
< |
* and waits for the other two parties to arrive. The party |
409 |
< |
* in the main thread arrives quickly so at best this thread |
410 |
< |
* waits for the second thread's party to arrive |
411 |
< |
*/ |
412 |
< |
Thread t1 = newStartedThread(new CheckedRunnable() { |
413 |
< |
public void realRun() { |
414 |
< |
sleepTillInterrupted(SMALL_DELAY_MS); |
415 |
< |
int phase = phaser.awaitAdvance(phaser.arrive()); |
416 |
< |
/* |
417 |
< |
* This point is reached when force termination is called in which phase = -1 |
418 |
< |
*/ |
419 |
< |
threadAssertTrue(phase < 0); |
420 |
< |
threadAssertTrue(phaser.isTerminated()); |
410 |
< |
}}); |
411 |
< |
/* |
412 |
< |
* This thread will cause the first thread run to wait, in doing so |
413 |
< |
* the main thread will force termination in which the first thread |
414 |
< |
* should exit peacefully as this one |
415 |
< |
*/ |
416 |
< |
Thread t2 = newStartedThread(new CheckedRunnable() { |
417 |
< |
public void realRun() { |
418 |
< |
sleepTillInterrupted(MEDIUM_DELAY_MS); |
419 |
< |
int p1 = phaser.arrive(); |
420 |
< |
int phase = phaser.awaitAdvance(p1); |
421 |
< |
threadAssertTrue(phase < 0); |
422 |
< |
threadAssertTrue(phaser.isTerminated()); |
423 |
< |
}}); |
424 |
< |
|
425 |
< |
phaser.arrive(); |
406 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
407 |
> |
final List<Thread> threads = new ArrayList<Thread>(); |
408 |
> |
for (int i = 0; i < 2; i++) { |
409 |
> |
Runnable r = new CheckedRunnable() { |
410 |
> |
public void realRun() { |
411 |
> |
int p1 = phaser.arrive(); |
412 |
> |
assertTrue(p1 >= 0); |
413 |
> |
threadsStarted.countDown(); |
414 |
> |
int phase = phaser.awaitAdvance(p1); |
415 |
> |
assertTrue(phase < 0); |
416 |
> |
assertTrue(phaser.isTerminated()); |
417 |
> |
}}; |
418 |
> |
threads.add(newStartedThread(r)); |
419 |
> |
} |
420 |
> |
threadsStarted.await(); |
421 |
|
phaser.forceTermination(); |
422 |
< |
t1.join(); |
423 |
< |
t2.join(); |
422 |
> |
for (Thread thread : threads) |
423 |
> |
awaitTermination(thread, SMALL_DELAY_MS); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
440 |
|
*/ |
441 |
|
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
442 |
|
final Phaser phaser = new Phaser(2); |
443 |
< |
Thread th = newStartedThread(new CheckedRunnable() { |
444 |
< |
public void realRun() { |
443 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
444 |
> |
final AtomicBoolean advanced = new AtomicBoolean(false); |
445 |
> |
final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false); |
446 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
447 |
> |
public void realRun() throws InterruptedException { |
448 |
> |
threadStarted.countDown(); |
449 |
|
phaser.arriveAndAwaitAdvance(); |
450 |
+ |
advanced.set(true); |
451 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
452 |
+ |
while (!checkedInterruptStatus.get()) |
453 |
+ |
Thread.yield(); |
454 |
|
}}); |
455 |
|
|
456 |
< |
Thread.sleep(SMALL_DELAY_MS); |
457 |
< |
th.interrupt(); |
455 |
< |
Thread.sleep(SMALL_DELAY_MS); |
456 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
457 |
> |
t.interrupt(); |
458 |
|
phaser.arrive(); |
459 |
< |
assertFalse(th.isInterrupted()); |
460 |
< |
th.join(); |
459 |
> |
while (!advanced.get()) |
460 |
> |
Thread.yield(); |
461 |
> |
assertTrue(t.isInterrupted()); |
462 |
> |
checkedInterruptStatus.set(true); |
463 |
> |
awaitTermination(t, SMALL_DELAY_MS); |
464 |
|
} |
465 |
|
|
466 |
|
/** |
470 |
|
*/ |
471 |
|
public void testArriveAndAwaitAdvance3() throws InterruptedException { |
472 |
|
final Phaser phaser = new Phaser(1); |
468 |
– |
final AtomicInteger arrivingCount = new AtomicInteger(0); |
473 |
|
final List<Thread> threads = new ArrayList<Thread>(); |
474 |
< |
for (int i = 0; i < 6; i++) { |
474 |
> |
for (int i = 0; i < 3; i++) { |
475 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
476 |
< |
public void realRun() throws InterruptedException { |
477 |
< |
phaser.register(); |
478 |
< |
sleepTillInterrupted(SHORT_DELAY_MS); |
479 |
< |
arrivingCount.getAndIncrement(); |
476 |
< |
phaser.arrive(); |
477 |
< |
}})); |
476 |
> |
public void realRun() throws InterruptedException { |
477 |
> |
phaser.register(); |
478 |
> |
phaser.arriveAndAwaitAdvance(); |
479 |
> |
}})); |
480 |
|
} |
481 |
< |
int phaseNumber = phaser.arriveAndAwaitAdvance(); |
482 |
< |
arrivingCount.incrementAndGet(); |
483 |
< |
//the + 1 adds to expectedArrive to account for the main threads arrival |
482 |
< |
int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1; |
483 |
< |
threadAssertEquals(expectedArrived, arrivingCount.get()); |
481 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
482 |
> |
assertEquals(phaser.getArrivedParties(), 3); |
483 |
> |
phaser.arriveAndAwaitAdvance(); |
484 |
|
for (Thread thread : threads) |
485 |
|
thread.join(); |
486 |
|
} |