ForkJoin realize divide and rule

  • For simple parallel tasks can be solved by "thread pool + Future" program.
  • If there is aggregation relationship between the amount of tasks (AND or OR polymerization polymerization) solved by CompletableFuture.
  • Quantities of parallel tasks solved by CompletionService.

Concurrent programming problem can be divided into three levels: the division of labor, collaboration, mutually exclusive.

What is the use ForkJoin

Fork / Join is a parallel computing framework, mainly to support the partition task model, a framework Fork this calculation corresponds to partition tasks in the task decomposition model, Join corresponding are the combined results.

What is divide and conquer

The complex problem is decomposed into a plurality of similar sub-problems, then the sub-problems decomposed into smaller sub-problems, a simple sub-problems known to be directly solved.

Partakers algorithm field rule algorithm (merge sort, quick sort belong to divide and conquer algorithm, a binary search is also a divide and conquer algorithm); MapReduce also large numbers.

Divide and Conquer model

Partition task can be divided into two phases: task decomposition, the result of the merger.

Fork / Join is used

Fork / Join calculated frame mainly consists of two parts, one partition ForkJoinPool task thread pool, the other part is the partition task ForkJoinTask. The relationship between the two parts of similar ThreadPoolExecutor and Runnable relationship can be understood as submit jobs to the thread pool, but partition has its own unique mission types ForkJoinTask.

ForkJoinTask

  • ForkJoinTask is an abstract class is the core () method and the join () method, fork () will be executed asynchronously a sub-task, join () will block the current thread to wait for the results of subtasks fork.
  • ForkJoinTask even have sub-categories:
    • RecursiveAction: recursive way to deal with the task partition, compute () method does not return a value.
    • RecursiveTask: recursive way to deal with the task partition, compute () method returns a value.

Use ForkJoinTask achieve computing Fibonacci number

package com.thread;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

/**
 * 实现斐波那契数列
 * 求出第n个斐波那契数列值
 **/
public class ForkJoinDemo {
    public static void main(String[] args) {
        //创建分治任务线程池
        ForkJoinPool fjp = new ForkJoinPool(4);
        //创建分治任务
        Fibonacci fib = new Fibonacci(4);
        //启动分治任务
        Integer result = fjp.invoke(fib);
        //输出结果
        System.out.println(result);
    }
    static class Fibonacci extends RecursiveTask<Integer>{
        final int n;
        public Fibonacci(int n){
            this.n = n;
        }
        @Override
        protected Integer compute() {
            if (n <= 1){
                return  n;
            }
            Fibonacci f1 = new Fibonacci(n-1);
            //创建⼦任务
            f1.fork();
            Fibonacci f2 = new Fibonacci(n-2);
            //等待子任务结果,并合并结果.
            return f2.compute() + f1.join();
        }
    }
}复制代码

ForkJoinPool and ForkJoinTask relationship similar to the relationship of ThreadPoolExecutor and Runnable.

ForkJoinPool steal the nature of the queue, the queue will steal busy idle task queue

Implement the recommendations of different types of computing tasks in different ForkJoinPool

If the code word is not easy to have to help you Give me a concern

Love love life technology QQ group: 894 109 590

Guess you like

Origin juejin.im/post/5d8ad950518825092a57a86f