Java for循环与ForkJoin与并行流的比较(以1累加到10亿为例)

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

public class addTest {
    public static void test1(){
        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));
    }
    public static void test2() throws ExecutionException, InterruptedException {
        long sum=0L;
        long start=System.currentTimeMillis();
        ForkJoinPool forkJoinPool=new ForkJoinPool();
        ForkJoinTask<Long> forkJoinTask=new ForkJoinDemo(0L,10_0000_0000L);
        ForkJoinTask<Long> submit=forkJoinPool.submit(forkJoinTask);
        sum=submit.get();
        long end=System.currentTimeMillis();
        System.out.println("sum:"+sum+",Time:"+(end-start));
    }
    public static void test3(){
        long start=System.currentTimeMillis();
        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));
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        test1();
        test2();
        test3();
    }
}

class ForkJoinDemo extends RecursiveTask<Long> {
    private long start;
    private long end;

    private long temp=10000000L;
    public ForkJoinDemo(long start, long end) {
        this.start = start;
        this.end = end;
    }
    @Override
    protected Long compute() {
        long sum=0L;
        if((end-start)<temp){
            for(long i=start;i<=end;i++){
                sum+=i;
            }
            return sum;
        }else{
            long middle=(start+end)/2;
            ForkJoinDemo task1=new ForkJoinDemo(start,middle);
            task1.fork();
            ForkJoinDemo task2=new ForkJoinDemo(middle+1,end);
            task2.fork();
            return task1.join()+task2.join();
        }
    }
}

ForkJoin特点:任务窃取,维护了一个双端队列,先执行完毕的线程抢未执行完的线程,会存在抢占问题。大数据量使用。

package com.example.goose.util;
import lombok.extern.slf4j.Slf4j;


import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

@Slf4j
public class TestJUC7 {
    public static void main(String[] args) {
        ForkJoinPool pool= new ForkJoinPool(4);
        System.out.println(pool.invoke(new Te(5)));
    }
}
class Te extends RecursiveTask<Integer>{
    private int n=0;
    public Te(int n){
        this.n=n;
    }
    @Override
    protected Integer compute() {
        if(n==1){
            return 1;
        }
        Te t1=new Te(n-1);
        t1.fork();
        int result=n+t1.join();
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值