How to make full use of all cores using Scala actors?
I have a high CPU/memory bound task that I would like my Scala program to execute in para开发者_如何学Gollel. So, I'm using the Actors framework (using receive in a while(true) loop). I call the start method on the actor and send it thousands of messages to process.
During the execution of the program (takes about an hour), only 100 - 120% of the CPU is used. The machine has 8 cores. Shouldn't the actor spawn multiple threads to use up all the 8 cores, and I should see usage close to 800%?
Or am I supposed to instantiate 8 actors and send it each some of the messages (or rather get them all to read from some concurrent queue)?
Thanks.
Nope. A single actor is guaranteed to execute on exactly one thread at a time, so access to its state doesn't need to be synchronized. If you want to spread your problem across multiple cores, you need multiple actors.
精彩评论