零度星河
返回
发布于 更新于 阅读:119 评论:0

java 异步查询统计提高效率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
List<String> idList = Lists.newArrayList("111");
List<CompletableFuture<Integer>> batchFutures = new ArrayList<>();
for (String id : idList) {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
Integer num = 0;//这里模拟查询统计操作
return num;
});
batchFutures.add(future);
}
 
CompletableFuture<List<Integer>> all = CompletableFuture.allOf(batchFutures.toArray(new CompletableFuture[0]))
.thenApply(v -> batchFutures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()));
List<Integer> integers = null;
try {
integers = all.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
 
int num = 0;
for (Integer integer : integers) {
if (integer != null) {
num += integer;
}
}
评论 (0)
暂无评论 来抢沙发吧~

目录

暂无目录

SpringWonderland

探索 Spring 生态的奇妙世界

2025-07-24 16:35

我的标签

随笔分类

归档

查看完整归档 →