Felix.Sung
7/26/2019 - 7:11 AM

JDK8#CompletableFuture

JDK8#CompletableFuture

CompletableFuture[] completableFuture = cityList.stream().map(adcode ->
                CompletableFuture.supplyAsync(()-> getLiveWeather(amapApiRateLimiter , adcode , amapLiveWeatherHandler), executorService)
                .thenAccept(weather -> {
                    if(weather != null){
                        if(AMAP_BAD_WEATHER.contains(weather.getWeather())){
                            log.warn("极端天气:{}" , JSON.toJSONString(weather,false));
                        }else{
                            log.info("weather : " + JSON.toJSONString(weather,false));
                        }
                    }
                }).whenComplete((v,e) ->{

                })).toArray(CompletableFuture[]::new);
        //等待总任务完成,但是封装后无返回值,必须自己whenComplete()获取
        CompletableFuture.allOf(completableFuture).join();