使用带有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)它在循环中表现更好.
谢谢
usingloop会将线程释放给其他任务,而 whilewhile不会。因此,如果您使用许多演员,loop那么使用 会使效率更高。另一方面,单个参与者使用whileand比使用and (或者就此而言,and )receive要快得多。loopreactloopreceive
| 归档时间: |
|
| 查看次数: |
1278 次 |
| 最近记录: |