• Java CompletableFuture:allOf等待所有异步线程任务结束


    /**
     * 使用java8 CompletableFuture.runAsync实现 多线程处理,获取治理任务明细
     * 使用线程安全list synchronizedList
     * @param env
     * @param systemEnNameList
     * @param ldcList
     * @param instanceList
     * @param strategyMap
     * @param actStrategyMap
     * @param unActStrategyMap
     * @param activitedSystemList
     * @param unActivitedSystemList
     * @return
     */
    private List<ResourceRuleTaskDetail> getTaskDetailsUseThreads(String env, List<String> systemEnNameList, List<String> ldcList, List<String> instanceList, Map<String, JSONArray> strategyMap, Map<String, JSONArray> actStrategyMap, Map<String, JSONArray> unActStrategyMap, List<String> activitedSystemList, List<String> unActivitedSystemList) {
    	// 对于集合写操作:synchronizedList 相对于Vector 、CopyOnWriteArrayList性能更佳。 读操作建议CopyOnWriteArrayList
    	List<ResourceRuleTaskDetail> resourceRuleTaskDetailList = Collections.synchronizedList(new ArrayList<ResourceRuleTaskDetail>());
    	CompletableFuture<Void> cf1 = CompletableFuture.runAsync(()->{
    		if (MapUtils.isNotEmpty(strategyMap)) {
    			List<ResourceRuleTaskDetail> strategyTaskDetailList = resourceDealStrategyNewService.dealStrategyToGetGovernDataNew(systemEnNameList, ldcList, env, strategyMap, instanceList);
    			resourceRuleTaskDetailList.addAll(strategyTaskDetailList);
    		}
    	});
    
    	CompletableFuture<Void> cf2 = CompletableFuture.runAsync(()->{
    		if (MapUtils.isNotEmpty(actStrategyMap) && CollectionUtils.isNotEmpty(activitedSystemList)) {
    			List<ResourceRuleTaskDetail> actStrategyTaskDetailList = resourceDealStrategyNewService.dealStrategyToGetGovernDataNew(activitedSystemList, ldcList, env, actStrategyMap, instanceList);
    			resourceRuleTaskDetailList.addAll(actStrategyTaskDetailList);
    		}
    	});
    	CompletableFuture<Void> cf3 = CompletableFuture.runAsync(()->{
    		if (MapUtils.isNotEmpty(unActStrategyMap) && CollectionUtils.isNotEmpty(unActivitedSystemList)) {
    			List<ResourceRuleTaskDetail> unActStrategyTaskDetailList = resourceDealStrategyNewService.dealStrategyToGetGovernDataNew(unActivitedSystemList, ldcList, env, unActStrategyMap, instanceList);
    			resourceRuleTaskDetailList.addAll(unActStrategyTaskDetailList);
    		}
    	});
    	// join等待所有线程执行完成
    	CompletableFuture.allOf(cf1, cf2, cf3).join();
    	return resourceRuleTaskDetailList;
    }
    每天一点点,惊喜不间断
  • 相关阅读:
    函数/方法的活动对象
    Function 详解(一)
    两栏自适应布局延展到多栏自适应布局
    height百分比以及高度自适应问题
    clearfix为什么用display:table,而不用display:block
    Nodejs写的搬家工具知识分享
    终于把自己多年前的百度文章搬到博客园了
    用Razor来生成模板 using razor for template
    一个request,但是controller被执行了多次的问题
    33个好用的图片轮显 jquery图片轮显
  • 原文地址:https://www.cnblogs.com/wszn-java/p/14981400.html
Copyright © 2020-2023  润新知