# 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;

public class fork_join {
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;
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();
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();
}
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() {

File[] files = path.listFiles();
if(files!=null) {
for(File file:files) {
if(file.isDirectory()) {
}else {
//File encountered, check
if(file.getAbsolutePath().endsWith(".java")) {
System.out.println("Document:"+file.getAbsolutePath());
}
}
}