Make sample code in http://www.ibm.com/developerworks/java/library/j-jtp11137.html and http://www.ibm.com/developerworks/java/library/j-jtp03048.html runnable.
public class SelectMaxProblem { private final int[] numbers; private final int start; private final int end; public SelectMaxProblem(int[] numbers, int start, int end) { this.numbers = numbers; this.start = start; this.end = end; } public int solveSequentially() { int max = Integer.MIN_VALUE; for (int i = start; i < end; i++) { int n = numbers[i]; if (n > max) max = n; } return max; } public SelectMaxProblem subproblem(int subStart, int subEnd) { return new SelectMaxProblem(numbers, start + subStart, start + subEnd); } public int getSize() { return end - start; } }
import jsr166y.ForkJoinPool; import jsr166y.ForkJoinTask; import jsr166y.RecursiveAction; public class MaxWithFJ extends RecursiveAction { private final int threshold; private final SelectMaxProblem problem; public int result; public MaxWithFJ(SelectMaxProblem problem, int threshold) { this.problem = problem; this.threshold = threshold; } @Override protected void compute() { if (problem.getSize() < threshold) result = problem.solveSequentially(); else { int midpoint = problem.getSize() / 2; MaxWithFJ left = new MaxWithFJ(problem.subproblem(0, midpoint + 1), threshold); MaxWithFJ right = new MaxWithFJ(problem.subproblem(midpoint + 1, problem.getSize()), threshold); invokeAll(new ForkJoinTask[]{left, right}); result = Math.max(left.result, right.result); } } public static void main(String[] args) { int[] data = {1, 200, 7, 800, 90, 19}; SelectMaxProblem problem = new SelectMaxProblem(data, 0, data.length); int threshold = 3; int nThreads = 4; MaxWithFJ mfj = new MaxWithFJ(problem, threshold); ForkJoinPool pool = new ForkJoinPool(nThreads); pool.invoke(mfj); int result = mfj.result; System.out.println(result); } private static final long serialVersionUID = 1L; }
import java.util.Arrays; import jsr166y.ForkJoinPool; import jsr166y.RecursiveAction; public class MergeSort extends RecursiveAction { final int[] numbers; final int startPos, endPos; final int[] result; public MergeSort(int[] numbers, int startPos, int endPos) { this.numbers = numbers; this.startPos = startPos; this.endPos = endPos; this.result = new int[size()]; } private void merge(MergeSort left, MergeSort right) { int i = 0, leftPos = 0, rightPos = 0, leftSize = left.size(), rightSize = right.size(); while (leftPos < leftSize && rightPos < rightSize) result[i++] = (left.result[leftPos] <= right.result[rightPos]) ? left.result[leftPos++] : right.result[rightPos++]; while (leftPos < leftSize) result[i++] = left.result[leftPos++]; while (rightPos < rightSize) result[i++] = right.result[rightPos++]; } public int size() { return endPos - startPos; } @Override protected void compute() { if (size() < 3) { // threshold System.arraycopy(numbers, startPos, result, 0, size()); Arrays.sort(result, 0, size()); } else { int midpoint = size() / 2; MergeSort left = new MergeSort(numbers, startPos, startPos + midpoint); MergeSort right = new MergeSort(numbers, startPos + midpoint, endPos); invokeAll(left, right); merge(left, right); } } public static void main(String[] args) { int[] numbers = {12, 23, 100, 1, 2, 9}; int nThreads = 4; MergeSort mfj = new MergeSort(numbers, 0, numbers.length); ForkJoinPool pool = new ForkJoinPool(nThreads); pool.invoke(mfj); printArray(mfj.result); } private static void printArray(int[] arr) { for (int n : arr) System.out.printf("%d ", n); System.out.println(); } private static final long serialVersionUID = 1L; }