使用循环与使用while(true)时,使用actors接收消息有什么区别?循环似乎更快,但是为什么?底层发生了什么?
使用循环代替while(true)是否有什么不好的影响?
更多背景信息:我正在对简单的ping/pong代码进行性能测试。我正在使用receive。
这是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) 会更有效率。
谢谢