在期货清单上流式传输的最有效方式 [英] Most efficient way to stream on list of Futures

查看:146
本文介绍了在期货清单上流式传输的最有效方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过流式传输对象列表来调用异步客户端方法。该方法返回Future。

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

迭代调用后返回的Futures列表的最佳方法是什么(以便处理那些首先出现的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)?

注意:异步客户端仅返回Future not CompletableFuture。

Note: The async client only returns Future not CompletableFuture.

以下是代码:

List<Future<Object>> listOfFuture = objectsToProcess.parallelStream()
    .map((object) -> {
        /* calling an async client returning a Future<Object> */ })
    .collect(Collectors.toList());


推荐答案

拥有 List<的列表; Future< Object>> ,我会将其提交到自定义池,而不是使用默认的流并行处理。

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

这是因为流api使用公共池进行并行处理,你将在那些Futures上调用 get (如果需要大量时间进行处理) - 你将阻止所有其他流操作在你的应用程序中使用并行操作直到完成。

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.

这有点像这样:

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

我会使用如图所示的自定义池这里

I would go with a custom pool like shown here

这篇关于在期货清单上流式传输的最有效方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆