我有Callable
列表,我想supplyAsync
到CompletableFuture
,但是当我在join()
上进行CompletableFuture
时,我得到null
class CustomCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
return 2;
}
}
List<Callable> callables = new ArrayList<>();
callables.add(new CustomCallable());
CompletableFuture<?>[] futures = callables.stream().map(x -> CompletableFuture.supplyAsync(() -> {
try {
return x.call();
} catch (Exception e) {
e.printStackTrace();
return null;
}
})).toArray(CompletableFuture[]::new);
System.out.println(CompletableFuture.allOf(futures).join()); --> This line prints `null`