Scenario: When there are many tasks, thousands of them, or when a single task is large and takes a lot of time to execute, you can save time by splitting the task into smaller tasks and then combining the results of each task when the smaller tasks are completed.

The ForkJoinPool implements the ExecutorService interface, so it is also a thread pool that divides a task into smaller tasks and aggregates the results of those tasks.

Here’s a small example:

/ / initialize a ForkJoinPool static ForkJoinPool pool = new ForkJoinPool (3, ForkJoinPool defaultForkJoinWorkerThreadFactory, null, true); Static ArrayList<String> list = new ArrayList<>(); Static void addList() {list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); list.add("www.baidu.com"); list.add("www.blog.csdn.net"); } public static void main(String[] args) throws ExecutionException, InterruptedException { addList(); ForkJoinTask<String> task = pool.submit(new Work(list, 0, list.size())); System.out.println(task.get()); } public static String doRequest(String URL, int index) {return index + "-- ":" + URL + "\n"; Static class Work extends RecursiveTask<String> {List<String> List; int start; int end; public Work(List<String> list, int start, int end) { this.list = list; this.start = start; this.end = end; } @Override protected String compute() { int count = end - start; String result = ""; If (count <= 10) {for (int I = 0; i<list.size(); i++) { result += doRequest(list.get(i), i); } return result; Int x = (start + end) / 2; int x = (start + end) / 2; Work1 = new Work(list, start, x); work1.fork(); Work2 = new Work(list, x, end); work2.fork(); Result += work1.join(); result += work2.join(); return result; }}}Copy the code

Execute logic:

The first step:

The second step:

Step 3:

When a thread has a task, it will split the task. When a small task meets the execution conditions, it will execute the task. Then, it will complete the task from the smallest level according to the hierarchy, and retrieve the result of the task one level at a time, and finally get the result of each small task in the ForkJoinTask.