|
| 1 | +package br.com.leonardoz.patterns.divideconquer; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.ForkJoinPool; |
| 6 | +import java.util.concurrent.RecursiveTask; |
| 7 | +import java.util.stream.Collectors; |
| 8 | +import java.util.stream.LongStream; |
| 9 | + |
| 10 | +/* |
| 11 | + * Pattern: Parallel Divide and Conquer |
| 12 | + * |
| 13 | + * Example: Parallel Sum |
| 14 | + */ |
| 15 | +public class ParallelSum extends RecursiveTask<BigInteger> { |
| 16 | + |
| 17 | + private static final long serialVersionUID = 1L; |
| 18 | + private final static int THRESHOLD = 10_000; // Choosing a number to split the computation |
| 19 | + |
| 20 | + private List<BigInteger> nums; |
| 21 | + |
| 22 | + public ParallelSum(List<BigInteger> nums) { |
| 23 | + this.nums = nums; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + protected BigInteger compute() { |
| 28 | + int size = nums.size(); |
| 29 | + if (size < THRESHOLD) { |
| 30 | + return sequentialSum(nums); |
| 31 | + } else { |
| 32 | + ParallelSum x = new ParallelSum(nums.subList(0, size / 2)); |
| 33 | + ParallelSum y = new ParallelSum(nums.subList(size / 2, size)); |
| 34 | + x.fork(); |
| 35 | + y.fork(); |
| 36 | + BigInteger xResult = x.join(); |
| 37 | + BigInteger yResult = y.join(); |
| 38 | + return yResult.add(xResult); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /* |
| 43 | + * Just showing how to use the pattern and some really dummy benchmark. Don't |
| 44 | + * take it seriously. |
| 45 | + */ |
| 46 | + public static void main(String[] args) throws InterruptedException { |
| 47 | + List<BigInteger> nums = LongStream.range(0, 10_000_000L).mapToObj(BigInteger::valueOf) |
| 48 | + .collect(Collectors.toList()); |
| 49 | + |
| 50 | +// Run one then comment and run another |
| 51 | + Runnable parallel = () -> { |
| 52 | + ForkJoinPool commonPool = ForkJoinPool.commonPool(); |
| 53 | + BigInteger result = commonPool.invoke(new ParallelSum(nums)); |
| 54 | + |
| 55 | + System.out.println("Parallel Result is: " + result); |
| 56 | + }; |
| 57 | + |
| 58 | + Runnable sequential = () -> { |
| 59 | + BigInteger acc = sequentialSum(nums); |
| 60 | + |
| 61 | + System.out.println("Sequential Result is: " + acc); |
| 62 | + }; |
| 63 | + |
| 64 | + sequential.run(); |
| 65 | + parallel.run(); |
| 66 | + |
| 67 | + Thread.sleep(2000); |
| 68 | + |
| 69 | + System.out.println("#### After some JIT \n\n"); |
| 70 | + |
| 71 | + dummyBenchmark(sequential); |
| 72 | + dummyBenchmark(parallel); |
| 73 | + |
| 74 | + Thread.sleep(2000); |
| 75 | + |
| 76 | + System.out.println("#### After more JIT \n\n"); |
| 77 | + |
| 78 | + dummyBenchmark(sequential); |
| 79 | + dummyBenchmark(parallel); |
| 80 | + } |
| 81 | + |
| 82 | + private static BigInteger sequentialSum(List<BigInteger> nums) { |
| 83 | + BigInteger acc = BigInteger.ZERO; |
| 84 | + for (BigInteger value : nums) { |
| 85 | + acc = acc.add(value); |
| 86 | + } |
| 87 | + return acc; |
| 88 | + } |
| 89 | + |
| 90 | + static void getHot(Runnable runnable) { |
| 91 | + runnable.run(); |
| 92 | + } |
| 93 | + |
| 94 | + static void dummyBenchmark(Runnable runnable) { |
| 95 | + long before = System.currentTimeMillis(); |
| 96 | + runnable.run(); |
| 97 | + long after = System.currentTimeMillis(); |
| 98 | + System.out.println("Executed in: " + (after - before)); |
| 99 | + System.out.println("######\n"); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments