当我们在 scala actor 中使用循环而不是 while(true) 时会发生什么?

发布于 2024-10-14 08:22:16 字数 747 浏览 3 评论 0原文

在与 actor 一起使用 receive 时,使用循环而不是 while(true) 有什么区别。 Loop 似乎工作得更快,但为什么,以及引擎盖下发生了什么?

使用循环代替 while(true) 有什么不好吗?

更多关于上下文。我正在使用简单的 ping/pong 代码进行性能测试。我正在使用接收。

这是 Ping 类:

class ReceivePing(
        count : Int,
        pong : Actor
       ) extends Actor {def act() {
var pingsLeft = count - 1
pong ! Start
pong ! ReceivePing
while(true) {
  receive {
    case ReceivePong =>
      if (pingsLeft % 10000 == 0)
        Console.println("ReceivePing: pong")
      if (pingsLeft > 0) {
        pong ! ReceivePing
        pingsLeft -= 1
      } else {
        Console.println("ReceivePing: stop")
        pong ! Stop
        exit()
      }
  }
}}}

它使用循环而不是 while(true),它的性能更好。

谢谢

What's the difference of using loop instead of while(true) while using receive with actors. Loop seems to work much faster, but why, and what's going on under the bonnet?

Is there anything bad to use loop instead of while(true)?

More about context. I'm doing performance tests within simple ping/pong code. And I'm using receive.

This is the Ping class:

class ReceivePing(
        count : Int,
        pong : Actor
       ) extends Actor {def act() {
var pingsLeft = count - 1
pong ! Start
pong ! ReceivePing
while(true) {
  receive {
    case ReceivePong =>
      if (pingsLeft % 10000 == 0)
        Console.println("ReceivePing: pong")
      if (pingsLeft > 0) {
        pong ! ReceivePing
        pingsLeft -= 1
      } else {
        Console.println("ReceivePing: stop")
        pong ! Stop
        exit()
      }
  }
}}}

instead of while(true) it performs better with loop.

Thanks

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

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

发布评论

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

评论(2

生生不灭 2024-10-21 08:22:16

while/receive 循环阻塞线程< /a>,而 loop/react 构造则不然。这意味着第一个构造需要每个参与者一个线程,这很快就会变得很慢。

根据 Haller 和 Odersky 2006

等待接收的演员
语句不由 a 表示
阻塞线程但是通过闭包
捕捉演员的其余部分
计算。关闭已执行
一旦消息发送给演员
与其中一条消息匹配
接收中指定的模式。
闭包的执行是在发送者的线程上“搭载”的。
如果接收关闭
终止,控制权返回到
发送者就像一个过程返回一样。如果
接收闭包块
第二次接收,控制权返回
发送者通过抛出一个特殊的
展开接收器的异常
调用堆栈。

(显然他们后来改变了 receive 的行为,并将旧的 receive 重命名为 react。)

The while/receive loop blocks a thread, whereas the loop/react construct doesn't. This means the first construct needs one thread per actor, which quickly becomes slow.

According to Haller and Odersky 2006,

An actor that waits in a receive
statement is not represented by a
blocked thread but by a closure that
captures the rest of the actor's
computation. The closure is executed
once a message is sent to the actor
that matches one of the message
patterns specied in the receive.
The execution of the closure is "piggy-backed" on the thread of the sender.
If the receiving closure
terminates, control is returned to the
sender as if a procedure returns. If
the receiving closure blocks in a
second receive, control is returned to
the sender by throwing a special
exception that unwinds the receiver's
call stack.

(Apparently they later changed the behavior of receive and renamed the old receive to react.)

巾帼英雄 2024-10-21 08:22:16

使用loop会将线程释放给其他任务,而while则不会。因此,如果您使用许多参与者,那么使用loop可以提高效率。另一方面,使用 whilereceive 的单个参与者比使用 loopreact 的参与者要快得多(或者,就此而言,循环接收)。

Using loop releases the thread to other tasks, while while doesn't. So, if you are using many actors, the use of loop makes then more efficient. On the other hand, a single actor using while and receive is much faster than one using loop and react (or, for that matter, loop and receive).

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