I use an assistant CompletableFuture.allOf(...)to create aggregate futures that will only be “executed” when their composite futures are marked as completed, for example:
CompletableFuture<?> future1 = new CompletableFuture<>();
CompletableFuture<?> future2 = new CompletableFuture<>();
CompletableFuture<?> future3 = new CompletableFuture<>();
CompletableFuture<?> future = CompletableFuture.allOf(future1, future2, future3);
I would like to slightly change this functionality, where the aggregate future will be complete when:
- All futures completed successfully OR
- Any one future failed
In the latter case, the aggregate future should end (exclusively) immediately, and there is no need to wait for the completion of other futures, i.e. with an error.
To illustrate this, unlike CompletableFuture.allOf(...), consider the following:
future1.complete(null);
System.out.println("Future1 Complete, aggregate status: " + future.isDone());
future2.completeExceptionally(new Exception());
System.out.println("Future2 Complete, aggregate status: " + future.isDone());
future3.complete(null);
System.out.println("Future3 Complete, aggregate status: " + future.isDone());
Using allOf(...), this code gives:
Future1 Complete, aggregate status: false
Future2 Complete, aggregate status: false
Future3 Complete, aggregate status: true
"" , Feature2 , , .
utils Java, , ... .
CompletableFuture.allOf(...), , . , , ?