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
This commit is contained in:
Tomasz Lelek 2017-04-28 19:33:06 +02:00 committed by Grzegorz Piwowarek
parent 01cad343f7
commit 9a3df1fe76
1 changed files with 7 additions and 6 deletions

View File

@ -17,25 +17,26 @@ public class LongAccumulatorTest {
public void givenLongAccumulator_whenApplyActionOnItFromMultipleThrads_thenShouldProduceProperResult() throws InterruptedException { public void givenLongAccumulator_whenApplyActionOnItFromMultipleThrads_thenShouldProduceProperResult() throws InterruptedException {
//given //given
ExecutorService executorService = Executors.newFixedThreadPool(8); ExecutorService executorService = Executors.newFixedThreadPool(8);
LongBinaryOperator higherValueFinder = (currentValue, previousValue) -> currentValue > previousValue ? currentValue : previousValue; LongBinaryOperator sum = Long::sum;
LongAccumulator accumulator = new LongAccumulator(higherValueFinder, 0L); LongAccumulator accumulator = new LongAccumulator(sum, 0L);
int numberOfThreads = 4; int numberOfThreads = 4;
int numberOfIncrements = 100; int numberOfIncrements = 100;
//when //when
Runnable accumulateAction = () -> IntStream Runnable accumulateAction = () -> IntStream
.rangeClosed(0, numberOfIncrements) .rangeClosed(0, numberOfIncrements)
.forEach(accumulator::accumulate); .forEach(accumulator::accumulate);
for (int i = 0; i < numberOfThreads; i++) { for (int i = 0; i < numberOfThreads; i++) {
executorService.execute(accumulateAction); executorService.execute(accumulateAction);
} }
//then //then
executorService.awaitTermination(500, TimeUnit.MILLISECONDS); executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
executorService.shutdown(); executorService.shutdown();
assertEquals(accumulator.get(), 20200);
assertEquals(accumulator.get(), 100);
} }
} }