Skip to content

Instantly share code, notes, and snippets.

@robertbraeutigam
Last active March 24, 2020 15:41
Show Gist options
  • Save robertbraeutigam/a0a06318bf953042ac61819e4ac79ed5 to your computer and use it in GitHub Desktop.
Save robertbraeutigam/a0a06318bf953042ac61819e4ac79ed5 to your computer and use it in GitHub Desktop.
Converting a collection of CompletableFutures to completion order Streams.
public static <T> Stream<T> inCompletionOrder(Collection<CompletableFuture<T>> futures) {
BlockingQueue<CompletableFuture<T>> queue = new LinkedBlockingQueue<>();
for (CompletableFuture<T> future: futures) {
future.whenComplete((value,exception) -> queue.add(future));
}
return Stream.generate(noException(queue::take))
.map(CompletableFuture::join)
.limit(futures.size());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment