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

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 ( ) ; }
adds by multiple threads produce correct sum
