Fork join divide and rule

Fork join divide and rule

Divide and rule: big problems turn into small problems, and small problems merge into solutions of big problems. fork decomposition, join coalescence

For example, when eating, there are 40 shrimps in total, which were originally peeled by one person. Now 8 people peel shrimps on average, 5 shrimps for one person, which was originally a big task of 40, and turned into a small task of 8 * 5. This is a kind of separation and treatment. Secondly, because shrimp peeling is fast and slow, maybe you peel it very fast, and your brother hasn't finished peeling it yet. At this time, you will peel it for him, but finally The result is still your brother's. at the end of the day, everyone finished with five shrimps. Because you helped your brother, compared with you didn't help your brother, you will surely save time. This mode is work secret. In fork join, the "shrimping" work of every small task will be regarded as a queue, one by one. If you peel, go to help others, you will be from other people's queue Tail, take out the shrimp, peel it, and then give it to him. Pay attention, the result is not your own. Help others to peel the good shrimp, it's someone else's. This is the divide and rule and work secret in fork join.

The standard paradigm of using fork join


Using fork join to calculate the sum of 1 to 100

package tets;

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

public class fork_join {
    public static class myTask extends RecursiveTask<Integer>{
        private final static int THRESHOLD = 10;
        private int[] src; //Represents the array we want to actually count
        private int fromIndex;//Subscript to start statistics
        private int toIndex;//Subscript of where to end statistics
        public myTask(int[] src, int fromIndex, int toIndex) {
            this.src = src;
            this.fromIndex = fromIndex;
            this.toIndex = toIndex;
        }
        @Override
        protected Integer compute() {
            if(toIndex-fromIndex < THRESHOLD) {
                int count = 0;
                for(int i=fromIndex;i<=toIndex;i++) {
                    count = count + src[i];
                }
                return count;
            }else {
                int mid = (fromIndex+toIndex)/2;
                myTask left = new myTask(src,fromIndex,mid);
                myTask right = new myTask(src,mid+1,toIndex);
                invokeAll(left,right);
                return left.join()+right.join();
            }
        }
    }

    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool();
        int arr[] = new int[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = i+1;
        }
        long start = System.currentTimeMillis();
        myTask task = new myTask(arr,0,99);
        pool.execute(task);
        System.out.println("The count is "+task.join()
                +" spend time:"+(System.currentTimeMillis()-start)+"ms");
    }
}

Traversing directories with fork join

package tets;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

public class fork_join_1 {
    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool();
        mytask mytask = new mytask(new File("G:/"));
        pool.execute(mytask);
        mytask.join();
    }
    public static class mytask extends RecursiveAction{
        private File path;//Directory to search for for the current task

        public mytask(File path) {
            this.path = path;
        }
        @Override
        protected void compute() {
            List<mytask> subTasks = new ArrayList<>();

            File[] files = path.listFiles();
            if(files!=null) {
                for(File file:files) {
                    if(file.isDirectory()) {
                        subTasks.add(new mytask(file));
                    }else {
                        //File encountered, check
                        if(file.getAbsolutePath().endsWith(".java")) {
                            System.out.println("Document:"+file.getAbsolutePath());
                        }
                    }
                }
                if(!subTasks.isEmpty()) {
                    for(mytask subTask:invokeAll(subTasks)) {
                        subTask.join();//Wait for subtask execution to complete
                    }
                }
            }
        }
    }
}
Published 7 original articles, won praise 0, visited 29
Private letter follow

Tags: Java

Posted on Sun, 02 Feb 2020 00:16:33 -0800 by casey_00