Just getting started on Scala Actors. The Scala website says:
Thread-blocking operations can be avoided by using
react
to wait for new messages (the event-based pendant ofreceive
). However, there is a (usually small) price to pay:react
never returns....
Note that using
react
inside awhile
loop does not work! However, since loops are common there is special library support for it in form of aloop
function. 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, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With