Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 351
SmartSolution Avatar asked Mar 21 '13 17:03

SmartSolution


People also ask

Does Akka use threads?

Behind the scenes, Akka runs actors on real threads and many actors may share one thread. A Actor can create many actors called child actors. Actors interact only through asynchronous messages and never through direct method calls.

Is Akka actor a thread?

The good news is that Akka actors conceptually each have their own light-weight thread, which is completely shielded from the rest of the system. This means that instead of having to synchronize access using locks you can write your actor code without worrying about concurrency at all.

Is Akka actor thread safe?

Meaning that this is not a tread, but the AKKA system will give the impression that an Actor is always running in it's own thread to the developer. This means that any operations performed as a result of acting on a message are, for all purposes, thread safe.

Can Akka be used with Java?

Akka is a toolkit and runtime for building highly concurrent, distributed, and fault-tolerant event-driven applications on the JVM. Akka can be used with both Java and Scala. This guide introduces Akka Actors by describing the Java version of the Hello World example.


2 Answers

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.

like image 131
Rex Kerr Avatar answered Sep 30 '22 00:09

Rex Kerr


An Akka actor is not equivalent to a thread. It is more like a Callable that is executed on a threadpool.

When a message is dispatched to an actor, that actor is placed on a threadpool to process the message. When it is done, the pooled thread can be used to execute other actors.

like image 40
parsifal Avatar answered Sep 29 '22 23:09

parsifal