如何获得期货清单的结果

use*_*502 1 java

我有期货清单

List<Future<Boolean>> futures = service.invokeAll( Arrays.asList( callable1, callable2 ));
Run Code Online (Sandbox Code Playgroud)

我需要的是一种获取结果列表的方法

可以提供Java解决方案吗?

就像是 whenAll()...

Vam*_*ire 5

您所追求的是这样的allMatch()方法:

boolean result = futures.stream().allMatch(booleanFuture -> {
    try
    {
        return booleanFuture.get();
    }
    catch (InterruptedException | ExecutionException e)
    {
        throw new RuntimeException(e);
    }
});
Run Code Online (Sandbox Code Playgroud)

如果您真的想要结果列表,那么map()您将是这样:

List<Boolean> results = futures.stream().map(booleanFuture -> {
    try
    {
        return booleanFuture.get();
    }
    catch (InterruptedException | ExecutionException e)
    {
        throw new RuntimeException(e);
    }
}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)