计算大数据量的进阶

计算10亿的数字求和

普通计算:

    public static void main(String[] args) {
        Long sum = 0l;
        long start = System.currentTimeMillis();
        for (long i = 1; i <= 10_0000_0000l; i++) {
            sum += i;
        }
        long end =System.currentTimeMillis();
        System.out.println(sum);
        System.out.println((end - start));
    }

在这里插入图片描述
耗时:5.5秒

ForkJoin计算:

ForkJoin的工具类(递归拆分任务,双向队列任务窃取)

public class ForkJoinDemo1 extends RecursiveTask<Long> {
    private Long start;
    private Long end;
    //临界值
    private Long temp=1000L;

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

    @Override
    protected Long compute() {
        if ((end - start) < temp) {
            Long sum = 0l;
            for (Long i = start; i <= end; i++) {
                sum += i;
            }
            return sum;
        } else {
            long middle = (start + end) / 2;
            ForkJoinDemo1 task1= new ForkJoinDemo1(start, middle);
            task1.fork();//拆封任务,吧任务压入线程队列
            ForkJoinDemo1 task2= new ForkJoinDemo1(middle, end);
            task2.fork();//拆封任务,吧任务压入线程队列
            return task1.join() + task2.join();
        }

    }
}

调用

public class Test {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> forkJoinDemo1 = new ForkJoinDemo1(0L, 10_0000_0000L);
        ForkJoinTask<Long> submit = forkJoinPool.submit(forkJoinDemo1);
        try {
            System.out.println(submit.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        long end =System.currentTimeMillis();
        System.out.println((end - start));
    }
}

在这里插入图片描述
耗时:10秒(可能是电脑的缘故 理论应该是比普通的快)

流式计算

public class Test{
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        System.out.println(LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0, Long::sum));
        long end =System.currentTimeMillis();
        System.out.println((end - start));
    }
}

在这里插入图片描述
耗时:0.5秒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值