From 9a3df1fe7675868415d6f627913950e03273f514 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 28 Apr 2017 19:33:06 +0200 Subject: [PATCH] Bael 856 long adder (#1748) * BAEL-856 code for long adder and accumulator * BAEL-856 rearange packages * BAEL-856 Formatting * BAEL-850 accumulator accumulates values * BAEL-881 use Long::sum --- .../concurrent/accumulator/LongAccumulatorTest.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorTest.java b/core-java/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorTest.java index 46e107b607..3a0092cf24 100644 --- a/core-java/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorTest.java +++ b/core-java/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorTest.java @@ -17,25 +17,26 @@ public class LongAccumulatorTest { public void givenLongAccumulator_whenApplyActionOnItFromMultipleThrads_thenShouldProduceProperResult() throws InterruptedException { //given ExecutorService executorService = Executors.newFixedThreadPool(8); - LongBinaryOperator higherValueFinder = (currentValue, previousValue) -> currentValue > previousValue ? currentValue : previousValue; - LongAccumulator accumulator = new LongAccumulator(higherValueFinder, 0L); - + LongBinaryOperator sum = Long::sum; + LongAccumulator accumulator = new LongAccumulator(sum, 0L); int numberOfThreads = 4; int numberOfIncrements = 100; //when Runnable accumulateAction = () -> IntStream - .rangeClosed(0, numberOfIncrements) - .forEach(accumulator::accumulate); + .rangeClosed(0, numberOfIncrements) + .forEach(accumulator::accumulate); for (int i = 0; i < numberOfThreads; i++) { executorService.execute(accumulateAction); } + //then executorService.awaitTermination(500, TimeUnit.MILLISECONDS); executorService.shutdown(); + assertEquals(accumulator.get(), 20200); + - assertEquals(accumulator.get(), 100); } }