Akka控制线程池线程

发布于 2024-11-26 15:17:02 字数 113 浏览 2 评论 0原文

可能是一个非常愚蠢的问题——

是否可以自定义 Akka/Scala actor 以便控制 actor 使用的线程?例如,您可以初始化要在线程池中使用的自己的线程集,或者以其他方式控制/修改线程吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是偏爱你 2024-12-03 15:17:02

在 Akka 中,线程池是通过 MessageDispatcher 实例进行管理的。您可以轻松地为参与者设置您想要的调度程序:

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

要提供您自己的调度程序,您可以扩展akka.dispatch.MessageDispatcher(请参阅现有调度程序实现的示例)。在这里你可以直接玩线程。

当然,将业务逻辑放入调度程序中是危险的,因为它可能会破坏参与者模型并增加并发错误的数量......

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...

这个俗人 2024-12-03 15:17:02

我试图自己理解它,但 Akka 中的人似乎不希望线程管理暴露在公众面前。

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)
    }
  }

因此,我没有看到提供自己的 ExecutorService 的简单方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文