编程语言
首页 > 编程语言> > Java8新特性_并行流与串行流

Java8新特性_并行流与串行流

作者:互联网

package com.zs.boot.controller;

import java.util.concurrent.RecursiveTask;

public class ForkJoinCalculate extends RecursiveTask<Long> {

    private long start;

    private long end;

    public ForkJoinCalculate(long start, long end) {
        this.start = start;
        this.end = end;
    }

    //临界值
    private static final long THRESHOLD = 10000;

    @Override
    protected Long compute() {
        long length = end - start;
        if(length <= THRESHOLD){
            long sum = 0;
            for (long i = start; i <= end; i++) {
                sum += i;
            }
            return sum;
        }else {
            long middle = (start+end)/2;
            ForkJoinCalculate left = new ForkJoinCalculate(start,middle);
            //拆分子任务,同时压入线程队列
            left.fork();

            ForkJoinCalculate right = new ForkJoinCalculate(middle+1,end);
            right.fork();

            //合并
            return left.join() + right.join();
        }
    }
}
package com.zs.boot.controller;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class TestForkJoin {
    public static void main(String[] args) {

        /**
         * ForkJoin框架
         */

        //long start = System.currentTimeMillis();
        //想试一下java8的时间
        /*Instant start = Instant.now();

        //需要线程池的支持
        ForkJoinPool pool = new ForkJoinPool();
        ForkJoinTask<Long> task = new ForkJoinCalculate(0, 10000000000L);
        Long sum = pool.invoke(task);
        System.out.println(sum);

        Instant end = start.now();
        System.out.println("耗费时间为:"+Duration.between(start, end).toMillis()+"毫秒");*/


        /**
         * 普通for循环,单线程
         */
        /*Instant start1 = Instant.now();
        long sum1 = 0L;
        for (long i = 0; i <= 10000000000L; i++) {
            sum1 +=i;
        }
        System.out.println(sum1);
        Instant end1 = Instant.now();
        System.out.println("耗费时间为:"+Duration.between(start1, end1).toMillis()+"毫秒");*/


        //测试总结:累加1亿,看不出效果,for循环看不出效果,累加100亿就能看出来了
        /*累加1亿:
        5000000050000000
        耗费时间为:553毫秒
        5000000050000000
        耗费时间为:296毫秒

        累加100亿:
        -5340232216128654848
        耗费时间为:19193毫秒
        -5340232216128654848
        耗费时间为:21043毫秒
        */


        /**
         * 但是ForkJoin框架应用的不广泛,因为操作起来非常的麻烦,所以java8改进了,
         * 下面看java8中怎么完成
         */
        //java8并行流
        Instant start2 = Instant.now();
        Long sum2 = LongStream.rangeClosed(0,10000000000L)
                .parallel()//底层是ForkJoin
                .reduce(0,Long::sum);
        System.out.println(sum2);
        Instant end2 = Instant.now();
        System.out.println("耗费时间为:"+Duration.between(start2, end2).toMillis()+"毫秒");
        /*可以看到明显快了
        -5340232216128654848
        耗费时间为:13761毫秒*/
    }
}

标签:end,并行,long,start,Instant,import,串行,java,Java8
来源: https://blog.csdn.net/ZHOU_VIP/article/details/121708558