java.util.concurrent.atomic.DoubleAdder

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

1. DoubleAdderTest#testAddAndSumMT()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * 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();
    DoubleAdder a = new DoubleAdder();
    CyclicBarrier barrier = new CyclicBarrier(nthreads + 1);
    for (int i = 0; i < nthreads; ++i) pool.execute(new AdderTask(a, barrier, incs));
    barrier.await();
    barrier.await();
    double total = (long) nthreads * incs;
    double sum = a.sum();
    assertEquals(sum, total);
    pool.shutdown();
}

2. DoubleAdderTest#testSerialization()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * a deserialized serialized adder holds same value
     */
public void testSerialization() throws Exception {
    DoubleAdder x = new DoubleAdder();
    DoubleAdder y = serialClone(x);
    assertNotSame(x, y);
    x.add(-22.0);
    DoubleAdder z = serialClone(x);
    assertEquals(-22.0, x.sum());
    assertEquals(0.0, y.sum());
    assertEquals(-22.0, z.sum());
}

3. Serial#testDoubleAdder()

Project: openjdk
Source File: Serial.java
View license
static void testDoubleAdder() {
    DoubleAdder a = new DoubleAdder();
    a.add(20.1d);
    DoubleAdder result = echo(a);
    if (result.doubleValue() != a.doubleValue())
        throw new RuntimeException("Unexpected doubleValue");
    checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAdder$SerializationProxy");
}

4. DoubleAdderTest#testAddAndSum()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * add adds given value to current, and sum returns current value
     */
public void testAddAndSum() {
    DoubleAdder ai = new DoubleAdder();
    ai.add(2.0);
    assertEquals(2.0, ai.sum());
    ai.add(-4.0);
    assertEquals(-2.0, ai.sum());
}

5. DoubleAdderTest#testReset()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * reset() causes subsequent sum() to return zero
     */
public void testReset() {
    DoubleAdder ai = new DoubleAdder();
    ai.add(2.0);
    assertEquals(2.0, ai.sum());
    ai.reset();
    assertEquals(0.0, ai.sum());
}

6. DoubleAdderTest#testSumThenReset()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * sumThenReset() returns sum; subsequent sum() returns zero
     */
public void testSumThenReset() {
    DoubleAdder ai = new DoubleAdder();
    ai.add(2.0);
    assertEquals(2.0, ai.sum());
    assertEquals(2.0, ai.sumThenReset());
    assertEquals(0.0, ai.sum());
}

7. DoubleAdderTest#testToString()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * toString returns current value.
     */
public void testToString() {
    DoubleAdder ai = new DoubleAdder();
    assertEquals(Double.toString(0.0), ai.toString());
    ai.add(1.0);
    assertEquals(Double.toString(1.0), ai.toString());
}

8. DoubleAdderTest#testIntValue()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * intValue returns current value.
     */
public void testIntValue() {
    DoubleAdder ai = new DoubleAdder();
    assertEquals(0, ai.intValue());
    ai.add(1.0);
    assertEquals(1, ai.intValue());
}

9. DoubleAdderTest#testLongValue()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * longValue returns current value.
     */
public void testLongValue() {
    DoubleAdder ai = new DoubleAdder();
    assertEquals(0, ai.longValue());
    ai.add(1.0);
    assertEquals(1, ai.longValue());
}

10. DoubleAdderTest#testFloatValue()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * floatValue returns current value.
     */
public void testFloatValue() {
    DoubleAdder ai = new DoubleAdder();
    assertEquals(0.0f, ai.floatValue());
    ai.add(1.0);
    assertEquals(1.0f, ai.floatValue());
}

11. DoubleAdderTest#testDoubleValue()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * doubleValue returns current value.
     */
public void testDoubleValue() {
    DoubleAdder ai = new DoubleAdder();
    assertEquals(0.0, ai.doubleValue());
    ai.add(1.0);
    assertEquals(1.0, ai.doubleValue());
}

12. DoubleAdderDemo#adderTest()

Project: openjdk
Source File: DoubleAdderDemo.java
View license
static void adderTest(int nthreads, int incs) {
    System.out.print("DoubleAdder  ");
    Phaser phaser = new Phaser(nthreads + 1);
    DoubleAdder a = new DoubleAdder();
    for (int i = 0; i < nthreads; ++i) pool.execute(new AdderTask(a, phaser, incs));
    report(nthreads, incs, timeTasks(phaser), a.sum());
}

13. DoubleAdderTest#testConstructor()

Project: openjdk
Source File: DoubleAdderTest.java
View license
/**
     * default constructed initializes to zero
     */
public void testConstructor() {
    DoubleAdder ai = new DoubleAdder();
    assertEquals(0.0, ai.sum());
}

14. BufferGaugeServiceSpeedTests#raw()

View license
@Theory
public void raw(String input) throws Exception {
    iterate("writeRaw");
    double rate = number / watch.getLastTaskTimeMillis() * 1000;
    System.err.println("Rate(" + count + ")=" + rate + ", " + watch);
    watch.start("readRaw" + count);
    for (String name : names) {
        this.gauges.forEach(Pattern.compile(name).asPredicate(), new BiConsumer<String, GaugeBuffer>() {

            @Override
            public void accept(String name, GaugeBuffer value) {
                err.println(name + "=" + value);
            }
        });
    }
    final DoubleAdder total = new DoubleAdder();
    this.gauges.forEach(Pattern.compile(".*").asPredicate(), new BiConsumer<String, GaugeBuffer>() {

        @Override
        public void accept(String name, GaugeBuffer value) {
            total.add(value.getValue());
        }
    });
    watch.stop();
    System.err.println("Read(" + count + ")=" + watch.getLastTaskTimeMillis() + "ms");
    assertThat(number * threadCount < total.longValue()).isTrue();
}