如何设置 Scala actor 独占使用单独的线程来运行?

发布于 2024-12-13 23:02:11 字数 88 浏览 4 评论 0原文

据我了解,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 技术交流群。

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

发布评论

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

评论(1

临走之时 2024-12-20 23:02:11

听起来你正在使用 Scala (而不是 Akka)演员。在这种情况下,如果您使用 receivereceiveWithin 风格的消息处理,那么每个参与者都会获得自己的线程。使用 react 风格的消息处理在参与者之间共享线程池。

当我说“接收”“样式”时,我的意思是在循环中,例如:

val timerActor = actor { 
  while (true) {
    receiveWithin(60 * 1000) {
       case Stop => self.exit()
       case TIMEOUT =>
          destination ! Tick
    }
  }
}

在这种情况下,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 or receiveWithin style of message handling then each actor will get its own thread. Using the react style of message handling shares a thread pool among actors.

When I say the receive "style", I mean in a loop, for example:

val timerActor = actor { 
  while (true) {
    receiveWithin(60 * 1000) {
       case Stop => self.exit()
       case TIMEOUT =>
          destination ! Tick
    }
  }
}

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.

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