-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathUsingParallelStreams.java
99 lines (86 loc) · 3.08 KB
/
UsingParallelStreams.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package br.com.leonardoz.features.parallel_stream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
*
* Warning
*
* Parallel Streams uses the ForkJoin Pool, so be aware of using it in Java
* EE/Jakarta EE environments!
*
* The Stream API allows, with methods like parallel() or parallelStream(), the
* execution of streams's operations in parallel.
*
* It enables each element to be processed in parallel, having a thread for each
* one of them, depending on the number of cores available. Like the Fork/Join
* Framework, it has an overhead, so the speed of execution can get much better
* in some cases, getting worse in some others.
*
* Streams are composed of a Source, several intermediate operations and a
* terminal operation. Streams are executed only when a terminal operation is
* executed, so they're lazy too. The intermediate operations respect the order
* that you used, they're sequential. The work of each intermediate operation is
* parallel.
*
* CPU intensive tasks benefits from this feature.
*
*
*/
public class UsingParallelStreams {
public static void main(String[] args) {
// Creating Parallel Streams from existing collection
new ArrayList<>().parallelStream();
// Making Stream Parallel
IntStream.rangeClosed(0, 30_000) // source
.parallel().mapToObj(BigInteger::valueOf).map(UsingParallelStreams::isPrime) // Intermediate operations
.collect(Collectors.toList()); // Terminal Operations
// Each operation run in parallel, out of order
IntStream.rangeClosed(0, 20) // source
.parallel().mapToObj(Integer::toString) // Intermediate operation
.forEach(System.out::print); // Terminal operation
System.out.println("\n");
// Runs sequentially, in order.
IntStream.rangeClosed(0, 20)
.mapToObj(Integer::toString)
.forEach(System.out::print);
System.out.println("\n");
dummyPerformanceCheck();
}
private static void dummyPerformanceCheck() {
// Sequential Stream
var start1 = System.currentTimeMillis();
IntStream.rangeClosed(0, 50_000)
.mapToObj(BigInteger::valueOf)
.map(UsingParallelStreams::isPrime)
.collect(Collectors.toList());
var end1 = System.currentTimeMillis();
var time1 = (end1 - start1) / 1000;
System.out.println("Sequential: " + time1);
// Parallel Stream
var start2 = System.currentTimeMillis();
IntStream.rangeClosed(0, 50_000)
.parallel()
.mapToObj(BigInteger::valueOf)
.map(UsingParallelStreams::isPrime)
.collect(Collectors.toList());
var end2 = System.currentTimeMillis();
var time2 = (end2 - start2) / 1000;
System.out.println("Parallel: " + time2);
}
// thanks to linski on
// https://stackoverflow.com/questions/15862271/java-compute-intensive-task
public static boolean isPrime(BigInteger n) {
var counter = BigInteger.ONE.add(BigInteger.ONE);
var isPrime = true;
while (counter.compareTo(n) == -1) {
if (n.remainder(counter).compareTo(BigInteger.ZERO) == 0) {
isPrime = false;
break;
}
counter = counter.add(BigInteger.ONE);
}
return isPrime;
}
}