|
| 1 | +package br.com.leonardoz.features.parallel_stream; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.stream.IntStream; |
| 7 | + |
| 8 | +/** |
| 9 | + * |
| 10 | + * Warning |
| 11 | + * |
| 12 | + * Parallel Streams uses the ForkJoin Pool, so be aware of using it in Java |
| 13 | + * EE/Jakarta EE environments! |
| 14 | + * |
| 15 | + * The Stream API allows, with methods like parallel() or parallelStream(), the |
| 16 | + * execution of streams's operations in parallel. |
| 17 | + * |
| 18 | + * It enables each element to be processed in parallel, having a thread for each |
| 19 | + * one of them, depending on the number of cores available. Like the Fork/Join |
| 20 | + * Framework, it has an overhead, so the speed of execution can get much better |
| 21 | + * in some cases, getting worse in some others. |
| 22 | + * |
| 23 | + * Streams are composed of a Source, several intermediate operations and a |
| 24 | + * terminal operation. Streams are executed only when a terminal operation is |
| 25 | + * executed, so they're lazy too. The intermediate operations respect the order |
| 26 | + * that you used, they're sequential. The work of each intermediate operation is |
| 27 | + * parallel. |
| 28 | + * |
| 29 | + * CPU intensive tasks benefits from this feature. |
| 30 | + * |
| 31 | + * |
| 32 | + */ |
| 33 | +public class UsingParallelStreams { |
| 34 | + |
| 35 | + public static void main(String[] args) { |
| 36 | + // Creating Parallel Streams from existing collection |
| 37 | + new ArrayList<>().parallelStream(); |
| 38 | + |
| 39 | + // Making Stream Parallel |
| 40 | + IntStream.rangeClosed(0, 30_000) // source |
| 41 | + .parallel().mapToObj(BigInteger::valueOf).map(UsingParallelStreams::isPrime) // Intermediate operations |
| 42 | + .collect(Collectors.toList()); // Terminal Operations |
| 43 | + |
| 44 | + // Each operation run in parallel, out of order |
| 45 | + IntStream.rangeClosed(0, 20) // source |
| 46 | + .parallel().mapToObj(Integer::toString) // Intermediate operation |
| 47 | + .forEach(System.out::print); // Terminal operation |
| 48 | + |
| 49 | + System.out.println("\n"); |
| 50 | + |
| 51 | + // Runs sequentially, in order. |
| 52 | + IntStream.rangeClosed(0, 20).mapToObj(Integer::toString).forEach(System.out::print); |
| 53 | + |
| 54 | + dummyPerformanceCheck(); |
| 55 | + } |
| 56 | + |
| 57 | + private static void dummyPerformanceCheck() { |
| 58 | + |
| 59 | + // Sequential Stream |
| 60 | + long start1 = System.currentTimeMillis(); |
| 61 | + IntStream.rangeClosed(0, 50_000).mapToObj(BigInteger::valueOf).map(UsingParallelStreams::isPrime) |
| 62 | + .collect(Collectors.toList()); |
| 63 | + long end1 = System.currentTimeMillis(); |
| 64 | + long time1 = (end1 - start1) / 1000; |
| 65 | + System.out.println("Sequential: " + time1); |
| 66 | + |
| 67 | + // Parallel Stream |
| 68 | + long start2 = System.currentTimeMillis(); |
| 69 | + IntStream.rangeClosed(0, 50_000).parallel().mapToObj(BigInteger::valueOf).map(UsingParallelStreams::isPrime) |
| 70 | + .collect(Collectors.toList()); |
| 71 | + long end2 = System.currentTimeMillis(); |
| 72 | + long time2 = (end2 - start2) / 1000; |
| 73 | + System.out.println("Parallel: " + time2); |
| 74 | + } |
| 75 | + |
| 76 | + // thanks to linski on |
| 77 | + // https://stackoverflow.com/questions/15862271/java-compute-intensive-task |
| 78 | + public static boolean isPrime(BigInteger n) { |
| 79 | + BigInteger counter = BigInteger.ONE.add(BigInteger.ONE); |
| 80 | + boolean isPrime = true; |
| 81 | + while (counter.compareTo(n) == -1) { |
| 82 | + if (n.remainder(counter).compareTo(BigInteger.ZERO) == 0) { |
| 83 | + isPrime = false; |
| 84 | + break; |
| 85 | + } |
| 86 | + counter = counter.add(BigInteger.ONE); |
| 87 | + } |
| 88 | + return isPrime; |
| 89 | + } |
| 90 | +} |
0 commit comments