Search in sources :

Example 6 with DoubleAdder

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

the class DoubleAdderTest method testFloatValue.

/**
 * 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());
}
Also used : DoubleAdder(java8.util.concurrent.atomic.DoubleAdder)

Example 7 with DoubleAdder

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

the class DoubleAdderTest method testToString.

/**
 * 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());
}
Also used : DoubleAdder(java8.util.concurrent.atomic.DoubleAdder)

Example 8 with DoubleAdder

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

the class DoubleAdderTest method testSerialization.

/**
 * a deserialized/reserialized 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());
}
Also used : DoubleAdder(java8.util.concurrent.atomic.DoubleAdder)

Example 9 with DoubleAdder

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

the class DoubleAdderTest method testAddAndSum.

/**
 * 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());
}
Also used : DoubleAdder(java8.util.concurrent.atomic.DoubleAdder)

Example 10 with DoubleAdder

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

the class DoubleAdderTest method testAddAndSumMT.

/**
 * 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();
}
Also used : DoubleAdder(java8.util.concurrent.atomic.DoubleAdder) ExecutorService(java.util.concurrent.ExecutorService) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

DoubleAdder (java8.util.concurrent.atomic.DoubleAdder)12 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ExecutorService (java.util.concurrent.ExecutorService)1 Phaser (java8.util.concurrent.Phaser)1