简体   繁体   中英

Fork and Join Java

Hey guys I need some help with my homework. I understand the way the Fork and Join Framework works, but my code does not join the results. Our exercise is to write a program, that counts the true values in an array. Sorry for any mistakes (bad grammar or something else) in this post, it is my first one.

Edit: Thanks for all the requests here is my solution of this problem: TrueFinder Class:

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

And the result class:

public class TrueResult
{
    private int trueCounter;

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

    public int getTrueCounter()
    {
        return trueCounter;
    }
}

The splitting task of your souce code is wrong as :
(1) your splitting isn't started from 0:
your start is 1
(2) fraction point is ignored for your splitting;
(granted that SEQUENTIAL_THRESHOLD=5 and trueArray.length = 13, your splitting is ignoring of the numbers from 11 to 12)
(3) if you modify for (1) and (2), the length of subtasks must be split not SQCUQNTIALTHRESHOLD.

So, the modifying source code is below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM