为什么这个简单的java fork join pool不起作用?

时间:2014-01-30 11:50:11

标签: java fork-join forkjoinpool

我即将测试这个forkjoin池,但它不能正常工作。我想知道为什么?

这是我为获取数组而添加3个元素的类:

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++;
        }
    }

}

这是主要的课程:

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.");

    }

}

1 个答案:

答案 0 :(得分:3)

您正在为每项任务创建一个新数组。虽然这些新数组的内容设置为与输入数组中的相同,但更改这些 new 数组中的值不会影响中的值输入数组。

您必须确保所有任务仅适用于INPUT阵列。任务仅在他们关心的输入数组的元素上有所不同。在此示例中,每个任务正在处理的范围由'min'和'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));
        }
    }

}