Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to stream on list of Futures

I'm calling an async client method by streaming over a list of objects. The method returns Future.

What's the best way to iterate over the list of Futures returned after the call (so as to process those Future which comes first)?

Note: The async client only returns Future not CompletableFuture.

Following is the code:

List<Future<Object>> listOfFuture = objectsToProcess.parallelStream()
    .map((object) -> {
        /* calling an async client returning a Future<Object> */ })
    .collect(Collectors.toList());
like image 945
syfro Avatar asked Apr 05 '17 08:04

syfro


People also ask

Are streams more efficient than for loops?

Remember that loops use an imperative style and Streams a declarative style, so Streams are likely to be much easier to maintain. If you have a small list, loops perform better. If you have a huge list, a parallel stream will perform better.

Which method is used to convert stream to List?

This class has the toList() method, which converts the Stream to a List.

Does stream on List maintain order?

E.g. if you call stream() on a HashSet the stream will be unordered while calling stream() on a List returns an ordered stream.


1 Answers

Having this list of List<Future<Object>>, I would submit it to a custom pool, instead of using the default stream parallel processing.

That is because the stream api uses a common pool for parallel processing and you will call get on those Futures(if it takes significant time for processing) - you will block all other stream operations that use parallel operations within your application until this one is done.

This would a bit like this:

forJoinPool.submit( () -> list.stream().parallel().map(future -> future.get()).collect(Collectors.toList())).get();

I would go with a custom pool like shown here

like image 108
Eugene Avatar answered Sep 28 '22 11:09

Eugene