分叉并加入Java

时间:2014-01-05 13:42:50

标签: java join fork

嘿伙计们我的作业需要一些帮助。我理解Fork和Join Framework的工作方式,但是我的代码没有加入结果。我们的练习是编写一个程序,它计算数组中的真值。对于这篇文章中的任何错误(语法错误或其他错误),这是我的第一个错误。

编辑: 感谢这里的所有请求是我解决这个问题的方法: TrueFinder类:

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

class TrueFinder extends RecursiveTask<TrueResult>
{

    private static final int SEQUENTIAL_THRESHOLD = 5;

    private boolean[] trueData;

    private final int start;

    private final int end;

    public TrueFinder(boolean[] data, int start, int end)
    {
        this.trueData = data;
        this.start = start;
        this.end = end;
    }

    public TrueFinder(boolean[] data)
    {
        this(data, 0, data.length);
    }

    protected TrueResult compute()
    {
        final int length = end - start;
        int counter = 0;
        if (length < SEQUENTIAL_THRESHOLD)
        {
            for (int i = start; i < end; i++)
            {
                if (trueData[i])
                {
                    counter++;
                }
            }
            return new TrueResult(counter);
        }
        else
        {            
            final int split = length / 2;
            TrueFinder left = new TrueFinder(trueData, start, start + split);
            left.fork();
            TrueFinder right = new TrueFinder(trueData, start + split, end);

            TrueResult subResultRight = right.compute();
            TrueResult subResultLeft = left.join();
            return new TrueResult(subResultRight.getTrueCounter() +  
            subResultLeft.getTrueCounter());          
        }
   }


    public static void main(String[] args)
    {
        int trues = 0;
        boolean[] trueArray = new boolean[500];
        for (int i = 0; i < 500; i++)
        {
            if (Math.random() < 0.3)
            {
                trueArray[i] = true;
                trues++;
            }
            else
            {
                trueArray[i] = false;
            }
        }

        TrueFinder finder = new TrueFinder(trueArray);
        ForkJoinPool pool = new ForkJoinPool(4);

        long startTime = System.currentTimeMillis();
        TrueResult result = pool.invoke(finder);
        long endTime = System.currentTimeMillis();
        long actualTime = endTime - startTime;

        System.out.println("Array mit der Länge " + trueArray.length + " in"   
        actualTime + " msec dursucht und " + result.getTrueCounter() + 
        " von " + trues + " True Werten gefunden.");
    }
}

结果类:

public class TrueResult
{
    private int trueCounter;

    public TrueResult(int counter)
    {
        this.trueCounter = counter;
    }

    public int getTrueCounter()
    {
        return trueCounter;
    }
}

1 个答案:

答案 0 :(得分:0)

你的源代码的分裂任务是错误的:
(1)你的分裂不是从0开始的:
  你的开始是1
(2)分裂时忽略分数点;
   (授予SEQUENTIAL_THRESHOLD = 5和trueArray.length = 13,你的分裂忽略了11到12之间的数字)
(3)如果修改(1)和(2),子任务的长度必须分开而不是SQCUQNTIALTHRESHOLD。

因此,修改源代码如下:

else
{
    int split = (length - 1 ) / SEQUENTIAL_THRESHOLD + 1;
    TrueFinder[] subtasks = new TrueFinder[split];
    int start = 0;
    for(int i = 0; i < split - 1; i++)
    {
        subtasks[i] = new TrueFinder(trueData, start, start + SEQUENTIAL_THRESHOLD);
        subtasks[i].fork();
        start += SEQUENTIAL_THRESHOLD;
    }
    subtasks[split - 1] = new TrueFinder(trueData, start, length);
    counter = subtasks[split - 1].compute();// better invoking compute than join
    for (int i = 0; i < SEQUENTIAL_THRESHOLD; i++)
    {
        counter += subtasks[i].join();
    }
    return new TrueResult(counter);
}