Advertisement
joerglenhard

Basic Java 7 Fork/Join Benchmark

Dec 9th, 2011
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. package de.jl.forkjoin;
  2.  
  3. public class Main {
  4.  
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         TestBed tb = new TestBed(75000);
  10.         tb.runTests();
  11.     }
  12. }
  13.  
  14. package de.jl.forkjoin;
  15.  
  16. import java.util.Random;
  17.  
  18. public class TestBed {
  19.  
  20.     private int[] numbers;
  21.  
  22.     public TestBed(int testSize) {
  23.         numbers = new int[testSize];
  24.         // populate Array
  25.         Random rand = new Random(System.currentTimeMillis());
  26.         for (int i = 0; i < numbers.length; i++) {
  27.             numbers[i] = rand.nextInt();
  28.         }
  29.     }
  30.  
  31.     public void runTests() {
  32.         System.out.println("=====STARTING TEST SESSION=====");
  33.         System.out.println("Array Size: " + numbers.length);
  34.  
  35.         runTest("PLAIN OLD THREAD", new PlainOldThreadAlgo());
  36.         runTest("ITERATIVE", new IterativeAlgo());
  37.         runTest("EXECUTOR", new ExecutorAlgo());
  38.  
  39.         runTest("FORKJOIN", new ForkJoinAlgo());
  40.     }
  41.  
  42.     private void runTest(String algoName, Algo algo) {
  43.         System.out.println("=====STARTING " + algoName + " TEST=====");
  44.         long start = System.currentTimeMillis();
  45.         int max = algo.max(numbers);
  46.         long duration = System.currentTimeMillis() - start;
  47.         System.out.println("Max: " + max + "\tduration: " + duration);
  48.     }
  49.  
  50. }
  51.  
  52. package de.jl.forkjoin;
  53.  
  54. public interface Algo {
  55.  
  56.     /**
  57.      * computes the max of an array of integers
  58.      *
  59.      * @param numbers
  60.      *            the array with ints
  61.      * @return the max of numbers
  62.      */
  63.     int max(int[] numbers);
  64.  
  65. }
  66.  
  67. package de.jl.forkjoin;
  68.  
  69. public class IterativeAlgo implements Algo {
  70.  
  71.     @Override
  72.     public int max(int[] numbers) {
  73.         int max = Integer.MIN_VALUE;
  74.         for (int i = 0; i < numbers.length; i++) {
  75.             if (numbers[i] > max) {
  76.                 max = numbers[i];
  77.             }
  78.         }
  79.         return max;
  80.     }
  81.  
  82. }
  83.  
  84. package de.jl.forkjoin;
  85.  
  86. import java.util.concurrent.Callable;
  87. import java.util.concurrent.ExecutionException;
  88. import java.util.concurrent.ExecutorService;
  89. import java.util.concurrent.Executors;
  90. import java.util.concurrent.Future;
  91.  
  92. public class ExecutorAlgo implements Algo {
  93.  
  94.     private ExecutorService pool;
  95.  
  96.     public ExecutorAlgo() {
  97.         pool = Executors.newCachedThreadPool();
  98.     }
  99.  
  100.     @Override
  101.     public int max(int[] numbers) {
  102.         if (numbers == null) {
  103.             throw new IllegalArgumentException("numbers must not be null");
  104.         } else {
  105.             WorkerThread start = new WorkerThread(numbers, 0, numbers.length);
  106.             Future<Integer> max = pool.submit(start);
  107.             try {
  108.                 return max.get();
  109.             } catch (InterruptedException | ExecutionException e) {
  110.                 e.printStackTrace();
  111.                 return -1;
  112.             }finally {
  113.                 pool.shutdown();
  114.             }
  115.         }
  116.     }
  117.  
  118.     private class WorkerThread implements Callable<Integer> {
  119.  
  120.         private int[] numbers;
  121.  
  122.         private int start;
  123.  
  124.         private int end;
  125.  
  126.         public WorkerThread(int[] numbers, int start, int end) {
  127.             this.numbers = numbers;
  128.             this.start = start;
  129.             this.end = end;
  130.  
  131.         }
  132.  
  133.         @Override
  134.         public Integer call() throws Exception {
  135.             if (end - start > 2) {
  136.                 int middle = (end - start) / 2;
  137.                 WorkerThread t1 = new WorkerThread(numbers, start, start
  138.                         + middle);
  139.                 WorkerThread t2 = new WorkerThread(numbers, start + middle + 1,
  140.                         end);
  141.                 Future<Integer> r1 = pool.submit(t1);
  142.                 Future<Integer> r2 = pool.submit(t2);
  143.                 return Math.max(r1.get(), r2.get());
  144.             } else {
  145.                 return Math.max(numbers[start], numbers[end - 1]);
  146.             }
  147.  
  148.         }
  149.  
  150.     }
  151.  
  152. }
  153.  
  154. package de.jl.forkjoin;
  155.  
  156. import java.util.concurrent.RecursiveTask;
  157.  
  158. public class ForkJoinAlgo implements Algo {
  159.  
  160.     @Override
  161.     public int max(int[] numbers) {
  162.         if (numbers == null) {
  163.             throw new IllegalArgumentException("numbers must not be null");
  164.         } else {
  165.             MaxTask start = new MaxTask(numbers, 0, numbers.length);
  166.             return start.compute();
  167.         }
  168.     }
  169.  
  170.     @SuppressWarnings("serial")
  171.     private class MaxTask extends RecursiveTask<Integer> {
  172.  
  173.         private int[] numbers;
  174.  
  175.         private int start;
  176.  
  177.         private int end;
  178.  
  179.         public MaxTask(int[] numbers, int start, int end) {
  180.             this.numbers = numbers;
  181.             this.start = start;
  182.             this.end = end;
  183.         }
  184.  
  185.         @Override
  186.         protected Integer compute() {
  187.             if (end - start > 2) {
  188.                 int middle = (end - start) / 2;
  189.                 MaxTask t1 = new MaxTask(numbers, start, start + middle);
  190.                 MaxTask t2 = new MaxTask(numbers, start + middle + 1, end);
  191.                 return Math.max(t2.compute(), t1.compute());
  192.             } else {
  193.                 return Math.max(numbers[start], numbers[end - 1]);
  194.             }
  195.  
  196.         }
  197.  
  198.     }
  199.  
  200. }
  201.  
  202. package de.jl.forkjoin;
  203.  
  204. public class PlainOldThreadAlgo implements Algo {
  205.  
  206.     @Override
  207.     public int max(int[] numbers) {
  208.         if (numbers == null) {
  209.             throw new IllegalArgumentException("numbers must not be null");
  210.         } else {
  211.             ComputeThread thread = new ComputeThread(numbers, 0, numbers.length);
  212.             thread.start();
  213.             try {
  214.                 thread.join();
  215.             } catch (InterruptedException e) {
  216.                 e.printStackTrace();
  217.             }
  218.             return thread.getResult();
  219.         }
  220.     }
  221.  
  222.     private class ComputeThread extends Thread {
  223.  
  224.         private int[] numbers;
  225.  
  226.         private int start;
  227.  
  228.         private int end;
  229.  
  230.         private int result;
  231.  
  232.         public ComputeThread(int[] numbers, int start, int end) {
  233.             this.numbers = numbers;
  234.             this.start = start;
  235.             this.end = end;
  236.         }
  237.  
  238.         @Override
  239.         public void run() {
  240.             try {
  241.                 this.result = recursiveSearch(numbers, start, end);
  242.             } catch (InterruptedException e) {
  243.                 e.printStackTrace();
  244.             }
  245.         }
  246.  
  247.         public int getResult() {
  248.             return result;
  249.         }
  250.  
  251.         private int recursiveSearch(int[] numbers, int start, int end)
  252.                 throws InterruptedException {
  253.             if ((end - start) > 2) {
  254.                 int middle = (end - start) / 2;
  255.                 ComputeThread t1 = new ComputeThread(numbers, start, start
  256.                         + middle);
  257.                 ComputeThread t2 = new ComputeThread(numbers, start + middle
  258.                         + 1, end);
  259.                 t1.start();
  260.                 t2.start();
  261.                 t1.join();
  262.                 t2.join();
  263.                 return Math.max(t1.getResult(), t2.getResult());
  264.             } else {
  265.                 return Math.max(numbers[start], numbers[end - 1]);
  266.             }
  267.  
  268.         }
  269.  
  270.     }
  271. }
  272.  
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement