代码优化:CompletableFuture + countDownLatch + Vector

需求:根据查询出来的一批合同编号,查询每个合同对应流程节点的最后审批人,最后导出合同编号及其对应的最后审批人。

本次代码优化总共分为以下四步骤:

  1. 遍历查询数据库
  2. 使用CompletableFuture将串行改为并行
  3. 简化代码,并引出线程安全问题
  4. CompletableFuture + countDownLatch + Vector

在这里插入图片描述
最终版代码如下:

public List<ContractMan> selectContractMan() throws InterruptedException{
    CountDownLatch countDownLatch = new CountDownLatch(5);
    List<String> contractNumers= zhbExcelMapper.selectContractNumer();
    Vector<ContractMan> man=new Vector<>();

    List<List<String>> lists = averageAssign(contractNumers, 5);

    for(int i=0;i<lists.size();i++){
        int finalI = i;
        CompletableFuture future1 = CompletableFuture.supplyAsync(() -> {
            List<String> stringList = lists.get(finalI);
            for(String s:stringList){
                ContractMan contractMan =new ContractMan();
                contractMan.setContractNumber(s);
                String name= zhbExcelMapper.selectMan(s);
                contractMan.setName(name);
                man.add(contractMan);
            }
            return Boolean.TRUE;
        }).whenComplete((v,e)->{
            countDownLatch.countDown();
        });
    }

    countDownLatch.await();
    return man;
}
  1. 在并发过程中,CountDownLatch减少至 0 的前提下,执行相关操作。
  2. CompletableFuture 使得我们的任务单独运行在与主线程分离的其他线程中,并且通过回调可以在主线程中得到异步任务的执行状态,是否完成,和是否异常等信息。
  3. 当CompletableFuture 完成时 CountDownLatch 执行 countDown() 方法,全部完成后,countDownLatch.await(); 放行

评论区:单纯使用CompletableFuture实现

        public List<ContractMan> selectContractMan() throws InterruptedException, ExecutionException {
        List<String> contractNumers= zhbExcelMapper.selectContractNumer();

        List<ContractMan> man=new ArrayList<>();

        List<List<String>> lists = averageAssign(contractNumers, 5);

        CompletableFuture[] futureArray = lists.stream()
                .map(idList -> CompletableFuture.supplyAsync(()->{
                            List<ContractMan> subMan=new ArrayList<>();
                            idList.forEach(i->{
                                ContractMan contractMan =new ContractMan();
                                contractMan.setContractNumber(i);
                                String name= zhbExcelMapper.selectMan(i);
                                contractMan.setName(name);
                                subMan.add(contractMan);
                            });
                            return subMan;
                        }
                )).toArray(CompletableFuture[]::new);
        CompletableFuture.allOf(futureArray).join();
        for (CompletableFuture t : futureArray) {
            man.addAll((Collection<? extends ContractMan>) t.get());
        }
        return man;
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值