Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.jl.forkjoin;
- public class Main {
- /**
- * @param args
- */
- public static void main(String[] args) {
- TestBed tb = new TestBed(75000);
- tb.runTests();
- }
- }
- package de.jl.forkjoin;
- import java.util.Random;
- public class TestBed {
- private int[] numbers;
- public TestBed(int testSize) {
- numbers = new int[testSize];
- // populate Array
- Random rand = new Random(System.currentTimeMillis());
- for (int i = 0; i < numbers.length; i++) {
- numbers[i] = rand.nextInt();
- }
- }
- public void runTests() {
- System.out.println("=====STARTING TEST SESSION=====");
- System.out.println("Array Size: " + numbers.length);
- runTest("PLAIN OLD THREAD", new PlainOldThreadAlgo());
- runTest("ITERATIVE", new IterativeAlgo());
- runTest("EXECUTOR", new ExecutorAlgo());
- runTest("FORKJOIN", new ForkJoinAlgo());
- }
- private void runTest(String algoName, Algo algo) {
- System.out.println("=====STARTING " + algoName + " TEST=====");
- long start = System.currentTimeMillis();
- int max = algo.max(numbers);
- long duration = System.currentTimeMillis() - start;
- System.out.println("Max: " + max + "\tduration: " + duration);
- }
- }
- package de.jl.forkjoin;
- public interface Algo {
- /**
- * computes the max of an array of integers
- *
- * @param numbers
- * the array with ints
- * @return the max of numbers
- */
- int max(int[] numbers);
- }
- package de.jl.forkjoin;
- public class IterativeAlgo implements Algo {
- @Override
- public int max(int[] numbers) {
- int max = Integer.MIN_VALUE;
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] > max) {
- max = numbers[i];
- }
- }
- return max;
- }
- }
- package de.jl.forkjoin;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- public class ExecutorAlgo implements Algo {
- private ExecutorService pool;
- public ExecutorAlgo() {
- pool = Executors.newCachedThreadPool();
- }
- @Override
- public int max(int[] numbers) {
- if (numbers == null) {
- throw new IllegalArgumentException("numbers must not be null");
- } else {
- WorkerThread start = new WorkerThread(numbers, 0, numbers.length);
- Future<Integer> max = pool.submit(start);
- try {
- return max.get();
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- return -1;
- }finally {
- pool.shutdown();
- }
- }
- }
- private class WorkerThread implements Callable<Integer> {
- private int[] numbers;
- private int start;
- private int end;
- public WorkerThread(int[] numbers, int start, int end) {
- this.numbers = numbers;
- this.start = start;
- this.end = end;
- }
- @Override
- public Integer call() throws Exception {
- if (end - start > 2) {
- int middle = (end - start) / 2;
- WorkerThread t1 = new WorkerThread(numbers, start, start
- + middle);
- WorkerThread t2 = new WorkerThread(numbers, start + middle + 1,
- end);
- Future<Integer> r1 = pool.submit(t1);
- Future<Integer> r2 = pool.submit(t2);
- return Math.max(r1.get(), r2.get());
- } else {
- return Math.max(numbers[start], numbers[end - 1]);
- }
- }
- }
- }
- package de.jl.forkjoin;
- import java.util.concurrent.RecursiveTask;
- public class ForkJoinAlgo implements Algo {
- @Override
- public int max(int[] numbers) {
- if (numbers == null) {
- throw new IllegalArgumentException("numbers must not be null");
- } else {
- MaxTask start = new MaxTask(numbers, 0, numbers.length);
- return start.compute();
- }
- }
- @SuppressWarnings("serial")
- private class MaxTask extends RecursiveTask<Integer> {
- private int[] numbers;
- private int start;
- private int end;
- public MaxTask(int[] numbers, int start, int end) {
- this.numbers = numbers;
- this.start = start;
- this.end = end;
- }
- @Override
- protected Integer compute() {
- if (end - start > 2) {
- int middle = (end - start) / 2;
- MaxTask t1 = new MaxTask(numbers, start, start + middle);
- MaxTask t2 = new MaxTask(numbers, start + middle + 1, end);
- return Math.max(t2.compute(), t1.compute());
- } else {
- return Math.max(numbers[start], numbers[end - 1]);
- }
- }
- }
- }
- package de.jl.forkjoin;
- public class PlainOldThreadAlgo implements Algo {
- @Override
- public int max(int[] numbers) {
- if (numbers == null) {
- throw new IllegalArgumentException("numbers must not be null");
- } else {
- ComputeThread thread = new ComputeThread(numbers, 0, numbers.length);
- thread.start();
- try {
- thread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return thread.getResult();
- }
- }
- private class ComputeThread extends Thread {
- private int[] numbers;
- private int start;
- private int end;
- private int result;
- public ComputeThread(int[] numbers, int start, int end) {
- this.numbers = numbers;
- this.start = start;
- this.end = end;
- }
- @Override
- public void run() {
- try {
- this.result = recursiveSearch(numbers, start, end);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public int getResult() {
- return result;
- }
- private int recursiveSearch(int[] numbers, int start, int end)
- throws InterruptedException {
- if ((end - start) > 2) {
- int middle = (end - start) / 2;
- ComputeThread t1 = new ComputeThread(numbers, start, start
- + middle);
- ComputeThread t2 = new ComputeThread(numbers, start + middle
- + 1, end);
- t1.start();
- t2.start();
- t1.join();
- t2.join();
- return Math.max(t1.getResult(), t2.getResult());
- } else {
- return Math.max(numbers[start], numbers[end - 1]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement