简体   繁体   中英

ForkJoinTask understanding

What happens after subtask.fork() returns a ForkJoinTask<Int> object? compute() method was invoked many times after that but from where and how?

I'm new to Java and I'm trying to learn the concept of fork/join framework. I saw the code below online. I know after reading Java API that subtask.fork() returns a object of ForkJoinTask<V> , which is a ForkJoinTask<Int> in this case. What I couldn't understand is what happens after that? The output indicate that the compute() method have been invoked many times after that but from where and how?

public class MyRecursiveAction extends RecursiveAction {

    public static void main(String[] args) {
        MyRecursiveAction mra1 = new MyRecursiveAction(100);
        ForkJoinPool fjp1 = new ForkJoinPool();
        fjp1.invoke(mra1);
    }

    private long workLoad = 0;

    public MyRecursiveAction(long workLoad) {
        this.workLoad = workLoad;
    }

    @Override
    protected void compute() {

        //if work is above threshold, break tasks up into smaller tasks
        if(this.workLoad > 16) {
            System.out.println("Splitting workLoad : " + this.workLoad);

            List<MyRecursiveAction> subtasks =
                new ArrayList<MyRecursiveAction>();

            subtasks.addAll(createSubtasks());

            for(RecursiveAction subtask : subtasks){
                subtask.fork();
            }

        } else {
            System.out.println("Doing workLoad myself: " + this.workLoad);
        }
    }

    private List<MyRecursiveAction> createSubtasks() {
        List<MyRecursiveAction> subtasks =
            new ArrayList<MyRecursiveAction>();

        MyRecursiveAction subtask1 = new MyRecursiveAction(this.workLoad / 2);
        MyRecursiveAction subtask2 = new MyRecursiveAction(this.workLoad / 2);

        subtasks.add(subtask1);
        subtasks.add(subtask2);

        return subtasks;
    }

}

Output:

Splitting workLoad : 100
Splitting workLoad : 50
Splitting workLoad : 50
Splitting workLoad : 25
Doing workLoad myself: 12
Doing workLoad myself: 12
Splitting workLoad : 25
Doing workLoad myself: 12
Doing workLoad myself: 12
Splitting workLoad : 25
Doing workLoad myself: 12
Splitting workLoad : 25
Doing workLoad myself: 12
Doing workLoad myself: 12
Doing workLoad myself: 12

You need to call invokeAll in order to split you work in two pieces instead of doing it manually, then 2 new threads would be created to 'compute' your result, and then if it's not small enough 2 new threads and so on..,

You can't predict in what order thread would be executed so the messages order would vary every time you run this code.

public class MyRecursiveAction extends RecursiveAction {

    public static void main(String[] args) {
        MyRecursiveAction mra1 = new MyRecursiveAction(100);
        ForkJoinPool fjp1 = new ForkJoinPool();
        fjp1.invoke(mra1);
    }

    private long workLoad = 0;

    public MyRecursiveAction(long workLoad) {
        this.workLoad = workLoad;
    }

    @Override
    protected void compute() {

        //if work is above threshold, break tasks up into smaller tasks
        if(this.workLoad > 16) {
            System.out.println("Splitting workLoad : " + this.workLoad);

            invokeAll(new MyRecursiveAction(this.workLoad / 2), new MyRecursiveAction(this.workLoad / 2));

        } else {
            System.out.println("Doing workLoad myself: " + this.workLoad);
        }
    }

}

Java doc of ForkJoinTask

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