java解决大量数据处理速度问题

1、使用线程池分批次同步处理数据

List<Data> beanList = mapper.getDataList(param);
    if(beanList != null && beanList.size()>0){
        int totalSize = beanList.size();
        int batchSize = 1000;
        
        ExecutorService executor = Executors.newFixedThreadPool(10);
        int numBatches = (int) Math.ceil((double) totalSize / batchSize);
        
        for (int i = 0; i < numBatches; i++) {
            int startIndex = i * batchSize;
            int endIndex = Math.min(startIndex + batchSize, totalSize);
            List<Data> batchList = Lists.newArrayList();
            for (int j = startIndex; j < endIndex; j++) {
                batchList.add(beanList.get(j));
            }
            // 提交任务给线程池
            executor.execute(() -> {
                // 执行更新操作
                update(batchList);
            });
        }
        // 关闭线程池
        executor.shutdown();
    }

2、使用java8,parallelStream流

List<Data> list = mapper.listData();

list.parallelStream().forEach(data ->{
    update(data)
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值