Can Scala's Actor framework handle 10.000 actors without stack problems?
-
06-07-2019 - |
Question
I want to do a multi-agent simulation containing about 10.000 agents (machine and product agents) using the Scala Actor framework.
As I understand, if there are lots of actors passing messages around, can it run out of stack due the recursion?
If so, how can I increase the stack sizes for the underlying worker threads?
Solution
The actor framework has been designed to handle this - in fact, it can handle this with only one thread, assuming you use the loop-react
pattern as follows:
import actors._
import actors.Actor._
val a = actor {
loop {
react {
case ABC => //Handle here
}
}
}
On pp 590-593 of Programming in Scala this is discussed in more detail: basically the react
method never returns normally (it terminates with an exception) and therefore its call stack does not need to be preserved. You can think of it as looping forever.