代码优化:CompletableFuture + countDownLatch + Vector

Posted root_zhb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码优化: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;
    

以上是关于代码优化:CompletableFuture + countDownLatch + Vector的主要内容,如果未能解决你的问题,请参考以下文章

优化排查-线程阻塞:CompletableFuture 和 DiscardPolicy

异步编程利器:CompletableFuture详解

异步编程利器:CompletableFuture如何操作?

异步编程CompletableFuture实现高性能系统优化之请求合并

更新丢失:当HashMap遇见CompletableFuture

多线程之CompletableFuture全方面解析