关于使用Future多线程处理多个任务并获取所有返回值,堵塞获取所有子线程的返回值。

创建一个线程池:

    // 全局异步执行的线程池
    @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("线程执行异常");
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值