Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka: what is the reason of processing messages one at a time in an Actor?

It is said:

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

Can you please explain what is the reason of processing messages one at a time in an Actor?

like image 947
Ivan Voroshilin Avatar asked Dec 15 '14 12:12

Ivan Voroshilin


People also ask

How do actors communicate in Akka?

Actors communicate using asynchronous messages. This ensures that the sender does not stick around waiting for their message to be processed by the recipient. Instead, the sender puts the message in the recipient's mailbox and is free to do other work.

Are Akka actors single threaded?

Behind the scenes Akka will run sets of actors on sets of real threads, where typically many actors share one thread, and subsequent invocations of one actor may end up being processed on different threads.

Which is one is the method in Akka actor life cycle?

Akka Actor life cycle methods This is overridable method, so you can override preStart() method to provide specific implementation for an Actor. It is invoked right after the starting of Actor and when an Actor is first created. In case of restart, it is called by postRestart() method.

Can Akka actors stop other actors?

In Akka, you can stop Actors by invoking the stop() method of either ActorContext or ActorSystem class. ActorContext is used to stop child actor and ActorSystem is used to stop top level Actor. The actual termination of the actor is performed asynchronously.


1 Answers

This way we can guarantee thread safety inside an Actor.

Because an actor will only ever handle one message at any given time, we can guarantee that accessing the actor's local state is safe to access, even though the Actor itself may be switching Threads which it is executing on. Akka guarantees that the state written while handling message M1 are visible to the Actor once it handles M2, even though it may now be running on a different thread (normally guaranteeing this kind of safety comes at a huge cost, Akka handles this for you).

It also originates from the original Actor model description, which is an concurrency abstraction, described as actors who can only one by one handle messages and respond to these by performing one of these actions: send other messages, change it's behaviour or create new actors.

like image 145
Konrad 'ktoso' Malawski Avatar answered Sep 25 '22 02:09

Konrad 'ktoso' Malawski