Search in sources :

Example 11 with LongAdder

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

the class SplittableRandomTest method testDoublesCount.

/**
 * A parallel sized stream of doubles generates the given number of values
 */
public void testDoublesCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.doubles(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 12 with LongAdder

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

the class SplittableRandomTest method testUnsizedIntsCountSeq.

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

Example 13 with LongAdder

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

the class ThreadLocalRandom8Test method testIntsCount.

/**
 * A parallel sized stream of ints generates the given number of values
 */
public void testIntsCount() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    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) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 14 with LongAdder

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

the class LongAdderDemo method adderTest.

static void adderTest(int nthreads, int incs) {
    System.out.print("LongAdder  ");
    Phaser phaser = new Phaser(nthreads + 1);
    LongAdder a = new LongAdder();
    for (int i = 0; i < nthreads; ++i) pool.execute(new AdderTask(a, phaser, incs));
    report(nthreads, incs, timeTasks(phaser), a.sum());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) Phaser(java8.util.concurrent.Phaser)

Example 15 with LongAdder

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

the class SplittableRandomTest method testDoublesCount.

/**
 * A parallel sized stream of doubles generates the given number of values
 */
public void testDoublesCount() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.doubles(size).parallel().forEach(new DoubleConsumer() {

            @Override
            public void accept(double x) {
                counter.increment();
            }
        });
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
Also used : DoubleConsumer(java8.util.function.DoubleConsumer) 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