Search in sources :

Example 1 with DoubleAccumulator

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());
}
Also used : Doubles(java8.lang.Doubles) DoubleAccumulator(java8.util.concurrent.atomic.DoubleAccumulator)

Example 2 with DoubleAccumulator

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());
}
Also used : Doubles(java8.lang.Doubles) DoubleAccumulator(java8.util.concurrent.atomic.DoubleAccumulator)

Example 3 with DoubleAccumulator

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());
}
Also used : Doubles(java8.lang.Doubles) DoubleAccumulator(java8.util.concurrent.atomic.DoubleAccumulator)

Example 4 with DoubleAccumulator

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();
        }
    }
}
Also used : DoubleAccumulator(java8.util.concurrent.atomic.DoubleAccumulator) ExecutorService(java.util.concurrent.ExecutorService) Phaser(java8.util.concurrent.Phaser)

Example 5 with DoubleAccumulator

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());
}
Also used : Doubles(java8.lang.Doubles) DoubleAccumulator(java8.util.concurrent.atomic.DoubleAccumulator)

Aggregations

DoubleAccumulator (java8.util.concurrent.atomic.DoubleAccumulator)9 Doubles (java8.lang.Doubles)8 ExecutorService (java.util.concurrent.ExecutorService)1 Phaser (java8.util.concurrent.Phaser)1