use of java8.util.concurrent.atomic.LongAccumulator in project streamsupport by stefan-zobel.
the class LongAccumulatorTest method testDoubleValue.
/**
* doubleValue returns current value.
*/
public void testDoubleValue() {
LongAccumulator acc = new LongAccumulator(Longs::max, 0L);
assertEquals(0.0, acc.doubleValue());
acc.accumulate(1);
assertEquals(1.0, acc.doubleValue());
}
use of java8.util.concurrent.atomic.LongAccumulator in project streamsupport by stefan-zobel.
the class LongAccumulatorTest method testIntValue.
/**
* intValue returns current value.
*/
public void testIntValue() {
LongAccumulator acc = new LongAccumulator(Longs::max, 0L);
assertEquals(0, acc.intValue());
acc.accumulate(1);
assertEquals(1, acc.intValue());
}
use of java8.util.concurrent.atomic.LongAccumulator in project streamsupport by stefan-zobel.
the class LongAccumulatorTest method testAccumulateAndGetMT.
/**
* accumulates by multiple threads produce correct result
*/
public void testAccumulateAndGetMT() {
final LongAccumulator acc = new LongAccumulator((x, y) -> x + y, 0L);
final int nThreads = ThreadLocalRandom.current().nextInt(1, 5);
final Phaser phaser = new Phaser(nThreads + 1);
final int incs = 1_000_000;
// Gauss
final long total = nThreads * incs / 2L * (incs - 1);
final Runnable task = () -> {
phaser.arriveAndAwaitAdvance();
for (int i = 0; i < incs; i++) {
acc.accumulate((long) 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.LongAccumulator in project streamsupport by stefan-zobel.
the class LongAccumulatorTest method testLongValue.
/**
* longValue returns current value.
*/
public void testLongValue() {
LongAccumulator acc = new LongAccumulator(Longs::max, 0L);
assertEquals(0, acc.longValue());
acc.accumulate(1);
assertEquals(1, acc.longValue());
}
use of java8.util.concurrent.atomic.LongAccumulator in project streamsupport by stefan-zobel.
the class LongAccumulatorTest method testGetThenReset.
/**
* getThenReset() returns current value; subsequent get() returns zero
*/
public void testGetThenReset() {
LongAccumulator acc = new LongAccumulator(Longs::max, 0L);
acc.accumulate(2);
assertEquals(2, acc.get());
assertEquals(2, acc.getThenReset());
assertEquals(0, acc.get());
}
Aggregations