List<BatchProductsBO> batchProductsBOList = new ArrayList<>();
Map<String,List<String>> mapAll = new HashMap<>();
long start = System.currentTimeMillis();
int capacity = 300;
List<List<BatchProductsBO>> perList = ListUtil.splitListBycapacity(batchProductsBOS, capacity);
log.info("批量业务开始多线程执行,线程数:{}", perList.size());
ExecutorService threadPool = Executors.newFixedThreadPool(perList.size());
for (int i = 0; i < perList.size(); i++) {
try {
int finalI = i;
threadPool.execute(() -> {
log.info("本次批量业务数据{},执行{}条数据", finalI, perList.get(finalI).size());
Map<String,List<String>> map = dealDataAll(perList.get(finalI),finalI);
batchProductsBOList.addAll(perList.get(finalI));
mapAll.putAll(map);
long perEnd = System.currentTimeMillis();
log.info("本次批量业务数据{},执行{}条数据,花费[{}]秒", finalI, perList.get(finalI).size(), (perEnd - start) / 1000);
});
} catch (Exception e) {
log.error("本次批量导入数据[{}]线程执行异常:{}", i, e.getMessage());
}
}
threadPool.shutdown();
while (true) {
if (threadPool.isTerminated()) {
log.info("所有的子线程都结束了!");
if(CollectionUtils.isEmpty(mapAll)){
List<BatchProductsPO> batchProductsPOS = JsonUtil.copyList(batchProductsBOList,BatchProductsPO.class);
this.saveBatch(batchProductsPOS);
}
log.info("所有批量业务数据插入执行完毕");
break;
}
}
return ResponseBuild.buildSuccResponse(mapAll);
public static <T> List<List<T>> splitListBycapacity(List<T> source,int capacity){
List<List<T>> result=new ArrayList<List<T>>();
if (source != null){
int size = source.size();
if (size > 0 ){
for (int i = 0; i < size;) {
List<T> value = null;
int end = i+capacity;
if (end > size){
end = size;
}
value = source.subList(i,end);
i = end;
result.add(value);
}
}else {
result = null;
}
}else {
result = null;
}
return result;
}