zoukankan      html  css  js  c++  java
  • 多线程使用之CompletableFuture

    public List<ComponentDataDTO> getComponentMinAndMaxData(String tableName, List<AnchorAndComponentDO> anchorAndComponentDOList, LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
            List<ComponentDataDTO> componentDataDTOList = new ArrayList<>();
            ConcurrentHashMap<String,List<ComponentDataDTO>> componentDataDTOMap = new ConcurrentHashMap<>();
            if(anchorAndComponentDOList!=null && !anchorAndComponentDOList.isEmpty()){
                //多线程调用
                List<CompletableFuture<Boolean>> completableFutureList = anchorAndComponentDOList.stream().map((AnchorAndComponentDO anchorComponent)->
                        CompletableFuture.supplyAsync(()->{
                            try{
                                //调用处理
                                List<ComponentDataDTO> componentMinAndMaxDataList = componentMapper.getComponentMinAndMaxData(tableName, anchorComponent, startLocalDateTime, endLocalDateTime);
                                componentDataDTOMap.put(anchorComponent.getAnchorId()+":"+anchorComponent.getComponentId(),componentMinAndMaxDataList);
                            }catch (Exception e){
                                //记录备注Map
                                logger.error("监测点:"+anchorComponent.getAnchorId()+",监测项:"+anchorComponent.getComponentId()+",获取数据报错:"+e+"-"+e.getMessage());
                                return false;
                            }
                            return true;
                        })).collect(Collectors.toList());
                List<Boolean> collect = completableFutureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
                if(!collect.contains(false)){
                    componentDataDTOMap.forEach((k, v) -> componentDataDTOList.addAll(v));
                }
            }
            return componentDataDTOList;
        }
  • 相关阅读:
    分母为0一定会抛异常吗?
    [译]Zookeeper的优点与局限性
    明明有class为什么还是报ClassNotFoundException?
    广告倒排索引架构与优化
    KafkaProducer源码分析
    Kafka服务端之网络连接源码分析
    Sublime常用快捷键
    sublime主题设置
    Sublime前端插件
    安装软件,更新软件,删除软件
  • 原文地址:https://www.cnblogs.com/panbingqi/p/14467663.html
Copyright © 2011-2022 走看看