如何设置 Scala actor 独占使用单独的线程来运行?
据我了解,Scala 管理一个线程池来运行参与者,并在它们之间共享线程。我可以将特定的 Actor 设置为专门在单独的线程中运行,而不与其他 Actor 共享吗?
As far as I understand, Scala manages a thread pool to run actors, sharing threads among them. Can I set up a particular actor to run in a separate thread exclusively, never sharing it with another actor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你正在使用 Scala (而不是 Akka)演员。在这种情况下,如果您使用
receive
或receiveWithin
风格的消息处理,那么每个参与者都会获得自己的线程。使用react
风格的消息处理在参与者之间共享线程池。当我说“接收”“样式”时,我的意思是在循环中,例如:
在这种情况下,timerActor 不与任何其他参与者共享其线程。
receiveWithin
将阻塞,直到参与者收到 Stop 消息或 60 秒过去。如果 60 秒过去,则执行 TIMEOUT 情况。如果您想了解有关 Scala Actor 的详细信息,请查看论文 统一线程的 Actor和事件。
除了基于事件的 actor 之外,Akka 还支持基于线程的 actor。
It sounds like you are using Scala (not Akka) actors. In that case if you use the
receive
orreceiveWithin
style of message handling then each actor will get its own thread. Using thereact
style of message handling shares a thread pool among actors.When I say the
receive
"style", I mean in a loop, for example:In this case
timerActor
does not share its thread with any other actor.receiveWithin
will block until either the actor receives a Stop message or 60 seconds passes. If 60 seconds passes then the TIMEOUT case is executed.If you want to learn the gritty details about Scala actors, check out the paper Actors That Unify Threads and Events.
Akka also supports thread-based actors in addition to event-based actors.