Skip to content

Commit aeee3c4

Browse files
author
timwe_dotun
committed
Implemented suggested edits from editor.
1 parent bbf527a commit aeee3c4

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

‎core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStream.java‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.baeldung.java.streams;
22

3-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
45

56
import java.util.ArrayList;
67
import java.util.List;
@@ -9,27 +10,28 @@
910
import java.util.stream.Stream;
1011

1112
import org.junit.Test;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
1413

1514
public class ThreadPoolInParallelStream {
16-
Integer total = 1784293664;
17-
Logger log = LoggerFactory.getLogger(ThreadPoolInParallelStream.class);
1815

1916
@Test
20-
public void givenListOfInteger_calculateSumOfIntegers() throws InterruptedException, ExecutionException {
17+
public void giveRangeOfInts_whenSumedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
2118
List<Integer> aList = new ArrayList<>();
22-
int listSize = 1_000_000;
23-
for(int i = 1; i <= listSize; i++){
19+
int lastNum = 1_000_000;
20+
int firstNum = 1;
21+
22+
int expectedTotal = (lastNum - firstNum + 1) * (firstNum + lastNum ) / 2;
23+
24+
for(int i = firstNum; i <= lastNum; i++){
2425
aList.add(i);
25-
}
26-
ForkJoinPool customThreadPool = new ForkJoinPool(5);
27-
Integer sum = customThreadPool.submit(() -> aList.parallelStream().reduce(
26+
}
27+
28+
ForkJoinPool customThreadPool = new ForkJoinPool(4);
29+
int actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(
2830
0, (x, y) -> {
2931
return x + y;
3032
})).get();
3133

32-
assertEquals(total, sum);
34+
assertEquals(expectedTotal, actualTotal);
3335
}
3436

3537
@Test

0 commit comments

Comments
 (0)