-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelStreamDemo.java
More file actions
50 lines (40 loc) · 1.82 KB
/
ParallelStreamDemo.java
File metadata and controls
50 lines (40 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package Stream;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
/**
* @author VIVEK
* A type of a stream that enables parallel processing of elements
* allowing multiple thread to process parts of stream simultaneously
* improves the performance for larger data sets
* workload is distributed across multiple threads
* not suitable for smallar data sets may cause overhead
* Usage: large datasets,sequence doesn't matter and data is independent
* like in below example factorial is independent but cumulative sum is dependent
*/
public class ParallelStreamDemo {
private static Long fact(long n) {
long fact = 1L;
for (long i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
static void main(String[] args) {
List<Long> list = Stream.iterate(1L, x -> x + 1).limit(20000).toList();
// without parallel stream
long startTime = System.currentTimeMillis();
List<Long> factList = list.stream().map(ParallelStreamDemo::fact).toList();
System.out.println("Time takes without parallel stream : " + (System.currentTimeMillis() - startTime) + " ms");
// with parallel stream
startTime = System.currentTimeMillis();
factList = list.parallelStream().map(ParallelStreamDemo::fact).toList();
System.out.println("Time takes with parallel stream : " + (System.currentTimeMillis() - startTime) + " ms");
// sequential stream
factList = list.parallelStream().map(ParallelStreamDemo::fact).sequential().toList();
//cumulative sum [1,2,3] -> [1,3,6]
List<Integer> nums = Stream.iterate(1, x -> x + 1).limit(100).toList();
AtomicInteger sum = new AtomicInteger(0);
System.out.println(nums.stream().map(sum::addAndGet).toList());
}
}