I have a recursive divide and conquer algorithm that requires two computationally intensive base case tasks before starting the dividing. The initial base cases are independent tasks, so I want to do them in parallel. After the base cases, the dividing runs the same tasks with different inputs between 0 and 1, and based on the output decides whether or not to split again. I got the base cases to work by creating a task wrapper object that fakes the recursion, but this feels like a kludge, as follows:
public static void doSomething () {
ForkJoinPool pool = new ForkJoinPool();
private ArrayList<Object> al = new ArrayList<Object>();
TaskWrapper tw = new TaskWrapper(true,-1);
al.addAll(pool.invoke(tw));
}
@SuppressWarnings("serial")
public static class TaskWrapper extends RecursiveTask<ArrayList<Object>> {
private ArrayList<Object> al = new ArrayList<Object>();
private boolean arg;
private double input;
private Object out;
TaskWrapper(boolean ar, double in){
arg = ar;
input = in;
}
@Override
public ArrayList<Object> compute() {
if (arg == false) {
out = new Object(runIntensiveTask(input));
al.add(out);
}
else {
// Right Base Case
TaskWrapper right = new TaskWrapper(false, 1);
right.fork();
// Left Base Case
TaskWrapper left = new TaskWrapper(false, 0);
al.addAll(left.compute());
// Join with Right result
al.addAll(right.join());
}
return al;
}
}
Is there a simpler way to accomplish the same thing?
This is my first StackOverflow post, so please forgive any formatting or protocol errors. Thank you for the help.