Search in sources :

Example 6 with LongAdder

use of java8.util.concurrent.atomic.LongAdder in project streamsupport by stefan-zobel.

the class LongAdderTest method testAddAndSumMT.

/**
 * adds by multiple threads produce correct sum
 */
public void testAddAndSumMT() throws Throwable {
    final int incs = 1000000;
    final int nthreads = 4;
    final ExecutorService pool = Executors.newCachedThreadPool();
    LongAdder a = new LongAdder();
    CyclicBarrier barrier = new CyclicBarrier(nthreads + 1);
    for (int i = 0; i < nthreads; ++i) pool.execute(new AdderTask(a, barrier, incs));
    barrier.await();
    barrier.await();
    long total = (long) nthreads * incs;
    long sum = a.sum();
    assertEquals(sum, total);
    pool.shutdown();
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ExecutorService(java.util.concurrent.ExecutorService) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 7 with LongAdder

use of java8.util.concurrent.atomic.LongAdder in project streamsupport by stefan-zobel.

the class LongAdderTest method testAddAndSum.

/**
 * add adds given value to current, and sum returns current value
 */
public void testAddAndSum() {
    LongAdder ai = new LongAdder();
    ai.add(2);
    assertEquals(2, ai.sum());
    ai.add(-4);
    assertEquals(-2, ai.sum());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder)

Example 8 with LongAdder

use of java8.util.concurrent.atomic.LongAdder in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testIntsCount.

/**
 * A parallel sized stream of ints generates the given number of values
 */
public void testIntsCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.ints(size).parallel().forEach(x -> counter.increment());
        assertEquals(size, counter.sum());
        size += 524959;
    }
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

Example 9 with LongAdder

use of java8.util.concurrent.atomic.LongAdder in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testUnsizedLongsCountSeq.

/**
 * A sequential unsized stream of longs generates at least 100 values
 */
public void testUnsizedLongsCountSeq() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.longs().limit(size).forEach(x -> counter.increment());
    assertEquals(size, counter.sum());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

Example 10 with LongAdder

use of java8.util.concurrent.atomic.LongAdder in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testLongsCount.

/**
 * A parallel sized stream of longs generates the given number of values
 */
public void testLongsCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.longs(size).parallel().forEach(x -> counter.increment());
        assertEquals(size, counter.sum());
        size += 524959;
    }
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

Aggregations

LongAdder (java8.util.concurrent.atomic.LongAdder)50 SplittableRandom (java8.util.SplittableRandom)18 ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)18 DoubleConsumer (java8.util.function.DoubleConsumer)3 IntConsumer (java8.util.function.IntConsumer)3 LongConsumer (java8.util.function.LongConsumer)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ExecutorService (java.util.concurrent.ExecutorService)1 Phaser (java8.util.concurrent.Phaser)1