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

Similar documents
Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

Design Approaches for Concurrent Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 02/09/2012

Clojure Concurrency Constructs. CSCI 5828: Foundations of Software Engineering Lecture 12 10/02/2014

Grand Central Dispatch and NSOperation. CSCI 5828: Foundations of Software Engineering Lecture 28 12/03/2015

CMPT 435/835 Tutorial 1 Actors Model & Akka. Ahmed Abdel Moamen PhD Candidate Agents Lab

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

Safety SPL/2010 SPL/20 1

Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors. Jonas Bonér Scalable Solutions

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Concurrent & Distributed Systems Supervision Exercises

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

This is a talk given by me to the Northwest C++ Users Group on May 19, 2010.

CS Programming Languages: Scala

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Clojure Lisp for the Real clojure.com

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

Chapter 4: Multi-Threaded Programming

The Proxy Pattern. Design Patterns In Java Bob Tarr

CmpSci 187: Programming with Data Structures Spring 2015

Introduction to Locks. Intrinsic Locks

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors. Jonas Bonér Viktor Klang

Problems with Concurrency. February 19, 2014

Java SE 7 Programming

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 12 09/29/2016

Objects Managing a Resource

Virtual Machine Design

Java 8 Programming for OO Experienced Developers

Spot: A Programming Language for Verified Flight Software

Threads Questions Important Questions

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

Introduction to Programming Using Java (98-388)

Lecture 8: September 30

Question 1. Notes on the Exam. Today. Comp 104: Operating Systems Concepts 11/05/2015. Revision Lectures

CMPSCI 187: Programming With Data Structures. Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

c) And last but not least, there are javadoc comments. See Weiss.

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

CORE JAVA TRAINING COURSE CONTENT

Java Threads and intrinsic locks

F. Tip and M. Weintraub CONCURRENCY

Introduction to User Stories. CSCI 5828: Foundations of Software Engineering Lecture 05 09/09/2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Synchronization SPL/2010 SPL/20 1

Functional Programming Patterns And Their Role Instructions

Programming Languages and Techniques (CIS120)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CPS221 Lecture: Threads

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

CSCD 330 Network Programming

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

CS558 Programming Languages

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CSCD 330 Network Programming

Java SE 7 Programming

Parallel Computing. Lecture 16: OpenMP - IV

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

CSE 160 Lecture 7. C++11 threads C++11 memory model

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

The Dynamic Typing Interlude

CST242 Concurrency Page 1

Vector and Free Store (Pointers and Memory Allocation)

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

CS 326: Operating Systems. Process Execution. Lecture 5

Comparing Algorithms. Example Access Control System. Unsorted lists new record. Unsorted lists search records. Cycle Counts

Chapter 17 vector and Free Store. Bjarne Stroustrup

Rules and syntax for inheritance. The boring stuff

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS140 Operating Systems and Systems Programming Midterm Exam

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Lecture Notes on Memory Layout

Introduction to Computer Science I

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Cover Page. The handle holds various files of this Leiden University dissertation

MF#K - Introduction to F# GroupM Mødegruppe for Ƒunktionelle Københavnere (MF#K)

CS113: Lecture 4. Topics: Functions. Function Activation Records

Weeks 6&7: Procedures and Parameter Passing

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

CS4961 Parallel Programming. Lecture 4: Data and Task Parallelism 9/3/09. Administrative. Mary Hall September 3, Going over Homework 1

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Software Design and Analysis for Engineers

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

An Introduction to Subtyping

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

Transcription:

Favoring Isolated Mutability The Actor Model of Concurrency CSCI 5828: Foundations of Software Engineering Lecture 24 04/11/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 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 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 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 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 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 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! 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 9

Actor Life Cycle If an actor is active but has no messages, then it is essentially blocked; we can swap it out and let some other actor run 10

Creating Actors (I) Support for the Actor Model is built into Scala as a result, Scala s syntax makes it easy to create actors and send messages to them and to process messages as well I m not going to cover the details of Scala in this class so we are going to use the Akka framework to implement actors in Java As a result, the information discussed in Lecture 19 on slides 34 and 35 is relevant here you will need to follow those instructions in order to compile our example programs 11

Creating Actors (II) The simplest actor in Akka has a class name of UntypedActor That class is located in the akka.actor package We can think of it as an abstract class that has one method we need to implement: public void onreceive(final Object message) We implement this method to indicate how our actor will handle its messages Note: the parameter type for message is java.lang.object In practice, only immutable types can be sent to us and we do need to perform checks at run-time to figure out what message was sent to us 12

Creating Actors (III) Our increment program lives again! public class Counter extends UntypedActor { private int count = 0; public void onreceive(final Object message) { if (message instanceof Integer) { count += (Integer)message; System.out.println("Count: " + count); } } } 13

Creating Actors (IV) Our increment program lives again! public class Drone extends UntypedActor { public void onreceive(final Object message) { if (message instanceof ActorRef) { ActorRef counter = (ActorRef)message; for (int i = 0; i < 5; i++) { counter.tell(new Integer(1)); } } } } 14

Creating Actors (V) To instantiate one of our actors, we make use of the Actors factory in the package akka.actors. We pass the factory the class of the instance, we want created We get back an ActorRef that points at our newly created Actor So, when we create an instance of our Counter actor we do not get back a reference to Counter we get back a reference to ActorRef We can use that reference to send messages to the Counter Why? We are not supposed to have access to instances of Counter directly; if we did, Counter s mutable variables might escape! 15

Creating Actors (VI) Our increment program lives again! The full program is in Increment.java Creating an ActorRef looks like this final ActorRef counter = Actors.actorOf(Counter.class); To start an Actor, you call start() on the ActorRef counter.start(); To send an asynchronous message, use the tell() method counter.tell(new Integer(100)); DEMO 16

Creating Actors (VII) The book had several examples of creating Actors DEMO One of its examples touches on creating Actors that have constructors Since you are not allowed to directly instantiate an Actor class, it is difficult to pass values to an Actor s constructor To do that, you need to create an anonymous instance of the UntypedActorFactory class that factory has a create() method that returns instances of UntypedActor and you can pass constructor arguments there You then pass the UntypedActorFactory to the actorof() method It uses the factory to create an instance and return an ActorRef 17

Sending Messages You can send messages in two ways tell(final Object message) -- sends an asynchronous, immutable message Future ask(final Object message) -- sends a message, provides future Future is NOT java.util.concurrent.future but it operates in the same way DEMO Once you get back a future you call Future.await() to block until a response is available You then call Future.result().get() to acquire the immutable response sent to you by the other actor The call to result().get() can fail; you need to call Future.result().isDefined() and only call get() when isdefined() returns true 18

Replying If you receive a message from another actor, how do you reply? If you are within the onreceive() method of an actor, you can simply call getcontext().channel() to get access to an ActorRef Once you have the ActorRef, you can call tell() and ask() as normal So, replying is really the same thing as just sending! 19

Handling Multiple Actors We already saw multiple actors in action with our simple Increment program The book returns to the PrimeFinder example It has a simple design We are provided the upper bound of our search and the number of partitions Our main program create one Actor per partition and sends it a range using ask(), which returns a Future Each actor calculates the number of primes in that range and sends it back Our main program loops through Future objects and calculates the total number of primes As you will see, the program maxes out the cores of my machine when I run the program 20

Coordinating Actors (I) To show how Actors can coordinate with each other, the book returns to the FileSize program Our previous versions of this program made use of coordination mechanisms (locks) and executors With the isolated mutability approach of the Actor model we can get a simpler solution and avoid some of the problems we encountered earlier such as when we locked up the thread pool with a poor design related to spawning tasks while traversing the file hierarchy 21

Coordinating Actors (II) The design of this system depends on two types of actors FileProcessors actors which process the size of a single directory we will create 100 of these to simulate the 100 threads we used earlier SizeCollector an actor who coordinates the FileProcessors and maintains a counter to keep track of the total size of the directory 22

Coordinating Actors (III) The design of this system also depends on three messages RequestAFile: sent by a FileProcessor to the SizeCollector The effect is to tell the SizeCollector, I m ready! FileToProcess: sent by SizeCollector to FileProcessor or vice versa provides a pointer to a file or directory that needs to be processed FileSize sent by FileProcessor to SizeCollector returns the size of a file/directory that was processed 23

Coordinating Actors (IV) Two key design points DEMO When a FileProcessor starts up, it needs to tell the SizeCollector that it is available It overrides a lifecycle method prestart() to do that That method ensures that we send a RequestAFile message to the SizeCollector A reference to the SizeCollector is passed in via FileProcessor s constructor When a FileProcessor is given a directory, it does not recursively work its way through all subdirectories. Instead, as it finds subdirectories, it sends FileToProcess messages to SizeCollector 24

Results In both cases (PrimeFinder and FileSizer), with the Actor Model, we get comparable performance to the previous solutions much simpler code no locks all code written from single threaded standpoint allows for use of mutable variables with predictable behavior 25

Summary Reviewed the basics of the Actor model Independent actors (which can be assigned to threads like tasks) with mutable state that is NOT shared with predictable semantics since the actor is single threaded communicating with other actors by passing immutable messages These constructs enable the isolated mutability approach to concurrency You get great performance with a very simple and straightforward model no thread allocation, no task decomposition, no locks 26

Coming Up Next Lecture 25: Advanced Actor Model Lecture 26: Creating Agile Software 27