List<GroupSubCodeEntity> records = groupSubCode.getRecords();
CompletableFuture<GroupListingDTO>[] completableFutures = records.stream().map(e -> CompletableFuture.supplyAsync(() -> {
GroupListingDTO dto = new GroupListingDTO();
BeanUtil.copyProperties(e, dto);
//计算此群人数
JSONObject list = subCodeService.getList(e.getChatId());
int count = 1 ;
if (LinkeWechat.OK.equals(list.get(LinkeWechat.ERRMSG))) {
count = list.getJSONObject(LinkeWechat.GROUP_CHAT).getJSONArray(LinkeWechat.MEMBER_LIST).size();
}
dto.setNumber(count);
return dto;
},threadPoolBuilder.getThreadPoolExecutor())).toArray(CompletableFuture[]::new);
// 发起异步
CompletableFuture.allOf(completableFutures).join();
// 汇总CompletableFuture::join
List<GroupListingDTO> dtos = Arrays.stream(completableFutures).map(CompletableFuture::join).collect(Collectors.toList());
多线程同时执行,汇总后继续执行
异步处理群组成员列表获取与计数
最新推荐文章于 2025-11-26 15:59:01 发布
文章描述了如何使用JavaCompletableFuture流和线程池执行异步操作,从数据库获取群组子代码实体,计算每个群组的人数,并将结果存储在GroupListingDTO对象中。
981

被折叠的 条评论
为什么被折叠?



