![](/img/trans.png)
[英]Will all futures passed to CompletableFuture.allOf() run?
[英]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
異常完成,則返回的CompletableFuture
也CompletableFuture
,CompletionException
將此異常作為其原因。 否則,給定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.