Programação Multiparadigma (2014/2015)



Teórica 12 (24/Nov/2014)

Concurrency and Actors in Scala.



Actors

[This lesson is dedicated the the "classic actors" of Scala and not to the newer Akka actors.]

Scala, and some other languages like Erlang, use the well known (and very old) high-level Actor model of concurrency.

An actor is a concurrent process that communicate with other actors by exchanging messages. It is assumed that shared resources are not used, what greatly simplifies the use of the model. [Of course, we can create an actor to manage a "de facto" shared resource, but this resource technically belongs to the single actor.]

An actor is an reactive entity that reacts to the messages by making local decisions and send more messages. An actor can have, and often have, local state. An actor can communicate with any other actor it knows and the actors themselves can be passed in messages.

We can regard an actor as a kind of object because it is able to react to messages, and can have local state. However there is a difference: object-oriented software is typically executed sequentially, while the Actor model is inherently concurrent. This is a very important point: communications among actors is asynchronous.

In most Actor systems, the messages that an actor has received and have not been handled yet are buffered in a structure known as mailbox. An actor can select the next message to handle by using pattern-matching and the order of arrival of the messages is also important. Typically, an actor runs an infinite loop in which it deals with the messages, one by one. When there are no messages available, the actor blocks, waiting for more messages to come. Note that a single actor is not internally concurrent because the messages are always handled one by one.

The Actor Model it is superior to shared state parallel programming because it reduces many pitfalls like deadlocks and race conditions, and this can be observed in practice. However deadlocks are still possible because locking is required in the management of the mailboxes.

Note that using actors no one need to deal with explicit locking and thread management. It is easier to write correct concurrent systems.


Actors in Scala

Actors is not the only concurrency model available in Scala. For example, Scala can also supports the lower level threads of Java and the accompanying basic mechanisms of mutual exclusion of shared resources: wait, notify and synchronized, provided by class AnyRef.

In the history of Scala, three kinds of Actors systems have been introduced:

In this course we will use lightweight actors only.

In Scala, an actor is a object that is created by instantiating a subclass of the trait Actor, or by calling the actor method. Interestingly enough, the messages are also objects, usually defined with case object to enable pattern-matching over them.

The actors in Scala go beyond the basic model in that synchronous messages are also available. However this kind of messages should be used rarely and carefully.

Below, is a summary of the main Scala operations related with actors. They are defined in the trait Actor and in the singleton object Actor, inside the package scala.actors.

Example of set of messages: Example of simple actor:

More detailed information about the actor operations: here.


Asynchronous programming

Programming using only asynchronous messages turns out to be a very particular style of programming that worth a try.


Examples discussed during the lecture



#12