public class AnalysisSalseTask implements Callable<AnalysisSalesVo> {
private TbAnalysisSalesMapper baseMapper;
private String startTime;//分段查询开始时间
private String endTIme;//分段查询结束时间
private String terminalCodes;//要查询的门店编码
//构造方法
public AnalysisSalseTask(String startTime, String endTIme, String terminalCodes,TbAnalysisSalesMapper baseMapper) {
this.baseMapper=baseMapper;
this.startTime = startTime;
this.endTIme = endTIme;
this.terminalCodes = terminalCodes;
}
//分段时间及门店编码查询数据的方法
@Override
public AnalysisSalesVo call() throws Exception {
//设置要查询的参数
AnalysisSalesVo analysisSalesVo = new AnalysisSalesVo();
try{
analysisSalesVo.setStartTime(startTime);
analysisSalesVo.setEndTime(endTIme);
analysisSalesVo.setTerminalCodes(terminalCodes);
//查询数据
analysisSalesVo = baseMapper.getAnalysisSales(analysisSalesVo);
}
catch (Exception e){
e.printStackTrace();
return analysisSalesVo;
}
//返回查询结果
return analysisSalesVo;
}
业务代码
public AnalysisSalesVo analysisSalesTask(List<AnalysisSalesVo> list) {
List<AnalysisSalesVo> result = new ArrayList<>();
List<Callable<AnalysisSalesVo>> tasks = new ArrayList<Callable<AnalysisSalesVo>>();
for (int i = 0; i < list.size(); i++){
Callable<AnalysisSalesVo> task = new AnalysisSalseTask(list.get(i).getStartTime(), list.get(i).getEndTime(),
list.get(i).getTerminalCodes(), baseMapper);
tasks.add(task);
}
ExecutorService execservice = Executors.newFixedThreadPool(4);
try{
List<Future<AnalysisSalesVo>> futures = execservice.invokeAll(tasks);
if (futures != null && futures.size() > 0){
for (Future<AnalysisSalesVo> future : futures){
result.add(future.get());
}
}
execservice.shutdown(); // 关闭线程池
}
catch (InterruptedException e){
e.printStackTrace();
}
catch (ExecutionException e){
e.printStackTrace();
}
return sumAnalysisSalesVo(result, new AnalysisSalesVo());
}
多线程查询之前两个月数据大概100s,使用之后35s左右,虽然时间还是比较久,但时间大大小于需求文档时间。查询联查3个表,数据分别在3700w 2400W 1000W