使用带有actor的接收时,使用循环而不是while(true)有什么区别.循环似乎工作得更快,但为什么,以及发动机罩下发生了什么?
使用循环而不是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()
}
}
}}}
Run Code Online (Sandbox Code Playgroud)
而不是while(true)它在循环中表现更好.
谢谢