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