Just getting started on Scala Actors. The Scala website says:
Thread-blocking operations can be avoided by using
reactto wait for
new messages (the event-based pendant ofreceive). However, there is a
(usually small) price to pay:reactnever returns.…
Note that using
reactinside awhileloop does not work! However,
since loops are common there is special library support for it in form
of aloopfunction. It can be used like this:
loop {
react {
case A => ...
case B => ...
}
}
I’m now confused – there seems to be a contradiction:
a) If react never returns, then what’s the point of putting it in a loop?
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”?
Both functions,
loopandreactare not pure.looptakes a call by name parameter andreacta 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 awhile(true)will lead to 100% cpu usage and the actor not responding.