import java.util.concurrent.RecursiveTask; /** * @author Aaron Nech * Recursive task that computes if a target is in an array */ public class ForkJoinTest extends RecursiveTask { //eclipse wants private static final long serialVersionUID = 1L; //important private int[] array; private int lowB; // Lower bound private int upB; // Upper bound private int target; /** * Constructor * @param array The source array where the sequence is located * @param lowB The lower bound on the search * @param upB The upper bound on the search * @param target The target number to find */ public ForkJoinTest(int[] array, int lowB, int upB, int target) { this.array = array; this.lowB = lowB; this.upB = upB; this.target = target; } @Override protected Boolean compute() { if(lowB + 1 == upB) { return a[lowB] == target; } else { int mid = (lowB + upB) / 2; ForkJoinTest left = new ForkJoinTest(array, lowB, mid, target); ForkJoinTest right = new ForkJoinTest(array, mid, upB, target); left.fork(); boolean rightAnswer = right.compute(); boolean leftAnswer = left.join(); return leftAnswer || rightAnswer; } } }