あなたのユースケースのために
static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)
をCompletableFutureを使用することができますが同じで、完全な 与えCompletableFuturesの際に任意の完成された新しいCompletableFutureを返します。結果。そうでない場合、 が例外的に完了した場合、返されたCompletableFutureも です。この例外をその原因として持つCompletionExceptionを持ちます。 CompletableFuturesが指定されていない場合は、不完全な CompletableFutureを返します。
すべての先物をリストに保存する必要があります。 `
今
List<CompletableFuture<?>> futureList = // all futures;
while(futureList.size()>0){
CompletableFuture<?> completed = CompletableFuture.anyOf(futureList); //convert futureList to array
if(completed.isCompletedExceptionally()){
// first future that completed with an exception
}else{
//future completed without exception, store the result or process
futureList.remove(completed); // remove it from the incomplete list
}
}
あなたはこのような場合、それには、しかし、明示的なエグゼキュータ・プール
final CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//...long running...
return "returnVal";
});
を使用したくない場合は、CompletableFuture
よう
final CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//...long running...
return "returnVal";
},
executor); //here executor is your executor pool
を得ることができますForkJoinPool .commonPool()
に送信されます。
なぜあなたの呼び出し可能コードをラップし、ラッパーの各エラーを処理しないのですか? –