Skip to content

Commit f979a40

Browse files
tomekl007pedja4
authored andcommitted
BAEL-856 code for long adder and accumulator (eugenp#1723)
* BAEL-856 code for long adder and accumulator * BAEL-856 rearange packages * BAEL-856 Formatting
1 parent b0e15fc commit f979a40

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.concurrent.accumulator;
2+
3+
import org.junit.Test;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.atomic.LongAccumulator;
9+
import java.util.function.LongBinaryOperator;
10+
import java.util.stream.IntStream;
11+
12+
import static junit.framework.TestCase.assertEquals;
13+
14+
public class LongAccumulatorTest {
15+
16+
@Test
17+
public void givenLongAccumulator_whenApplyActionOnItFromMultipleThrads_thenShouldProduceProperResult() throws InterruptedException {
18+
//given
19+
ExecutorService executorService = Executors.newFixedThreadPool(8);
20+
LongBinaryOperator higherValueFinder = (currentValue, previousValue) -> currentValue > previousValue ? currentValue : previousValue;
21+
LongAccumulator accumulator = new LongAccumulator(higherValueFinder, 0L);
22+
23+
int numberOfThreads = 4;
24+
int numberOfIncrements = 100;
25+
26+
//when
27+
Runnable accumulateAction = () -> IntStream
28+
.rangeClosed(0, numberOfIncrements)
29+
.forEach(accumulator::accumulate);
30+
31+
for (int i = 0; i < numberOfThreads; i++) {
32+
executorService.execute(accumulateAction);
33+
}
34+
35+
//then
36+
executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
37+
executorService.shutdown();
38+
39+
assertEquals(accumulator.get(), 100);
40+
}
41+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.concurrent.adder;
2+
3+
import org.junit.Test;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.atomic.LongAdder;
9+
import java.util.stream.IntStream;
10+
11+
import static junit.framework.TestCase.assertEquals;
12+
13+
public class LongAdderTest {
14+
@Test
15+
public void givenMultipleThread_whenTheyWriteToSharedLongAdder_thenShouldCalculateSumForThem() throws InterruptedException {
16+
//given
17+
LongAdder counter = new LongAdder();
18+
ExecutorService executorService = Executors.newFixedThreadPool(8);
19+
20+
int numberOfThreads = 4;
21+
int numberOfIncrements = 100;
22+
23+
//when
24+
Runnable incrementAction = () -> IntStream
25+
.range(0, numberOfIncrements)
26+
.forEach((i) -> counter.increment());
27+
28+
for (int i = 0; i < numberOfThreads; i++) {
29+
executorService.execute(incrementAction);
30+
}
31+
32+
//then
33+
executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
34+
executorService.shutdown();
35+
36+
assertEquals(counter.sum(), numberOfIncrements * numberOfThreads);
37+
assertEquals(counter.sum(), numberOfIncrements * numberOfThreads);
38+
}
39+
40+
@Test
41+
public void givenMultipleThread_whenTheyWriteToSharedLongAdder_thenShouldCalculateSumForThemAndResetAdderAfterward() throws InterruptedException {
42+
//given
43+
LongAdder counter = new LongAdder();
44+
ExecutorService executorService = Executors.newFixedThreadPool(8);
45+
46+
int numberOfThreads = 4;
47+
int numberOfIncrements = 100;
48+
49+
//when
50+
Runnable incrementAction = () -> IntStream
51+
.range(0, numberOfIncrements)
52+
.forEach((i) -> counter.increment());
53+
54+
for (int i = 0; i < numberOfThreads; i++) {
55+
executorService.execute(incrementAction);
56+
}
57+
58+
//then
59+
executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
60+
executorService.shutdown();
61+
62+
assertEquals(counter.sumThenReset(), numberOfIncrements * numberOfThreads);
63+
assertEquals(counter.sum(), 0);
64+
}
65+
}

0 commit comments

Comments
 (0)