繁体   English   中英

您如何访问传递给 CompletableFuture allOf 的已完成期货?

[英]How do you access completed futures passed to CompletableFuture allOf?

我正在尝试掌握 Java 8 CompletableFuture。 我怎样才能将这些加入到人中并在“allOf”之后将它们归还。 下面的代码不起作用,但可以让您了解我尝试过的内容。

在 javascript ES6 我会做

Promise.all([p1, p2]).then(function(persons) {
   console.log(persons[0]); // p1 return value     
   console.log(persons[1]); // p2 return value     
});

到目前为止我在Java的努力

public class Person {

        private final String name;

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

    }

@Test
public void combinePersons() throws ExecutionException, InterruptedException {
    CompletableFuture<Person> p1 = CompletableFuture.supplyAsync(() -> {
        return new Person("p1");
    });

    CompletableFuture<Person> p2 = CompletableFuture.supplyAsync(() -> {
        return new Person("p1");
    });

    CompletableFuture.allOf(p1, p2).thenAccept(it -> System.out.println(it));

}

CompletableFuture#allOf方法不会公开传递给它的已完成CompletableFuture实例的集合。

返回一个新的CompletableFuture ,它在所有给定的CompletableFuture完成时完成。 如果任何给定的CompletableFuture异常完成,则返回的CompletableFutureCompletableFutureCompletionException将此异常作为其原因。 否则,给定CompletableFuture的结果(如果有)不会反映在返回的CompletableFuture ,但可以通过单独检查它们来获得 如果没有提供CompletableFuture s,则返回一个CompletableFuture完成,值为null

请注意, allOf还将异常完成的期货视为已完成。 所以你不会总是有一个Person可以一起工作。 您实际上可能有异常/可抛出。

如果您知道正在使用的CompletableFuture的数量,请直接使用它们

CompletableFuture.allOf(p1, p2).thenAccept(it -> {
    Person person1 = p1.join();
    Person person2 = p2.join();
});

如果你不知道你有多少(你正在处理一个数组或列表),只需捕获你传递给allOf的数组

// make sure not to change the contents of this array
CompletableFuture<Person>[] persons = new CompletableFuture[] { p1, p2 };
CompletableFuture.allOf(persons).thenAccept(ignore -> {
   for (int i = 0; i < persons.length; i++ ) {
       Person current = persons[i].join();
   }
});

如果您希望您的combinePersons方法( combinePersons忽略它是一个@Test )返回一个Person[]包含来自已完成期货的所有Person对象,您可以这样做

@Test
public Person[] combinePersons() throws Exception {
    CompletableFuture<Person> p1 = CompletableFuture.supplyAsync(() -> {
        return new Person("p1");
    });

    CompletableFuture<Person> p2 = CompletableFuture.supplyAsync(() -> {
        return new Person("p1");
    });

    // make sure not to change the contents of this array
    CompletableFuture<Person>[] persons = new CompletableFuture[] { p1, p2 };
    // this will throw an exception if any of the futures complete exceptionally
    CompletableFuture.allOf(persons).join();

    return Arrays.stream(persons).map(CompletableFuture::join).toArray(Person[]::new);
}

正如@Sotirios Delimanolis 的回答中所指出的,CompletableFuture 不是异常友好的,这意味着当一个或多个期货中发生异常时,我们无法轻松获得所有期货的结果。

由于OP的问题不仅限于成功的情况,在这个答案中,我想对CompletableFuture的这个不完美做一个补充。

但是,如果我们想知道所有期货的结果,我们可以在定义期货时单独处理。 例如:

    CompletableFuture<Person> p1 = CompletableFuture.supplyAsync(() -> {
      return new Person("p1");
    });
    
    p1.thenAccept(person -> {
      // handle successful future
      // for example:  add p1 to success operation list 
    });

    p1.exceptionally((exception) -> {
      // handle fail future
      // for example, log the parameter (not exist in this case) to create the person
      System.out.println("Exception happen in creating p1");
      return null;
    });

然后在调用CompletableFuture.allOf(persons).join()之后(也请注意这里的异常处理),我们可以判断出哪个future成功,哪个future失败。

希望这个简单的技巧可以帮助新手在现实世界的业务系统中编写可靠的代码。

CompletableFuture.allOf(p1, p2); // .get can be ignored
List<Person> res =
            List.of(p1, p2)  // list of futures
            .stream()
            .map(future -> {
               System.out.println("future " + future.join());
               return future.join();
            })  
            .collect(Collectors.toList());

或者您可以使用 p1.get() 和 p2.get() 单独获取值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM