java.util.concurrent.atomic.LongAccumulator

Here are the examples of the java api class java.util.concurrent.atomic.LongAccumulator taken from open source projects.

1. Serial#testLongAccumulator()

Project: openjdk
Source File: Serial.java
View license
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperatorSerializable & ) ( x,  y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");
    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}

2. LongAccumulator1#testAccumulate()

View license
private static void testAccumulate() {
    LongBinaryOperator op = ( x,  y) -> 2 * x + y;
    LongAccumulator accumulator = new LongAccumulator(op, 1L);
    ExecutorService executor = Executors.newFixedThreadPool(2);
    IntStream.range(0, 10).forEach( i -> executor.submit(() -> accumulator.accumulate(i)));
    ConcurrentUtils.stop(executor);
    System.out.format("Add: %d\n", accumulator.getThenReset());
}

3. LongAccumulatorTest#testAccumulateAndGetMT()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * accumulates by multiple threads produce correct result
     */
public void testAccumulateAndGetMT() {
    final int incs = 1000000;
    final int nthreads = 4;
    final ExecutorService pool = Executors.newCachedThreadPool();
    LongAccumulator a = new LongAccumulator(Long::max, 0L);
    Phaser phaser = new Phaser(nthreads + 1);
    for (int i = 0; i < nthreads; ++i) pool.execute(new AccTask(a, phaser, incs));
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    long expected = incs - 1;
    long result = a.get();
    assertEquals(expected, result);
    pool.shutdown();
}

4. LongAccumulatorTest#testAccumulateAndGet()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * accumulate accumulates given value to current, and get returns current value
     */
public void testAccumulateAndGet() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    ai.accumulate(-4);
    assertEquals(2, ai.get());
    ai.accumulate(4);
    assertEquals(4, ai.get());
}

5. LongAccumulatorTest#testReset()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * reset() causes subsequent get() to return zero
     */
public void testReset() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    ai.reset();
    assertEquals(0, ai.get());
}

6. LongAccumulatorTest#testGetThenReset()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * getThenReset() returns current value; subsequent get() returns zero
     */
public void testGetThenReset() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    assertEquals(2, ai.getThenReset());
    assertEquals(0, ai.get());
}

7. LongAccumulatorTest#testToString()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * toString returns current value.
     */
public void testToString() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals("0", ai.toString());
    ai.accumulate(1);
    assertEquals(Long.toString(1), ai.toString());
}

8. LongAccumulatorTest#testIntValue()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * intValue returns current value.
     */
public void testIntValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0, ai.intValue());
    ai.accumulate(1);
    assertEquals(1, ai.intValue());
}

9. LongAccumulatorTest#testLongValue()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * longValue returns current value.
     */
public void testLongValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0, ai.longValue());
    ai.accumulate(1);
    assertEquals(1, ai.longValue());
}

10. LongAccumulatorTest#testFloatValue()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * floatValue returns current value.
     */
public void testFloatValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0.0f, ai.floatValue());
    ai.accumulate(1);
    assertEquals(1.0f, ai.floatValue());
}

11. LongAccumulatorTest#testDoubleValue()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * doubleValue returns current value.
     */
public void testDoubleValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0.0, ai.doubleValue());
    ai.accumulate(1);
    assertEquals(1.0, ai.doubleValue());
}

12. LongAccumulatorTest#testConstructor()

Project: openjdk
Source File: LongAccumulatorTest.java
View license
/**
     * default constructed initializes to zero
     */
public void testConstructor() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0, ai.get());
}