问题描述
Scala Actor入门。 说:
Just getting started on Scala Actors. The Scala website says:
。 ..
请注意,在 while
内使用反应
循环不起作用!但是,由于循环是常见的
,因此它在 loop
函数的形式
中提供了特殊的库支持。可以这样使用:
Note that using react
inside a while
loop does not work! However, since loops are common there is special library support for it in form of a loop
function. It can be used like this:
loop {
react {
case A => ...
case B => ...
}
}
我现在很困惑-似乎是矛盾的:
I'm now confused - there seems to be a contradiction:
a)如果 react
永不返回,那么将其置于循环中的意义何在? ?
a) If react
never returns, then what's the point of putting it in a loop?
b)由于循环
重复执行一个块,它与有何不同? while(true)
-为什么 while
不起作用,为什么它不起作用?
b) Since loop
repeatedly executes a block, how is it any different to while(true)
- why doesn't while
work, and in what way does it "not work"?
推荐答案
两个函数,循环
和反应
不纯 loop
通过名称参数进行调用,而 react
进行PartialFunction调用,两者均在原始actor上设置变量。这是因为actor始终没有附加线程。仅当其消息框中有一条消息时,它才会激活。这就是为什么 while(true)
会导致100%的CPU使用率并且参与者没有响应的原因。
Both functions, loop
and react
are not pure. loop
takes a call by name parameter and react
a PartialFunction, both set variables on the raw actor. This is because an actor does not have a thread attached all the time. It will become active only when there is a message in it's messagebox. This is why a while(true)
will lead to 100% cpu usage and the actor not responding.
这篇关于Scala Actors:如果react永不返回,为什么它必须处于循环状态{},为什么while(true)不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!