Java8-LongAccumulator

mac2022-06-30  113

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.LongAccumulator; import java.util.function.LongBinaryOperator; import java.util.stream.IntStream; public class LongAccumulator { public static void main(String[] args) { 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()); } }

转载于:https://www.cnblogs.com/bilaisheng/p/10210900.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)