Java批量数据采用线程池分批同步处理

Java批量数据采用线程池分批同步处理,第1张

Java批量数据采用线程分批同步处理
第一步:申明线程池
public ExecutorService taskExecutor = new ThreadPoolExecutor(6, Integer.MAX_VALUE, 0, TimeUnit.MILLISECONDS, new SynchronousQueue());

第二步:分批处理数据

// 每批次数量
Integer batchCount = 100;Integer start = 0;
// 结尾
List list=new ArrayList<>();
Integer end = 100;
int loopCount = (int) Math.ceil((float) list.size() / batchCount);

// 采用线程池的同步计数类
CountDownLatch countDownLatch = new CountDownLatch(loopCount);
for (int k = 0; k < loopCount; k++) {
    final Integer startTemp = start;
    final Integer endTemp = end;
    // 开启线程池
    taskExecutor.execute(new Runnable() {
        @Override
        public void run() {
            try {
               //todo 书写业务代码逻辑

            } catch (Exception ex) {
                LOGGER.error("getQwAllUserInfo error:", ex);
            } finally {
                countDownLatch.countDown();
            }
        }
    });
    //计算下次分片数据
    start = end;
    end = start + batchCount > list.size() ? list.size() : batchCount + start;
}

最后等所有线程数据处理完毕

countDownLatch.await();

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4674040.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-07
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存