第一步:申明线程池
public ExecutorService taskExecutor = new ThreadPoolExecutor(6, Integer.MAX_VALUE, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
第二步:分批处理数据
// 每批次数量
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();
————————————————
原文链接:https://blog.csdn.net/wu_wenjie/article/details/120867357
Java批量数据采用线程池分批同步处理
最新推荐文章于 2024-07-30 20:50:32 发布