java.util.concurrent.atomic.DoubleAccumulator

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

1. Serial#testDoubleAccumulator()

Project: openjdk
Source File: Serial.java
View license
static void testDoubleAccumulator() {
    DoubleBinaryOperator plus = (DoubleBinaryOperatorSerializable & ) ( x,  y) -> x + y;
    DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
    a.accumulate(17.5d);
    DoubleAccumulator 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.DoubleAccumulator$SerializationProxy");
}

2. DoubleAccumulatorTest#testAccumulateAndGetMT()

Project: openjdk
Source File: DoubleAccumulatorTest.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();
    DoubleAccumulator a = new DoubleAccumulator(Double::max, 0.0);
    Phaser phaser = new Phaser(nthreads + 1);
    for (int i = 0; i < nthreads; ++i) pool.execute(new AccTask(a, phaser, incs));
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    double expected = incs - 1;
    double result = a.get();
    assertEquals(expected, result);
    pool.shutdown();
}

3. DoubleAccumulatorTest#testAccumulateAndGet()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * accumulate accumulates given value to current, and get returns current value
     */
public void testAccumulateAndGet() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    ai.accumulate(2.0);
    assertEquals(2.0, ai.get());
    ai.accumulate(-4.0);
    assertEquals(2.0, ai.get());
    ai.accumulate(4.0);
    assertEquals(4.0, ai.get());
}

4. DoubleAccumulatorTest#testReset()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * reset() causes subsequent get() to return zero
     */
public void testReset() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    ai.accumulate(2.0);
    assertEquals(2.0, ai.get());
    ai.reset();
    assertEquals(0.0, ai.get());
}

5. DoubleAccumulatorTest#testGetThenReset()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * getThenReset() returns current value; subsequent get() returns zero
     */
public void testGetThenReset() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    ai.accumulate(2.0);
    assertEquals(2.0, ai.get());
    assertEquals(2.0, ai.getThenReset());
    assertEquals(0.0, ai.get());
}

6. DoubleAccumulatorTest#testToString()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * toString returns current value.
     */
public void testToString() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    assertEquals("0.0", ai.toString());
    ai.accumulate(1.0);
    assertEquals(Double.toString(1.0), ai.toString());
}

7. DoubleAccumulatorTest#testIntValue()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * intValue returns current value.
     */
public void testIntValue() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    assertEquals(0, ai.intValue());
    ai.accumulate(1.0);
    assertEquals(1, ai.intValue());
}

8. DoubleAccumulatorTest#testLongValue()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * longValue returns current value.
     */
public void testLongValue() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    assertEquals(0, ai.longValue());
    ai.accumulate(1.0);
    assertEquals(1, ai.longValue());
}

9. DoubleAccumulatorTest#testFloatValue()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * floatValue returns current value.
     */
public void testFloatValue() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    assertEquals(0.0f, ai.floatValue());
    ai.accumulate(1.0);
    assertEquals(1.0f, ai.floatValue());
}

10. DoubleAccumulatorTest#testDoubleValue()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * doubleValue returns current value.
     */
public void testDoubleValue() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    assertEquals(0.0, ai.doubleValue());
    ai.accumulate(1.0);
    assertEquals(1.0, ai.doubleValue());
}

11. DoubleAccumulatorTest#testConstructor()

Project: openjdk
Source File: DoubleAccumulatorTest.java
View license
/**
     * default constructed initializes to zero
     */
public void testConstructor() {
    DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
    assertEquals(0.0, ai.get());
}