简体   繁体   English

Akka控制线程池线程

[英]Akka control threadpool threads

Potentially a very silly question-- 可能是一个非常愚蠢的问题-

Is it possible to customize Akka/Scala actors such that you control the threads that are used by the actors? 是否可以自定义Akka / Scala actor,以便您控制actor使用的线程? eg can you initialize your own set of threads to be used in the thread pool, or otherwise control/modify the threads? 例如,您可以初始化自己的线程集以在线程池中使用,还是以其他方式控制/修改线程?

In Akka, the thread pool is managed via a MessageDispatcher instance. 在Akka中,线程池是通过MessageDispatcher实例进行管理的。 You can set the dispatcher you want to actors easily: 您可以将要调度的调度员轻松设置为参与者:

class MyActor( dispatcher: MessageDispatcher ) extends Actor {
  self.dispatcher = dispatcher
  ...
}

To provide your own dispatcher, you can extend akka.dispatch.MessageDispatcher (see existing dispatchers implementation for examples). 要提供自己的调度程序,可以扩展akka.dispatch.MessageDispatcher (有关示例,请参阅现有调度程序实现)。 Here you can play directly with the threads. 在这里您可以直接玩线程。

Of course, it's dangerous to put business logic inside a dispatcher because it may break the actor model and increase the number of concurrency bugs... 当然,将业务逻辑放入调度程序中是危险的,因为它可能会破坏参与者模型并增加并发错误的数量...

I tried to understand it myself, but seams that guys in Akka don't want thread management to be exposed to public. 我本人试图理解它,但是似乎Akka的家伙不希望线程管理公开。

ThreadPoolConfig - the class that is responsible for creation of ExecutorService instances is a case class with method createExecutorService() declared final ! ThreadPoolConfig-负责创建ExecutorService实例的类是一个案例类,其方法createExecutorService()声明为final

  final def createExecutorService(threadFactory: ThreadFactory): ExecutorService = {
    flowHandler match {
      case Left(rejectHandler) ⇒
        val service = new ThreadPoolExecutor(...)
        service
      case Right(bounds) ⇒
        val service = new ThreadPoolExecutor(...)
        new BoundedExecutorDecorator(service, bounds)
    }
  }

So, I don't see an easy ways to provide your own ExecutorService. 因此,我看不到提供自己的ExecutorService的简便方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM