CompletableFuture
List<Object> res = new ArrayList<>();
CompletableFuture<?> completableFuture1 = CompletableFuture.runAsync(()->{
});
CompletableFuture<?> completableFuture2 = CompletableFuture.runAsync(()->{
});
CompletableFuture all = CompletableFuture.allOf(completableFuture1,completableFuture2);
while (true){
if(all.isDone()){
break;
}
}
long b = System.currentTimeMillis();
return String.valueOf(b-a);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test
@SneakyThrows
public void ah2() {
List<CompletableFuture<Void>> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(CompletableFuture.runAsync(() -> {
System.out.println(Thread.currentThread().getName());
}));
}
CompletableFuture.allOf(list.toArray(new CompletableFuture[0])).join();
System.out.println("aaaaa");
Thread.sleep(2000L);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14