Question

I am about to test this forkjoin pool but it doesn't work fine. I wonder why?

This is the class which I made to get an array and adds 3 to its elements:

import static java.util.concurrent.ForkJoinTask.invokeAll;
import java.util.concurrent.RecursiveAction;

public class t extends RecursiveAction{

    private final int array[];
    private final int th=4;

    public t (int array[]){
        this.array=array;
    }

    protected void compdir(){

        for (int i=0;i<array.length;i++){
            array[i]=array[i]+3;
        }
    }

    @Override
    protected void compute(){
        int cont=1;

        System.out.println("array="+(array[0])+","+array[array.length-1]);


        System.out.println("cont="+cont);
        if (array.length<th){      
            compdir();

        } else {

            int spil=(array[0]+array[(array.length-1)])/2;
            int array1[]=new int[spil];
            int array2[]=new int[spil];
            for (int i=0;i<array1.length;i++){
                array1[i]=array[i];
            }
            for (int i=0;i<array2.length;i++){
                array2[i]=array[i+(array1.length)];
            }
            invokeAll(new t(array1) , new t(array2));
            cont++;
        }
    }

}

and this is the main class:

package nt;

import java.util.concurrent.ForkJoinPool;

public class Nt {


    public static void main(String[] args) {
        int processors = Runtime.getRuntime().availableProcessors();
        System.out.println(Integer.toString(processors) + " processor"
                + (processors != 1 ? "s are " : " is ")
                + "available");

        int arr[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
        t test=new t(arr);

        ForkJoinPool pool = new ForkJoinPool();

        long startTime = System.currentTimeMillis();
        pool.invoke(test);
        long endTime = System.currentTimeMillis();

        System.out.println("Takes " + (endTime - startTime) + 
                " milliseconds.");

    }

}
Was it helpful?

Solution

You are creating a new array for each task. Although the contents of these new arrays is set to be the same as in the input array, changing the values in these new arrays will NOT affect the values in the input array.

You have to make sure that ALL tasks ONLY work on the INPUT array. The tasks only differ in the elements of the input array that they care about. In this example, the range that each task is working on is specified by 'min' and 'max' :

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

public class Nt {
    public static void main(String[] args) {
        int processors = Runtime.getRuntime().availableProcessors();
        System.out.println(Integer.toString(processors) + " processor"
            + (processors != 1 ? "s are " : " is ")
            + "available");

        int arr[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
        t test=new t(arr);

        ForkJoinPool pool = new ForkJoinPool();

        long startTime = System.currentTimeMillis();
        pool.invoke(test);
        long endTime = System.currentTimeMillis();

        System.out.println("Takes " + (endTime - startTime) + 
            " milliseconds.");

    }
}
class t extends RecursiveAction{


    private final int array[];
    private final int min;
    private final int max;
    private final int th=4;

    public t (int array[]){
        this(array, 0, array.length);
    }

    public t (int array[], int min, int max){
        this.array=array;
        this.min = min;
        this.max = max;

        System.out.println("Task to handle range "+min+" to "+max);
    }

    protected void compdir(){

        for (int i=min;i<max;i++){
            array[i]=array[i]+3;
        }
    }

    @Override
    protected void compute(){
        if (max-min<th) {      
            compdir();
        } else {

            int center = min + (max - min) / 2;
            invokeAll(new t(array, min, center) , new t(array, center+1, max));
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top