use of java.util.concurrent.atomic.LongAccumulator in project java8-tutorial by winterbe.
the class LongAccumulator1 method testAccumulate.
private static void testAccumulate() {
LongBinaryOperator op = (x, y) -> 2 * x + y;
LongAccumulator accumulator = new LongAccumulator(op, 1L);
ExecutorService executor = Executors.newFixedThreadPool(2);
IntStream.range(0, 10).forEach(i -> executor.submit(() -> accumulator.accumulate(i)));
ConcurrentUtils.stop(executor);
System.out.format("Add: %d\n", accumulator.getThenReset());
}
use of java.util.concurrent.atomic.LongAccumulator in project jdk8u_jdk by JetBrains.
the class Serial method testLongAccumulator.
static void testLongAccumulator() {
LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
LongAccumulator a = new LongAccumulator(plus, -2);
a.accumulate(34);
LongAccumulator 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.LongAccumulator$SerializationProxy");
}
Aggregations