Java CompletableFuture没有给出Callable的call()结果

时间:2018-10-17 21:36:54

标签: java multithreading concurrency java.util.concurrent

我有Callable列表,我想supplyAsyncCompletableFuture,但是当我在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`

0 个答案:

没有答案