Understanding the thread of actors in scala

I was told that (Scala) Actors never perform two operations at the same time, which suggests that the act (or respond? Or receive?) Method is essentially synchronized. I know that a lengthy operation in an action method can cause blocking problems, and I assume that access to the message queue should somehow synchronize ... but ...

It has been suggested that an actor receiving messages telling him to increase the internal counter will increment the counter in a file-stream manner. That no two update messages will be processed at the same time, and therefore no two messages can try to update the counter at the same time.

The counter attribute in the actor sounds like a “general state”.

Is it true that such an operation will be completely thread safe? If so, how can an actor use several basic machines in some effective way? How is an actor multithreaded?

If not, what is the appropriate idiomatic way to count messages in streaming mode without the need for any synchronized / mutable variable?

+6
multithreading scala functional-programming actor
source share
3 answers

The Actor model can be used to isolate a volatile state from the outside world. When you have a mutable state (for example, a global registry of identifiers assigned to several parallel processes), you can wrap this mutable state inside an actor and force clients to communicate with the Actor via message passing. Thus, only the actor accesses the mutable state directly, and, as you say, the client message queue must be read and processed one by one. It is important that the messages are unchanged.

To avoid queue filling, it is important that message processing ( react , receive , etc.) is react as short as possible. Long-term tasks must be transferred to another player:

 1. Actor A receives a message M from sender S 2. A spawns a new actor C 3. A sends (S, f(M)) to C 4. In parallel: 4a. A starts processing the next message. 4b. C does the long-running or dangerous (IO) task, When finished, sends the result to S, and C terminates. 

Some alternatives in the process:

So, to repeat, the actor is multithreaded to the point that several clients can send him several messages from different streams, and it is guaranteed that the actor will process all these messages in turn (although the order may be subject to various not too strict restrictions).

Please note that although Actor react code can be executed in different threads , at one given point in time it is executed on only one given thread (you can imagine that the actor goes from thread to thread as the scheduler considers necessary, but this is a technical detail) . Note. Internal state still does not require synchronization, as an action occurs between message processing . - before semantics .

Parallelism is achieved through the simultaneous use of several actors, usually forming a supervisor hierarchy or balancing workload .

Please note that if all you need is parallel / asynchronous computing, but you don’t have or cannot get rid of the global state, Future better compiled and a more understandable concept.

+8
source share

Actor is not multithreaded, but usually there is an actor system. Each actor performs only one action at a time, but when there are several actors, each of them can work in their respective encapsulated state in parallel. A counter attribute is not a common mutable state if it is not shared among the participants.

If your question is about implementing an actor system, this changes and is usually customizable, i.e. By default, Scala actors can be configured to run both a single thread and a thread pool, or using Java ForkJoin tasks. I think the source of scala.actors is very readable, so I recommend taking a look if you want to understand what is happening.

+6
source share

You can use a separate actor to count. When an actor receives a message, he can send a message to one scoring actor (singleton). This way, you can have several active participants and still read the messages.

Akka has something called Agents that may be useful in this context.

 val counter = Agent(0) counter send (_ + 1) 

http://doc.akka.io/docs/akka/2.0.2/scala/agents.html

+2
source share

All Articles