Favoring Isolated Mutability The Actor Model of Concurrency


Download Favoring Isolated Mutability The Actor Model of Concurrency


Preview text

Favoring Isolated Mutability The Actor Model of Concurrency
CSCI 5828: Foundations of Software Engineering Lecture 24 — 04/11/2012

© Kenneth M. Anderson, 2012

1

Goals
• Review the material in Chapter 8 of the Concurrency textbook • that deals with the fundamentals of the actor approach • and the use of UntypedActors in Akka
• We’ll review the use of • TypedActors, and • Mixing STM with the Actor Model
• in our next lecture

© Kenneth M. Anderson, 2012

2

“If it hurts, stop doing it”

• With concurrency, we have learned • that the shared mutability approach to concurrency leads to lots of problems and difficulty
• As a result, we’ve been looking at alternatives • With the STM model, we looked at managed mutability • where all values were immutable • but a “ref” or an “identity” could be associated with different values over time; with change occurring inside of transactions
• Now, we’ll look at the isolated mutability approach • as embodied in the Actor Model of Concurrency

© Kenneth M. Anderson, 2012

3

Isolated Mutability and the Actor Model
• Isolated Mutability is a design approach in which • we can have mutable values • but we make sure that for each mutable value • only one thread has access to it
• The Actor Model of concurrency is one in which • we have multiple lightweight processes, known as actors • each actor can have mutable state if it wants • because no other actor has access to that state • actors instead pass immutable messages to each other if they need to communicate or coordinate • these messages are passed asynchronously and are processed in the order that they arrive

© Kenneth M. Anderson, 2012

4

The Actor Model: Background
• This model has been around for a long time • It is built into the functional programming language, Erlang
• And was also built into Scala, a hybrid functional/OO language • Scala is built on top of the JVM • it can call Java code and Java can call Scala code • as we saw when working with the STM • The Akka framework is written in Scala

© Kenneth M. Anderson, 2012

5

Actor Model
• To emphasize • Programs that use the Actor model are multithreaded • but each individual actor is single threaded • they each have access to mutable state • but an actor cannot access the mutable state of another actor • all one actor can do to another actor is send an immutable message • since the messages are immutable, it is safe to share them between actors • likewise, all an actor can do is sit and wait for messages to arrive

© Kenneth M. Anderson, 2012

6

Actor Model == OOP++?
• The book asserts that the actor model can be seen as taking OOP to the next level • We have objects and they can have mutable state • but they each run on their own thread • and all we can do is send messages to them • we can’t call their methods directly

© Kenneth M. Anderson, 2012

7

Actor Qualities (I)
• Each actor is an independent activity • it can receive messages • process them • and send messages
• Each actor has a built in message queue • it can receive multiple messages at once • it can send a message at the same time that other actors are sending them • As a result, there is plenty of opportunity for concurrency!

© Kenneth M. Anderson, 2012

8

Actor Qualities (II)

• Actor does not equal Thread • Instead, think of an actor as a task
• Recall how we separated task decomposition from thread allocation? • Allowing us to create, for instance, a thread pool with 20 threads • And then allocate 100 tasks to be processed by the thread pool
• The same thing happens with actors • We will likely have X actors being managed by Y threads where • X >> Y
• We can get away with this because of the actor lifecycle

© Kenneth M. Anderson, 2012

9

Actor Life Cycle

Creating Actors • 167

If an actor is active but has

no messages, then it is

essentially blocked; we can

swap it out and let some

Figure 12—Life cycle ofoanthaectroar ctor run

© Kenneth M. Anderson, 2012

10

Preparing to load PDF file. please wait...

0 of 0
100%
Favoring Isolated Mutability The Actor Model of Concurrency