List<CompletableFuture> futureList = new ArrayList<>();
List<Info> resultList = new ArrayList<>();
for (int i=1;i<pageSize;i++){
// 启线程查, 使用定长线程池
int j = i;
CompletableFuture<List<Map<String, Object>>> future1 = CompletableFuture.supplyAsync(()->{
param.put("page", j+1);
log.info("url===>" + url);
log.info("param===>{}", JSON.toJSONString(param));
String respInner = myHttpClient.doPost(url, JSON.toJSONString(param));
JSONObject jsonInner = JSON.parseObject(respInner);
String resultInner = jsonInner.getString("result");
JSONObject jsonResultInner = JSON.parseObject(resultInner);
String listStrInner = jsonResultInner.getString("list");
List<Map<String, Object>> listMapInner = JSON.parseObject(listStrInner, List.class);
return listMapInner;
},ThreadPoolUtils.getScheduledExec());
future1.thenAccept(results -> {
resultLists.addAll(results);
});
futureList.add(future1);
}
// 合并任务
CompletableFuture<Void> combinedFutures = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()]));
// 等待执行
combinedFutures.whenComplete((v,th)->{
System.out.println("all completed");
}).join();