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());
}
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());
}
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());
}
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());
}
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();
}
Aggregations