处理两种不同类型的未来(其中一种未来依赖于另一种未来)的理想方式是什么?
我正在查看 AKKA 的 Java Futures API,我看到了很多处理同一类型的多个 future 的方法,但我没有看到任何处理不同类型的 future 的方法。我猜我让事情变得更加复杂了。
无论如何,假设我们有两个不同的 Actor:actorA 和 actorB。它们基于不同的类,并且各自返回不同的 Future。然而,actorB 依赖于 actorA 的 Future。是以下逻辑,如何最好地处理这种情况?
Future<A> a = actorA.sendRequestReplyFuture(...);
Future<B> b = actorB.sendRequestReplyFuture(a);
如果我们有一个 actorA 和 actorB 的列表怎么样?
I'm looking at AKKA's Java Futures API, and I see a lot of ways to handle multiple futures of the same type, but I don't see anything that pops out at me for handling futures of different types. I'm guessing that I'm making this more complicated than it is.
Anyways let's say we have two different Actors: actorA and actorB. They are based on different classes and they each return a different Future. However, actorB is dependent on a Future from actorA. Is the following logic, how to best handle this case?
Future<A> a = actorA.sendRequestReplyFuture(...);
Future<B> b = actorB.sendRequestReplyFuture(a);
How about if we have a list of actorAs and actorBs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
警告,仅从内存中编写,可能无法编译 OOTB:
WARNING, just written from memory, might not compile OOTB:
为什么不像您那样直接将它们包装起来,但只需稍作更改即可将 a 的结果传递给 b。
这种安排仅意味着在
actorA.sendRequestReplyFuture()
完成之前不会调用actorB.sendRequestReplyFuture()
。这没有什么问题,事实上,我喜欢你那里的模式。Why not just wrap them, as you have, but with one slight change - pass the result of a into b.
This arrangement just means that
actorB.sendRequestReplyFuture()
won't get called untilactorA.sendRequestReplyFuture()
has finished. Nothing wrong with that, in fact, I like the pattern you've got there.