FokJoin

作者: arkliu | 来源:发表于2022-12-17 08:49 被阅读0次

    jdk1.7开始增加FokJoin, 并行执行任务,提高执行效率,大数据量。

    image.png
    image.png
    package com.test.forkjoin;
    
    import java.util.concurrent.RecursiveTask;
    
    // 继承自RecursiveTask
    public class ForkJoinDemo extends RecursiveTask<Long>{
        private long start; // 1
        private long end;  // 100000
        private long tmp = 10000;
    
        public ForkJoinDemo(long start, long end) {
            this.start = start;
            this.end = end;
        }
        
        // 实现compute方法
        @Override
        protected Long compute() {
            if (end - start < tmp) { //比1万小求和,普通计算
                long sum = 0;
                for (long i = start; i < end; i++) {
                    sum += i;
                }
                return sum;
            } else { // 使用forkjoin
                long middle = (start + end) /2; // 中间值
                ForkJoinDemo task1 = new ForkJoinDemo(start, middle);
                task1.fork(); // 拆分任务,把任务压入线程队列
                ForkJoinDemo task2 = new ForkJoinDemo(middle, end);
                task2.fork(); // 拆分任务,把任务压入线程队列
                return task1.join() + task2.join(); // 合并任务
            }
        }
    }
    
    
    package com.test.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 InterruptedException, ExecutionException {
    //      test1();
    //      test2();
            test3();
        }
        
        public static void test1() {
            long start = System.currentTimeMillis();
            long sum = 0;
            for (long i = 1; i <= 10_0000_0000; i++) {
                sum += i;
            }
            long end = System.currentTimeMillis();
    //      sum = 500000000500000000执行了:283
            System.out.println("sum = "+sum+"执行了:"+(end - start));
        }
        
        public static void test2() throws InterruptedException, ExecutionException {
            long start = System.currentTimeMillis();
            ForkJoinPool forkJoinPool = new ForkJoinPool();
            ForkJoinTask<Long> task = new ForkJoinDemo(1 , 10_0000_0000);
            ForkJoinTask<Long> submit = forkJoinPool.submit(task);
            Long sum = submit.get(); // 阻塞等待
            long end = System.currentTimeMillis();
            //sum = 499999999500000000执行了:242
            System.out.println("sum = "+sum+"执行了:"+(end - start));
        }
        
        public static void test3() {
            long start = System.currentTimeMillis();
            // 使用stream流计算
            long sum = LongStream.rangeClosed(0, 10_0000_0000).parallel().reduce(0, Long::sum);
            long end = System.currentTimeMillis();
            // sum = 500000000500000000执行了:180
            System.out.println("sum = "+sum+"执行了:"+(end - start));
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:FokJoin

          本文链接:https://www.haomeiwen.com/subject/nlisxdtx.html