2
votes

I have read that:

Akka ensures that each instance of an actor runs in its own lightweight thread and its messages are processed one at a time.

If this is the case that AKKA actors processes its messages sequentially then how AKKA provides concurrency for a single Actor.

1
why do you think Akka provides concurrency for a single Actor?Alexei Kaigorodov
Akka is for concurrency. I am asking that if we are using actors instead of Threads and we are using single actor for multiple message processing then those message processing is not concurrent?user1147070
@ user1147070 exactly. One at a time, remember? This is to ensure that actor's instance variables are modified sequentially. If you want handle messages in parallel, wrap each message in a Runnable and submit them to an Executor.Alexei Kaigorodov
Yes, but then what is the benefit of AKKA over Executor? Why AKKA is used then?user1147070
One of the benefits is that messages for one actor are handled sequentially.Alexei Kaigorodov

1 Answers

3
votes

Actors are independent agents of computation, each one is executed strictly sequentially but many actors can be executed concurrently. You can view an Actor as a Thread that costs only about 0.1% of what a normal thread costs and that also has an address to which you can send messages—you can of course manage a queue in your own Thread and use that for message passing but you’d have to implement all that yourself.

If Akka—or the Actor Model—stopped here, then it would indeed not be very useful. The trick is that giving stable addresses (ActorRef) to the Actors enables them to communicate even across machine boundaries, over a network, in a cluster. It also allows them to be supervised for principled failure handling—when a normal Thread throws an exception it simply terminates and nothing is done to fix it.

It is this whole package of encapsulation (provided by hiding everything behind ActorRef), message-based communication that is location transparent, and support for failure handling that makes the Actor Model a perfect fit for expressing distributed systems. And today there is a distributed system of many CPU cores within even the smallest devices.