Akka control threadpool threads

自作多情 提交于 2019-12-01 10:35:34

问题


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? e.g. can you initialize your own set of threads to be used in the thread pool, or otherwise control/modify the threads?


回答1:


In Akka, the thread pool is managed via a MessageDispatcher instance. 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). 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...




回答2:


I tried to understand it myself, but seams that guys in Akka don't want thread management to be exposed to public.

ThreadPoolConfig - the class that is responsible for creation of ExecutorService instances is a case class with method createExecutorService() declared 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.



来源:https://stackoverflow.com/questions/6855154/akka-control-threadpool-threads

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