Search in sources :

Example 1 with LongAccumulator

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

Example 2 with LongAccumulator

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

Example 3 with LongAccumulator

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

Example 4 with LongAccumulator

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

Example 5 with LongAccumulator

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

Aggregations

LongAccumulator (java8.util.concurrent.atomic.LongAccumulator)9 Longs (java8.lang.Longs)8 ExecutorService (java.util.concurrent.ExecutorService)1 Phaser (java8.util.concurrent.Phaser)1