/** * 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(); }
public void run() { DoubleAccumulator offset = new DoubleAccumulator((a,b) -> {return a + b;}, 0.0); shotQueue.removeIf(s -> { offset.accumulate(s.decay()); return s.ticksLeft == 0; }); this.offset = offset; if (shotQueue.isEmpty()) { throw new RuntimeException("All done for now for this player"); } }
static void testDoubleAccumulator() { DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (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"); }
/** * new instance initialized to supplied identity */ public void testConstructor() { for (double identity : new double[] { Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MIN_VALUE, Double.MAX_VALUE, 0.0, }) assertEquals(identity, new DoubleAccumulator(Double::max, identity).get()); }
/** * accumulate accumulates given value to current, and get returns current value */ public void testAccumulateAndGet() { DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); acc.accumulate(2.0); assertEquals(2.0, acc.get()); acc.accumulate(-4.0); assertEquals(2.0, acc.get()); acc.accumulate(4.0); assertEquals(4.0, acc.get()); }
/** * reset() causes subsequent get() to return zero */ public void testReset() { DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); acc.accumulate(2.0); assertEquals(2.0, acc.get()); acc.reset(); assertEquals(0.0, acc.get()); }
/** * getThenReset() returns current value; subsequent get() returns zero */ public void testGetThenReset() { DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); acc.accumulate(2.0); assertEquals(2.0, acc.get()); assertEquals(2.0, acc.getThenReset()); assertEquals(0.0, acc.get()); }
/** * toString returns current value. */ public void testToString() { DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); assertEquals("0.0", acc.toString()); acc.accumulate(1.0); assertEquals(Double.toString(1.0), acc.toString()); }
/** * intValue returns current value. */ public void testIntValue() { DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); assertEquals(0, acc.intValue()); acc.accumulate(1.0); assertEquals(1, acc.intValue()); }
/** * longValue returns current value. */ public void testLongValue() { DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); assertEquals(0, acc.longValue()); acc.accumulate(1.0); assertEquals(1, acc.longValue()); }
/** * floatValue returns current value. */ public void testFloatValue() { DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); assertEquals(0.0f, acc.floatValue()); acc.accumulate(1.0); assertEquals(1.0f, acc.floatValue()); }
/** * doubleValue returns current value. */ public void testDoubleValue() { DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); assertEquals(0.0, acc.doubleValue()); acc.accumulate(1.0); assertEquals(1.0, acc.doubleValue()); }
/** * 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()); }
/** * 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()); }
/** * 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()); }
/** * 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()); }
/** * 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()); }
/** * 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()); }
/** * 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()); }
/** * 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()); }
public void run() { phaser.arriveAndAwaitAdvance(); DoubleAccumulator a = acc; for (int i = 0; i < incs; ++i) a.accumulate(i); result = a.get(); phaser.arrive(); }
/** * Use synchronized keyword and java.util.concurrent.atomic package to * control the order of thread execution */ synchronized private void synchronizedExample() { number = number + number; // Atomic AtomicInteger i = new AtomicInteger(); i.incrementAndGet(); i.getAndIncrement(); i.decrementAndGet(); i.getAndDecrement(); i.getAndAdd(10); i.addAndGet(10); i.updateAndGet(v -> v * 2); i = new AtomicInteger(10); i.accumulateAndGet(10, (a, b) -> a * b); System.out.print("AtomicInteger : "); System.out.println(i); AtomicBoolean ab; AtomicInteger ai; DoubleAdder da; LongAdder la; DoubleAccumulator dac; LongAccumulator lac; }
public Account() { balance = new AtomicLong(); operations = new LongAdder(); commission = new DoubleAccumulator((x,y)-> x+y*0.2, 0); }
/** * default constructed initializes to zero */ public void testConstructor() { DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); assertEquals(0.0, ai.get()); }
AccTask(DoubleAccumulator acc, Phaser phaser, int incs) { this.acc = acc; this.phaser = phaser; this.incs = incs; }