use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class DoubleAccumulatorTest method testAccumulateAndGetMT.
/**
* accumulates by multiple threads produce correct result
*/
public void testAccumulateAndGetMT() {
final DoubleAccumulator acc = new DoubleAccumulator((x, y) -> x + y, 0.0);
final int nThreads = ThreadLocalRandom.current().nextInt(1, 5);
final Phaser phaser = new Phaser(nThreads + 1);
final int incs = 1_000_000;
// Gauss
final double total = nThreads * incs / 2.0 * (incs - 1);
final Runnable task = () -> {
phaser.arriveAndAwaitAdvance();
for (int i = 0; i < incs; i++) {
acc.accumulate((double) i);
assertTrue(acc.get() <= total);
}
phaser.arrive();
};
final ExecutorService p = Executors.newCachedThreadPool();
PoolCleaner cleaner = null;
try {
cleaner = cleaner(p);
for (int i = nThreads; i-- > 0; ) /**/
p.execute(task);
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
assertEquals(total, acc.get());
} finally {
if (cleaner != null) {
cleaner.close();
}
}
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testArriveAndDeregister4.
/**
* arriveAndDeregister deregisters one party from its parent when
* the number of parties of child is zero after deregistration
*/
public void testArriveAndDeregister4() {
Phaser parent = new Phaser();
Phaser child = new Phaser(parent);
assertEquals(0, parent.register());
assertEquals(0, child.register());
assertState(child, 0, 1, 1);
assertState(parent, 0, 2, 2);
assertEquals(0, child.arriveAndDeregister());
assertState(child, 0, 0, 0);
assertState(parent, 0, 1, 1);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testPhaseIncrement1.
/**
* the phase number increments correctly when tripping the barrier
*/
public void testPhaseIncrement1() {
for (int size = 1; size < nine; size++) {
final Phaser phaser = new Phaser(size);
for (int index = 0; index <= (1 << size); index++) {
int phase = phaser.arrive();
assertTrue(index % size == 0 ? (index / size) == phase : index - (phase * size) > 0);
}
}
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testArrive3.
/**
* arrive() returns a negative number if the Phaser is terminated
*/
public void testArrive3() {
Phaser phaser = new Phaser(1);
phaser.forceTermination();
assertTerminated(phaser, 0, 1);
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
assertTrue(phaser.arrive() < 0);
assertTrue(phaser.register() < 0);
assertTrue(phaser.arriveAndDeregister() < 0);
assertTrue(phaser.awaitAdvance(1) < 0);
assertTrue(phaser.getPhase() < 0);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testAwaitAdvance6.
/**
* awaitAdvance returns when the phaser is externally terminated
*/
public void testAwaitAdvance6() {
final Phaser phaser = new Phaser(3);
final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
final List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 2; i++) {
Runnable r = new CheckedRunnable() {
public void realRun() {
assertEquals(0, phaser.arrive());
pleaseForceTermination.countDown();
assertTrue(phaser.awaitAdvance(0) < 0);
assertTrue(phaser.isTerminated());
assertTrue(phaser.getPhase() < 0);
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
assertEquals(3, phaser.getRegisteredParties());
}
};
threads.add(newStartedThread(r));
}
await(pleaseForceTermination);
phaser.forceTermination();
assertTrue(phaser.isTerminated());
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
for (Thread thread : threads) awaitTermination(thread);
assertEquals(3, phaser.getRegisteredParties());
}
Aggregations