|
1 | | -package org.baeldung.java.streams; |
2 | | - |
3 | | -import org.junit.Test; |
4 | | - |
5 | | -import java.util.ArrayList; |
6 | | -import java.util.List; |
7 | | -import java.util.concurrent.ExecutionException; |
8 | | -import java.util.concurrent.ForkJoinPool; |
9 | | -import java.util.stream.Collectors; |
10 | | -import java.util.stream.LongStream; |
11 | | -import java.util.stream.Stream; |
12 | | - |
13 | | -import static org.junit.Assert.assertEquals; |
14 | | -import static org.junit.Assert.assertTrue; |
15 | | - |
16 | | -public class ThreadPoolInParallelStreamTest { |
17 | | - |
18 | | - @Test |
19 | | - public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException { |
20 | | - long firstNum = 1; |
21 | | - long lastNum = 1_000_000; |
22 | | - |
23 | | - List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList()); |
24 | | - |
25 | | - ForkJoinPool customThreadPool = new ForkJoinPool(4); |
26 | | - long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get(); |
27 | | - |
28 | | - assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal); |
29 | | - } |
30 | | - |
31 | | - @Test |
32 | | - public void givenList_whenCallingParallelStream_shouldBeParallelStream() { |
33 | | - List<Long> aList = new ArrayList<>(); |
34 | | - Stream<Long> parallelStream = aList.parallelStream(); |
35 | | - |
36 | | - assertTrue(parallelStream.isParallel()); |
37 | | - } |
38 | | -} |
| 1 | +package org.baeldung.java.streams; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.ExecutionException; |
| 8 | +import java.util.concurrent.ForkJoinPool; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import java.util.stream.LongStream; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertTrue; |
| 15 | + |
| 16 | +public class ThreadPoolInParallelStreamIntegrationTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException { |
| 20 | + long firstNum = 1; |
| 21 | + long lastNum = 1_000_000; |
| 22 | + |
| 23 | + List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList()); |
| 24 | + |
| 25 | + ForkJoinPool customThreadPool = new ForkJoinPool(4); |
| 26 | + long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get(); |
| 27 | + |
| 28 | + assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void givenList_whenCallingParallelStream_shouldBeParallelStream() { |
| 33 | + List<Long> aList = new ArrayList<>(); |
| 34 | + Stream<Long> parallelStream = aList.parallelStream(); |
| 35 | + |
| 36 | + assertTrue(parallelStream.isParallel()); |
| 37 | + } |
| 38 | +} |
0 commit comments