Skip to content

Commit

Permalink
Make futures more simple
Browse files Browse the repository at this point in the history
Co-authored-by: Josiah Glosson <soujournme@gmail.com>
  • Loading branch information
AlexProgrammerDE and Gaming32 committed Oct 2, 2024
1 parent f6af709 commit f6898bb
Showing 1 changed file with 2 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,14 @@ public static void writeIfNeeded(Path path, String content) throws IOException {

public static <S, T> List<T> maxFutures(int maxFutures, Collection<S> source, Function<S, CompletableFuture<T>> toFuture) {
final var sourceIter = source.iterator();
final var futures = new ArrayList<CompletableFuture<T>>(maxFutures);
final var futures = new ArrayList<CompletableFuture<Void>>(maxFutures);
final var result = new ArrayList<T>(source.size());
while (sourceIter.hasNext()) {
while (futures.size() < maxFutures && sourceIter.hasNext()) {
futures.add(toFuture.apply(sourceIter.next()).thenApply(r -> {
futures.add(toFuture.apply(sourceIter.next()).thenAccept(r -> {
synchronized (result) {
result.add(r);
}

return r;
}));
}

Expand Down

0 comments on commit f6898bb

Please sign in to comment.