如何为actor指定线程池

发布于 2024-08-09 05:20:54 字数 198 浏览 11 评论 0原文

我有一个使用全局线程池的现有 java/scala 应用程序。我想开始在项目中使用参与者,但希望应用程序中的所有内容都使用相同的池。

我知道我可以设置参与者使用的最大线程数,但更喜欢共享线程池。这是必要/合理的吗?是否可以指定参与者的线程池?

如果不可能/不推荐,那么在已经使用线程的应用程序中集成参与者时是否有任何经验规则?

谢谢。

I have an existing java/scala application using a global thread pool. I would like to start using actors in the project but would like everything in the app using the same pool.

I know I can set the maximum number of threads that actors use but would prefer sharing the thread pool. Is this necessary/reasonable, and is it possible to designate the actor's thread pool?

If it is not possible/recommended, are there any rules of thumb when integrating actors in apps that are already using threads?

Thanks.

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

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

发布评论

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

评论(3

梦醒灬来后我 2024-08-16 05:20:54

我相信你可以做这样的事情:

trait MyActor extends Actor {
  val pool = ... // git yer thread pool here
  override def scheduler = new SchedulerAdapter {
    def execute(block: => Unit) =
      pool.execute(new Runnable {
        def run() { block }
      })
  }
} 

I believe you can do something like this:

trait MyActor extends Actor {
  val pool = ... // git yer thread pool here
  override def scheduler = new SchedulerAdapter {
    def execute(block: => Unit) =
      pool.execute(new Runnable {
        def run() { block }
      })
  }
} 
疯到世界奔溃 2024-08-16 05:20:54

对于 Scala 2.8.1 来说是:

scala -Dactors.corePoolSize=20

For Scala 2.8.1 it's:

scala -Dactors.corePoolSize=20
夜无邪 2024-08-16 05:20:54

但是重用 Actor 子系统使用的线程池非常容易。首先,您可以控制它的大小:

-Dactors.maxPoolSize=8

并且您可以对其调用工作:

actors.Scheduler.execute( f ); //f is => Unit

它唯一缺少的是安排工作的能力。为此,我使用一个单独的 ScheduledExecutorService,它是单线程,并在 actor 线程池上运行其工作:

object MyScheduler {
  private val scheduler = Executors.newSingleThreadedScheduledExecutorService

  def schedule(f: => Unit, delay: (Long, TimeUnit)) : ScheduledFuture[_] = {
      scheduler.schedule(new ScheduledRun(f), delay._1, delay._2)
  }

  private class ScheduledRun(f: => Unit) extends Runnable {
    def run = actors.Scheduler.execute(f)
  }

}

然后您可以使用它来安排任何事情:

MyScheduler.schedule(f, (60, SECONDS))

But it's quite easy to re-use the thread pool used by the actor subsystem. Firstly you can control it's size:

-Dactors.maxPoolSize=8

And you can invoke work on it:

actors.Scheduler.execute( f ); //f is => Unit

The only thing it lacks is the ability to schedule work. For this I use a separate ScheduledExecutorService which is single-threaded and runs its work on the actors thread pool:

object MyScheduler {
  private val scheduler = Executors.newSingleThreadedScheduledExecutorService

  def schedule(f: => Unit, delay: (Long, TimeUnit)) : ScheduledFuture[_] = {
      scheduler.schedule(new ScheduledRun(f), delay._1, delay._2)
  }

  private class ScheduledRun(f: => Unit) extends Runnable {
    def run = actors.Scheduler.execute(f)
  }

}

Then you can use this to schedule anything:

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