liveBook · Manning

Actor

As explained in Functional Programming in Scala, Second Edition

An actor is a computational component that communicates through asynchronous messages and processes them according to its internal state.

An actor is a non-blocking concurrency primitive that communicates through messages. It occupies a thread only while processing a message, handles one message at a time, and queues messages sent concurrently. This serialized processing makes shared coordination less vulnerable to race conditions and deadlocks. section 7.3.4 section 7.6

Actors are useful when combining asynchronous results. In non-blocking Par.map2, an actor records whichever result arrives first and applies the combining function after both results arrive, avoiding direct low-level coordination between concurrent callbacks. section 7.3.4

A message echoer

An actor can receive strings and print each one: sending “hello,” “goodbye,” and a longer question produces the corresponding output. The ! operation sends messages asynchronously to the actor.

Combining two parallel results

A map2 actor receives either the left or right result, stores it when its partner is absent, and invokes the callback once both values are available.

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines message processing and demonstrates map2 coordination, but defers the subtle implementation details of a complete actor runtime.
Examples
Demonstrates a string echoer and an actor that combines two asynchronous Par results.
What it assumes
Assumes familiarity with asynchronous callbacks, parallel computations, and Scala execution services.
What you can do after
Apply it via a Scala actor implementation to serialize concurrent messages and coordinate asynchronous callbacks.
How it connects
Connects actors with message passing, non-blocking concurrency, parallel computation, callbacks, race conditions, and deadlocks.

Also explained in

Compare how 7 books explain actor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.