Прямой ответ - да, вот пример метода
private <T> CompletableFuture<T> waitAny(List<CompletableFuture<T>> allFutures) throws InterruptedException {
Thread thread = Thread.currentThread();
while (!thread.isInterrupted()) {
for (CompletableFuture<T> future : allFutures) {
if (future.isDone()) {
return future;
}
}
Thread.sleep(50L);
}
throw new InterruptedException();
}
Второй вариант
private <T> CompletableFuture<T> waitAny(List<CompletableFuture<T>> allFutures) throws InterruptedException {
CompletableFuture<CompletableFuture<T>> any = new CompletableFuture<>();
for (CompletableFuture<T> future : allFutures) {
future.handleAsync((t, throwable) -> {
any.complete(future);
return null;
});
}
try {
return any.get();
} catch (ExecutionException e) {
throw new IllegalStateException(e);
}
}
, но весь контекст задачи неясен, возможно, существует более оптимальное решение.