Search in sources :

Example 1 with LongAccumulator

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());
}
Also used : IntStream(java.util.stream.IntStream) LongAccumulator(java.util.concurrent.atomic.LongAccumulator) LongBinaryOperator(java.util.function.LongBinaryOperator) ExecutorService(java.util.concurrent.ExecutorService) Executors(java.util.concurrent.Executors) LongAccumulator(java.util.concurrent.atomic.LongAccumulator) LongBinaryOperator(java.util.function.LongBinaryOperator) ExecutorService(java.util.concurrent.ExecutorService)

Example 2 with LongAccumulator

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");
}
Also used : LongAccumulator(java.util.concurrent.atomic.LongAccumulator) LongBinaryOperator(java.util.function.LongBinaryOperator)

Aggregations

LongAccumulator (java.util.concurrent.atomic.LongAccumulator)2 LongBinaryOperator (java.util.function.LongBinaryOperator)2 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 IntStream (java.util.stream.IntStream)1