Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterable of Futures using Guava?

Tags:

java

future

guava

I've been working extensively with Google Guava and ListenableFutures as of late, and something I've been finding missing is a way to provide an out-of-order streaming handler of future completions. What I'm envisioning is an iterable which blocks on next() until another future returns.

Before I go through the effort of creating my own, does one exist already?

If not, my thought was to leverage the callback functionality of a ListenableFuture to push results onto a BlockingQueue. The goal is to process returned values as soon as they're back. Futures.successfulAsList() is great, but waits for all values before returning rather than letting me schedule other useful work.

like image 691
Xorlev Avatar asked Feb 20 '23 07:02

Xorlev


1 Answers

You can use java.util.concurrent.CompletionService. Its method take could be easily wrapped to Iterator.next() if it didn't throw InterruptedException.

like image 75
maaartinus Avatar answered Feb 27 '23 23:02

maaartinus