创建一个线程池:
// 全局异步执行的线程池
@Bean(name = "globalAsyncThreadPool")
public ThreadPoolExecutor globalAsyncThreadPool() {
return new ThreadPoolExecutor(10, 100, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10000), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
}
可写在启动类中,在各层的调用;
@Resource(name = "globalAsyncThreadPool")
private ThreadPoolExecutor globalAsyncThreadPool;
之后便是执行子线程:
Future<AjaxResult> t1= globalAsyncThreadPool.submit(() -> {
//业务代码,必须有返回值
return AjaxResult.success('返回值');
});
示例,可写多个:
Future<AjaxResult> t1= globalAsyncThreadPool.submit(() -> {
Map<String, ICFaInvestigationNorm> normMap = null;
try {
normMap = varianceAnalysisService.getInvestigationData(investigationId, normAppendMap);
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
return AjaxResult.success(normMap);
});
再就是获取执行结果,可用一个try包起来,再try外定义接收返回值的变量,在内部通过get()方法获取结果后,赋值给返回值变量即可:
Map<String, ICFaInvestigationNorm> normMap = null;
List<ICBmLevel> targetLevels = null;
List<ICBmLevel> allLevels = null;
Map<String, Double> levelDetailMap = null;
List<ICBmEvaluationTitleDTO> icBmEvaluationTitleList = null;
try {
AjaxResult investigationResult = investigationFuture.get();
if((Integer)investigationResult.get(AjaxResult.CODE_TAG) != HttpStatus.SUCCESS) {
return investigationResult;
}
AjaxResult targetLevelsResult = targetLevelsFuture.get();
if((Integer)targetLevelsResult.get(AjaxResult.CODE_TAG) != HttpStatus.SUCCESS) {
return targetLevelsResult;
}
AjaxResult allLevelsResult = allLevelsFuture.get();
if((Integer)allLevelsResult.get(AjaxResult.CODE_TAG) != HttpStatus.SUCCESS) {
return allLevelsResult;
}
AjaxResult levelDetailResult = levelDetailFuture.get();
if((Integer)levelDetailResult.get(AjaxResult.CODE_TAG) != HttpStatus.SUCCESS) {
return levelDetailResult;
}
AjaxResult evaluationTitleResult = evaluationTitleFuture.get();
if((Integer)evaluationTitleResult.get(AjaxResult.CODE_TAG) != HttpStatus.SUCCESS) {
return evaluationTitleResult;
}
normMap = (Map<String, ICFaInvestigationNorm>)investigationResult.get(AjaxResult.DATA_TAG);
targetLevels = (List<ICBmLevel>)targetLevelsResult.get(AjaxResult.DATA_TAG);
allLevels = (List<ICBmLevel>)allLevelsResult.get(AjaxResult.DATA_TAG);
levelDetailMap = (Map<String, Double>)levelDetailResult.get(AjaxResult.DATA_TAG);
icBmEvaluationTitleList = (List<ICBmEvaluationTitleDTO>)evaluationTitleResult.get(AjaxResult.DATA_TAG);
} catch (InterruptedException e) {
e.printStackTrace();
return AjaxResult.error("线程中断");
} catch (ExecutionException e) {
e.printStackTrace();
return AjaxResult.error("线程执行异常");
}
更新,22.9.14
可将子线程返回值的报错不用return,而使用抛出
Integer targetLevelCode = null;
List<ICBmLevel> allLevels = null;
Integer firstNormValue = 0;
try {
AjaxResult targetLevelCodeResult = targetLevelCodeFuture.get();
if((Integer)targetLevelCodeResult.get(AjaxResult.CODE_TAG) != HttpStatus.SUCCESS) {
throw new Exception(String.valueOf(targetLevelCodeResult.get(AjaxResult.MSG_TAG)));
}
AjaxResult allLevelsResult = allLevelsFuture.get();
if((Integer)allLevelsResult.get(AjaxResult.CODE_TAG) != HttpStatus.SUCCESS) {
throw new Exception(String.valueOf(allLevelsResult.get(AjaxResult.MSG_TAG)));
}
AjaxResult firstNormResult = firstNormFuture.get();
if((Integer)firstNormResult.get(AjaxResult.CODE_TAG) !=HttpStatus.SUCCESS) {
throw new Exception(String.valueOf(firstNormResult.get(AjaxResult.MSG_TAG)));
}
targetLevelCode = (Integer)targetLevelCodeResult.get(AjaxResult.DATA_TAG);
allLevels = (List<ICBmLevel>)allLevelsResult.get(AjaxResult.DATA_TAG);
firstNormValue = (Integer)firstNormResult.get(AjaxResult.DATA_TAG);
} catch (InterruptedException e) {
e.printStackTrace();
throw new Exception("线程中断");
} catch (ExecutionException e) {
e.printStackTrace();
throw new Exception("线程执行异常");
}