Merging completable future results - Good practices

Is this a bad idea?
public static <T> CompletableFuture<List<T>> mergeFutures(List<CompletableFuture<T>> completableFutureList) {
if (completableFutureList.isEmpty()) {
return CompletableFuture.completedFuture(List.of());
}
T[] unwrappedFutures = (T[]) new Object[completableFutureList.size()];
CompletableFuture<?>[] completableFutures = new CompletableFuture<?>[completableFutureList.size()];
for (int i = 0; i < completableFutureList.size(); i++) {
final int iFinal = i;
completableFutures[i] = completableFutureList.get(i).thenAcceptAsync(t -> unwrappedFutures[iFinal] = t);
}
return CompletableFuture.allOf(completableFutures)
.thenApplyAsync(ignored -> Arrays.asList(unwrappedFutures));
}
public static <T> CompletableFuture<List<T>> mergeFutures(List<CompletableFuture<T>> completableFutureList) {
if (completableFutureList.isEmpty()) {
return CompletableFuture.completedFuture(List.of());
}
T[] unwrappedFutures = (T[]) new Object[completableFutureList.size()];
CompletableFuture<?>[] completableFutures = new CompletableFuture<?>[completableFutureList.size()];
for (int i = 0; i < completableFutureList.size(); i++) {
final int iFinal = i;
completableFutures[i] = completableFutureList.get(i).thenAcceptAsync(t -> unwrappedFutures[iFinal] = t);
}
return CompletableFuture.allOf(completableFutures)
.thenApplyAsync(ignored -> Arrays.asList(unwrappedFutures));
}
Will there be race conditions here? I tested 100000 times and found no issues, but is this theoretically a bad idea?
6 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @Thorinwasher! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
0x150
0x1506mo ago
what's your goal here? do you just want to have a future that resolves when all futures are done?
Thorinwasher
ThorinwasherOP6mo ago
I want this to be able to merge multiple completable futures into the same future and retrieve all items of those completable futures. I can for example do array operations in that merged future.
0x150
0x1506mo ago
@SafeVarargs
public static <T> CompletableFuture<List<? extends T>> merge(CompletableFuture<? extends T>... futures) {
return CompletableFuture.allOf(futures).thenApply(unused -> Arrays.stream(futures).map(CompletableFuture::join).toList());
}
@SafeVarargs
public static <T> CompletableFuture<List<? extends T>> merge(CompletableFuture<? extends T>... futures) {
return CompletableFuture.allOf(futures).thenApply(unused -> Arrays.stream(futures).map(CompletableFuture::join).toList());
}
Thorinwasher
ThorinwasherOP6mo ago
Damn that looks a lot simpler 😅 Also looks more thread safe
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?