Akka actor和Java8 CompletableFuture的能安全结合吗?

时间:2022-07-29 23:37:57

As far as I understood the Akka documentation, an Akka ActorSystem contains its own thread pool to execute actors. I use Akka in a Java application that also uses Java8 futures; the latter are executed by the ForkJoinPool.commonPool(). So, actors and futures use different pools, which may defeat certain assumptions hidden in the two schedulers (e.g. the Akka scheduler might assume that futures are run on the Akka pool). Could this create any performance problems?

就我对Akka文档的理解而言,Akka ActorSystem包含自己的线程池来执行参与者。我在Java应用程序中使用Akka,它也使用Java8期货;后者由ForkJoinPool.commonPool()执行。因此,参与者和期货使用不同的池,这可能会挫败隐藏在两个调度器中的某些假设(例如,Akka调度器可能会假设期货在Akka池中运行)。这会产生性能问题吗?

1 个解决方案

#1


2  

There are no hidden assumptions concerning the execution of Actors and Futures: the only guarantee that we give is that any given Actor is only executed on at most one thread at any given time. Futures observe no such restrictions, they run whenever the ExecutionContext (or ThreadPool) decides to run them.

关于行为人和期货的执行没有隐藏的假设:我们提供的唯一保证是,任何给定行为人在任何给定时间最多只能在一个线程上执行。期货没有这种限制,它们在执行上下文(或线程池)决定运行它们时运行。

You will of course have to observe all the same caveats when combining Actors with Java8 Futures that also apply to Scala Futures, see the docs. In particular, never touch anything (no fields, no methods) of an Actor from within a Future task or callback. Only the ActorRef is safe.

当然,在将参与者与Java8期货组合在一起时,您当然必须观察到所有相同的注意事项,这也适用于Scala的未来,见文档。特别是,永远不要在将来的任务或回调中触摸参与者的任何东西(没有字段,没有方法)。只有演员是安全的。

#1


2  

There are no hidden assumptions concerning the execution of Actors and Futures: the only guarantee that we give is that any given Actor is only executed on at most one thread at any given time. Futures observe no such restrictions, they run whenever the ExecutionContext (or ThreadPool) decides to run them.

关于行为人和期货的执行没有隐藏的假设:我们提供的唯一保证是,任何给定行为人在任何给定时间最多只能在一个线程上执行。期货没有这种限制,它们在执行上下文(或线程池)决定运行它们时运行。

You will of course have to observe all the same caveats when combining Actors with Java8 Futures that also apply to Scala Futures, see the docs. In particular, never touch anything (no fields, no methods) of an Actor from within a Future task or callback. Only the ActorRef is safe.

当然,在将参与者与Java8期货组合在一起时,您当然必须观察到所有相同的注意事项,这也适用于Scala的未来,见文档。特别是,永远不要在将来的任务或回调中触摸参与者的任何东西(没有字段,没有方法)。只有演员是安全的。