pet*_*hel 7 java java-8 completable-future
我试图掌握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
});
Run Code Online (Sandbox Code Playgroud)
到目前为止我在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));
}
Run Code Online (Sandbox Code Playgroud)
Sot*_*lis 20
该CompletableFuture#allOf方法不公开CompletableFuture传递给它的已完成实例的集合.
返回
CompletableFuture在所有给定CompletableFutures完成时完成的新内容.如果任何给定的CompletableFutures完全异常,则返回CompletableFuture也会这样做,并将CompletionException此异常作为其原因.否则,给定CompletableFutures 的结果(如果有的话)不会反映在返回的结果中CompletableFuture,但可以通过单独检查它们来获得.如果未CompletableFuture提供s,则返回CompletableFuture带有值的 完成null.
请注意,allOf还会考虑已完成的异常完成的期货.所以你不会总是有一个Person合作.你可能实际上有一个异常/ throwable.
如果您知道CompletableFuture正在使用的s 数量,请直接使用它们
CompletableFuture.allOf(p1, p2).thenAccept(it -> {
Person person1 = p1.join();
Person person2 = p2.join();
});
Run Code Online (Sandbox Code Playgroud)
如果您不知道自己有多少(使用数组或列表),只需捕获传递给的数组 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();
}
});
Run Code Online (Sandbox Code Playgroud)
如果你想要你的combinePersons方法(@Test现在忽略它是a )来返回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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7043 次 |
| 最近记录: |