Skip to content

Commit 9a3df1f

Browse files
tomekl007pivovarit
authored andcommitted
Bael 856 long adder (eugenp#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
1 parent 01cad34 commit 9a3df1f

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎core-java/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorTest.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@ public class LongAccumulatorTest {
1717
public void givenLongAccumulator_whenApplyActionOnItFromMultipleThrads_thenShouldProduceProperResult() throws InterruptedException {
1818
//given
1919
ExecutorService executorService = Executors.newFixedThreadPool(8);
20-
LongBinaryOperator higherValueFinder = (currentValue, previousValue) -> currentValue > previousValue ? currentValue : previousValue;
21-
LongAccumulator accumulator = new LongAccumulator(higherValueFinder, 0L);
22-
20+
LongBinaryOperator sum = Long::sum;
21+
LongAccumulator accumulator = new LongAccumulator(sum, 0L);
2322
int numberOfThreads = 4;
2423
int numberOfIncrements = 100;
2524

2625
//when
2726
Runnable accumulateAction = () -> IntStream
28-
.rangeClosed(0, numberOfIncrements)
29-
.forEach(accumulator::accumulate);
27+
.rangeClosed(0, numberOfIncrements)
28+
.forEach(accumulator::accumulate);
3029

3130
for (int i = 0; i < numberOfThreads; i++) {
3231
executorService.execute(accumulateAction);
3332
}
3433

34+
3535
//then
3636
executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
3737
executorService.shutdown();
38+
assertEquals(accumulator.get(), 20200);
39+
3840

39-
assertEquals(accumulator.get(), 100);
4041
}
4142
}

0 commit comments

Comments
 (0)