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;
}
}