ForkJoin

In the case of large data volume, parallel execution of tasks, improve efficiency

Break big tasks into smaller ones

package com.zhong.forkjoin;

import java.util.concurrent.RecursiveTask;

ForkJoin three methods for calculating sum values from 0 to 1 billion ForkJoin performs tasks in parallel, increasing efficiency and large amounts of data */
public class ForkJoinDemo extends RecursiveTask<Long> {

    private Long start;
    private Long end;

    / / the critical value
    private Long temp = 10000L;

    public ForkJoinDemo(Long start, Long end){
        this.start = start;
        this.end = end;
    }

    // The method of calculation
    @Override
    protected Long compute(a) {
        if ( (end-start)<temp ){
            Long sum = 0L;
            for (Long i = start; i <= end ; i++) {
                sum += i;
            }
            return sum;
        } else {
            / / forkjoin recursion
            long middle = (start + end) / 2; / / intermediate value
            ForkJoinDemo task1 = new ForkJoinDemo(start, middle);
            task1.fork();// Split the task and push it into a thread queue
            ForkJoinDemo task2 = new ForkJoinDemo(middle+1, end);
            task2.fork();// Split the task and push it into a thread queue
            returntask1.join() + task2.join(); }}}Copy the code

The test class

package com.zhong.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class Test {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        test1(); / / 7688
        test2(); / / 8186
        test3(); / / 139
    }

    Int money = 10_0000_0000; int money = 10_0000_0000; * /

    // For ordinary people, use this. To adjust
    public static void test1(a){
        Long sum = 0L;
        long start = System.currentTimeMillis();
        for (Long i = 1L; i <= 10 _0000_0000 ; i++) {
            sum += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("sum= "+ sum +"Time:" + (end-start));
    }

    ForkJoin is used
    public static void test2(a) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> task = new ForkJoinDemo(0L.10_0000_0000L);
        ForkJoinTask<Long> submit = forkJoinPool.submit(task);// Submit the task
        Long sum = submit.get();
        long end = System.currentTimeMillis();
        System.out.println("sum= "+ sum +"Time:" + (end-start));
    }

    / / the Stream parallel flows
    public static void test3(a){
        long start = System.currentTimeMillis();
        / / the Stream parallel flows
        long sum = LongStream.rangeClosed(0L.10_0000_0000L).parallel().reduce(0, Long::sum);
        long end = System.currentTimeMillis();
        System.out.println("sum= "+ sum +"Time:"+ (end-start)); }}Copy the code