use of java8.util.concurrent.atomic.DoubleAccumulator in project streamsupport by stefan-zobel.
the class DoubleAccumulatorTest method testFloatValue.
/**
* floatValue returns current value.
*/
public void testFloatValue() {
DoubleAccumulator acc = new DoubleAccumulator(Doubles::max, 0.0);
assertEquals(0.0f, acc.floatValue());
acc.accumulate(1.0);
assertEquals(1.0f, acc.floatValue());
}
use of java8.util.concurrent.atomic.DoubleAccumulator in project streamsupport by stefan-zobel.
the class DoubleAccumulatorTest method testGetThenReset.
/**
* getThenReset() returns current value; subsequent get() returns zero
*/
public void testGetThenReset() {
DoubleAccumulator acc = new DoubleAccumulator(Doubles::max, 0.0);
acc.accumulate(2.0);
assertEquals(2.0, acc.get());
assertEquals(2.0, acc.getThenReset());
assertEquals(0.0, acc.get());
}
use of java8.util.concurrent.atomic.DoubleAccumulator in project streamsupport by stefan-zobel.
the class DoubleAccumulatorTest method testReset.
/**
* reset() causes subsequent get() to return zero
*/
public void testReset() {
DoubleAccumulator acc = new DoubleAccumulator(Doubles::max, 0.0);
acc.accumulate(2.0);
assertEquals(2.0, acc.get());
acc.reset();
assertEquals(0.0, acc.get());
}
use of java8.util.concurrent.atomic.DoubleAccumulator 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.atomic.DoubleAccumulator in project streamsupport by stefan-zobel.
the class DoubleAccumulatorTest method testIntValue.
/**
* intValue returns current value.
*/
public void testIntValue() {
DoubleAccumulator acc = new DoubleAccumulator(Doubles::max, 0.0);
assertEquals(0, acc.intValue());
acc.accumulate(1.0);
assertEquals(1, acc.intValue());
}
Aggregations