Search in sources :

Example 31 with LongAdder

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

the class ThreadLocalRandomTest method testUnsizedLongsCountSeq.

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

Example 32 with LongAdder

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

the class ThreadLocalRandomTest method testUnsizedIntsCountSeq.

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

Example 33 with LongAdder

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

the class LongAdderTest method testToString.

/**
 * toString returns current value.
 */
public void testToString() {
    LongAdder ai = new LongAdder();
    assertEquals("0", ai.toString());
    ai.increment();
    assertEquals(Long.toString(1), ai.toString());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder)

Example 34 with LongAdder

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

the class LongAdderTest method testFloatValue.

/**
 * floatValue returns current value.
 */
public void testFloatValue() {
    LongAdder ai = new LongAdder();
    assertEquals(0.0f, ai.floatValue());
    ai.increment();
    assertEquals(1.0f, ai.floatValue());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder)

Example 35 with LongAdder

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

the class LongAdderTest method testSumThenReset.

/**
 * sumThenReset() returns sum; subsequent sum() returns zero
 */
public void testSumThenReset() {
    LongAdder ai = new LongAdder();
    ai.add(2);
    assertEquals(2, ai.sum());
    assertEquals(2, ai.sumThenReset());
    assertEquals(0, ai.sum());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder)

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