Java多线程处理集合数据

Java利用CountDownLatch和ExecutorService实现多线程处理集合数据问题


以下代码段可以自行选择在需要使用多线程的代码上下文时插入


// 一百条为基准为一个线程处理
// 1.使用集合切割工具类分割需要处理的数组,BSOwnerPerson为你需要处理的数组对象
List<List<BSOwnerPerson>> groupList = CollectionUtils.partition(bsOwnerPersonList, 100);
CountDownLatch countDownLatch = new CountDownLatch(groupList.size());
ExecutorService executorService = Executors.newFixedThreadPool(groupList.size());
// 2.根据分组长度循环处理
for (int i = 0; i < groupList.size(); i++) {
int finalI = i;
executorService.execute(() -> {
List<BSOwnerPersonNaturalPersonDTO> BSOwnerPersonNaturalPersonGroup = groupList.get(finalI);
for (BSOwnerPersonNaturalPersonDTO ownerPersonNaturalPersonDTO : BSOwnerPersonNaturalPersonGroup) {
// 业务内容,你需要对每个对象做的处理
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown(); //关闭线程池

单独新建一个数组分段处理的工具类

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @Author lingyang
* @Description 集合工具类
* @Date 2021/9/27 9:28
* @Version 1.0
*/
public class CollectionUtils
{
/**
* 集合按长度分组
*
* @param list 集合
* @param size 分割大小,100则为每100条数据为一组
* @param <T>
* @return
*/
public static <T> List<List<T>> partition(final List<T> list, final int size) {
if (list == null) {
throw new IllegalArgumentException("List must not be null");
}
if (size <= 0) {
throw new IllegalArgumentException("Size must be greater than 0");
}
List<List<T>> result = new ArrayList<>();
Iterator<T> it = list.iterator();
List<T> subList = null;
while (it.hasNext()) {
if (subList == null) {
subList = new ArrayList<>();
}
T t = it.next();
subList.add(t);
if (subList.size() == size) {
result.add(subList);
subList = null;
}
}
//补充最后一页
if (subList != null) {
result.add(subList);
}
return result;
}
}
posted @   luckyangg  阅读(2517)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示