Akka控制线程池线程 [英] Akka control threadpool threads

查看:318
本文介绍了Akka控制线程池线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常愚蠢的问题-

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

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?

推荐答案

在Akka,线程池是通过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
  ...
}

提供您自己的调度员,您可以扩展 akka.dispatch.MessageDispatcher (有关示例,请参见现有调度程序实现)。在这里您可以直接使用线程。

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

这篇关于Akka控制线程池线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆