How are Java threads heavy compared to Scala / Akka actors?

SmartSolution picture SmartSolution · Mar 21, 2013 · Viewed 13.3k times · Source

I was just comparing the performance of scala actors vs java threads.

I was amazed to see the difference, I observed that with my system I was able to spawn maximum ~2000 threads (live at a time) only But with the same system I was able to spawn ~500,000 actors of scala.

Both programs used around 81MB of Heap memory of JVM.

Can you explain How java thread are this much heavy weight than scala / akka actors? What is the key factor which made scala-actor this much light weight?

If I want to achieve best scalability, Should I go for actor based web server instead of java based traditional web/app server like JBoss or Tomcat?

Thanks.

Answer

Rex Kerr picture Rex Kerr · Mar 21, 2013

Scala actors (including the Akka variety) use Java threads. There's no magic: more than a few thousand threads running simultaneously is a problem for most desktop machines.

The Actor model allows for awake-on-demand actors which do not occupy a thread unless they have work to do. Some problems can be modeled effectively as lots of sleeping agents waiting to get some work, who will do it relatively quickly and then go back to sleep. In that case, actors are a very efficient way to use Java threading to get your work done, especially if you have a library like Akka where performance has been a high priority.

The Akka docs explain the basics pretty well.

All reasonably scalable web servers have to solve this sort of problem one way or another; you probably ought not be basing your decision for web server primarily on whether actors are used under the hood, and regardless of what you use you can always add actors yourself.