使用多线程分别请求,然后将结果聚合处理【项目使用】
使用多线程分别请求,然后将结果聚合处理,提高效率,减少请求时间
ExecutorService service = Executors.newSingleThreadExecutor(); ExecutorCompletionService<List<CityRankInfo>> ece = new ExecutorCompletionService<>(service); ece.submit(() -> { return iHotelRankOutService.searchCityRankInfoByRankType(1, req.getTrackid()); }); ece.submit(() -> { return iHotelRankOutService.searchCityRankInfoByRankType(2, req.getTrackid()); }); ece.submit(() -> { return iHotelRankOutService.searchCityRankInfoByRankType(3, req.getTrackid()); }); Future<List<CityRankInfo>> take1 = ece.take(); Future<List<CityRankInfo>> take2 = ece.take(); Future<List<CityRankInfo>> take3 = ece.take(); if (null != take1) { rankInfosList.addAll(take1.get()); } if (null != take2) { rankInfosList.addAll(take2.get()); } if (null != take3) { rankInfosList.addAll(take3.get()); } //然后对rankInfoList进行处理即可