Most efficient way to stream on list of Futures

跟風遠走 提交于 2019-12-04 23:10:34

问题


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());

回答1:


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



来源:https://stackoverflow.com/questions/43226405/most-efficient-way-to-stream-on-list-of-futures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!