public List<CbepCbeBody> queryData(String mainId,long count){
List<CbepCbeBody> list = new ArrayList<>();
int pageSize = 20000; // 每个子任务查询的数据量
int threadCount = 6; // 线程数,可根据实际情况调整
// 计算总页数
int totalPages = (int) Math.ceil((double) count / pageSize);
// 创建线程池
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
// 创建查询任务列表
List<Callable<List<CbepCbeBody>>> tasks = new ArrayList<>();
for (int currentPage = 1; currentPage <= totalPages; currentPage++) {
int offset = (currentPage - 1) * pageSize;
tasks.add(new QueryTask(mainId,offset,pageSize,iCbepCbeBodyService));
}
try {
// 提交任务并获取Future结果
List<Future<List<CbepCbeBody>>> futures = executor.invokeAll(tasks);
// 处理查询结果
for (Future<List<CbepCbeBody>> future : futures) {
List<CbepCbeBody> result = future.get();
// 对每个查询的结果进行处理
list.addAll(result);
}
// 使用流对List进行升序排序
List<CbepCbeBody> sortedList = list.stream()
.sorted(Comparator.comparing(CbepCbeBody::getLogisticsno))
.collect(Collectors.toList());
return sortedList;
}catch (Exception e){
e.printStackTrace();
}finally {
// 关闭线程池
executor.shutdown();
}
return list;
}
static class QueryTask implements Callable<List<CbepCbeBody>> {
private ICbepCbeBodyService iCbepCbeBodyService;
private String mainId;
private int offset;
private int limit;
public QueryTask(String mainId,int offset, int limit,ICbepCbeBodyService iCbepCbeBodyService) {
this.mainId = mainId;
this.offset = offset;
this.limit = limit;
this.iCbepCbeBodyService = iCbepCbeBodyService;
}
@Override
public List<CbepCbeBody> call() throws Exception {
return iCbepCbeBodyService.list(new QueryWrapper<CbepCbeBody>().eq(CbepCbeBody.MAIN_ID, mainId).eq(CbepCbeBody.DEL_FLAG, "1").last(" limit "+offset+","+limit));
}
}
其他创建任务的方式
//创建任务
Callable<List<String>> task1 = new Callable<List<String>>() {
@Override
public List<String> call() throws Exception {
//校验订单
return cbepCbeHeadMapper.checkOrderno(mainId);
}
};