CS Programming Languages: Scala

Similar documents
Programming Paradigms for Concurrency Lecture 10 The Actor Paradigm

Combining Concurrency Abstractions

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

Scaling Up & Out. Haidar Osman

Scala Actors. Scalable Multithreading on the JVM. Philipp Haller. Ph.D. candidate Programming Methods Lab EPFL, Lausanne, Switzerland

Explain briefly how starvation may occur in process scheduling. (2 marks)

Covers Scala 2.10 IN ACTION. Nilanjan Raychaudhuri. FOREWORD BY Chad Fowler MANNING

Multithreading and Interactive Programs

Concept of a process

IGL S AND MOUNT NS. Taking AKKA to Production

Advances in Programming Languages

IV. Process Synchronisation

Multithreading and Interactive Programs

THREADS & CONCURRENCY

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

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

Comp 310 Computer Systems and Organization

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

The Actor Model. Towards Better Concurrency. By: Dror Bereznitsky

CS 179: GPU Programming LECTURE 5: GPU COMPUTE ARCHITECTURE FOR THE LAST TIME

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

CS 6112 (Fall 2011) Foundations of Concurrency

Akka Java Documentation

CS 556 Distributed Systems

Akka. Developing SEDA Based Applications

CSE Traditional Operating Systems deal with typical system software designed to be:

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Programming with Algebra

CS 450 Operating System Week 4 Lecture Notes

CS Programming Languages: Scala

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

CS 351 Design of Large Programs Threads and Concurrency

Problems with Concurrency. February 19, 2014

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Lecture 27: Safety and Liveness Properties, Java Synchronizers, Dining Philosophers Problem

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

THREADS & CONCURRENCY

Making most of Scala. Akka, Scala, Spray, Specs2; all in 50 minutes!

An actor system for Scala.js Semester project, Fall 2013

An Introduction to Parallel Systems

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Threads, Concurrency, and Parallelism

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Dealing with Issues for Interprocess Communication

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Operating Systems (2INC0) 2017/18

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Actors in the Small. Making Actors more Useful. Bill La

High-level Parallel Programming: Scala on the JVM

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Processes COMPSCI 386

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Virtual Machine Design

Processes and Threads

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

Reactive Application Development

Synchronization Principles II

Evolution of an Apache Spark Architecture for Processing Game Data

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

CS Programming Languages: Scala

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

Process Management And Synchronization

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

Threads & Concurrency

Problems with Concurrency

Dept. of CSE, York Univ. 1

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

THREADS AND CONCURRENCY

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

Deadlocks. Deadlock in Resource Sharing Environment. CIT 595 Spring Recap Example. Representing Deadlock

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 22: Remote Procedure Call (RPC)

Threads & Concurrency

Chapters 5 and 6 Concurrency

Midterm Exam. October 20th, Thursday NSC

SFDV3006 Concurrent Programming

Chapter 6: Process Synchronization

Fault Tolerance. Goals: transparent: mask (i.e., completely recover from) all failures, or predictable: exhibit a well defined failure behavior

Concepts and Technologies for Distributed Systems and Big Data Processing Futures, Async, and Actors

More on Synchronization and Deadlock

CONCURRENCY AND THE ACTOR MODEL

Concurrent ML. John Reppy January 21, University of Chicago

Chapter 4: Multi-Threaded Programming

Lecture 3: Intro to Concurrent Processing using Semaphores

Object Oriented Programming and Design in Java. Session 18 Instructor: Bert Huang

Concurrency with Threads and Actors

CS 318 Principles of Operating Systems

CSE 153 Design of Operating Systems

Akka$Concurrency$Works

Philipp Wille. Beyond Scala s Standard Library

Threads Questions Important Questions

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Processor speed. Concurrency Structure and Interpretation of Computer Programs. Multiple processors. Processor speed. Mike Phillips <mpp>

Transcription:

CS 3101-2 - Programming Languages: Scala Lecture 6: Actors and Concurrency Daniel Bauer (bauer@cs.columbia.edu) December 3, 2014 Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 1/19

1 Actors and Concurrency Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 2/19

Concurrency Goal: run several computations at the same time to make better use of resources. CPU and IO Multicore CPUs Cloud instances Examples: Perform background computation while waiting for user or network I/O. Speed up computation by splitting it over multiple CPU cores. Pandora s Box: Different branches of computation share resources and need to communicate. Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 3/19

Concurrency - multiple processes vs. threading Time sharing/scheduling in the operating system Multiple processes. IPC via sockets/pipes/signals. Threading Multiple parallel threads within the same process Threads are automatically mapped to different CPUs by OS or VM. IPC via shared memory or messaging. Generally, want to allow the developer to write concurrent programs without having to think about the low-level mechanism. Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 4/19

Problems with Concurrency Shared resources (Files, IO) Read and write access to shared data. Different threads communicate with each other. How? Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 5/19

Concurrency in Java In Java threads communicate via shared data (multiple threads have read/write access to the same object). Threads are special objects (subclass of Thread or implementation of Runnable). Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 6/19

Java Concurrency Example Java uses Monitors: Blocks of code marked as synchronized Only one thread can execute every synchronized block at a time. Only one synchronized block can be executed in every object at a time. This establishes a lock on the object s attributes. public synchronized void atomic_ add ( int y) { x = x+y; Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 7/19

Deadlocks: The Dining Philosophers Problem Dining philosophers by Benjamin D. Esham - License: CC-BY-SA 3.0 via Wikimedia Commons Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 8/19

Synchronization in Scala: The Actor Model An actor is a self-contained branch of the program. Actors share nothing with other actors. No locks required. Instead actors communicate via message passing. Every actor has a mailbox (infinite message queue). Other actors can send messages (arbitrary Scala objects to the mailbox). Actors consume messages in the order they arrive. Scala now uses the actors in Akka. Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 9/19

Actors in Akka Actors are objects that extend or mix-in the akka.actors.actor trait. Actors have a method receive that handles incoming messages. An ActorSystem is a hierarchical group of actors with a common configuration, used to create new actors. Props are configuration objects using when creating an actor. akka. actor. Actor class HelloActor extends Actor { def receive = { case " ping " => println (" pong ") case _ => println (" huh?") Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 10/19

Creating Actors An ActorSystem is a hierarchical group of actors with a common configuration, used to create new actors. Props are configuration objects using when creating an actor. akka. actor.{ Actor, Props, ActorSystem class HelloActor extends Actor { def receive = { case " ping " => println (" pong ") case _ => println (" huh?") scala > val system = ActorSystem (" HelloSystem ") system : akka. actor. ActorSystem = akka :// HelloSystem scala > val props = Props ( new HelloActor ) props : akka. actor. Props =... scala > val actorref = system. actorof ( props ) actorref : akka. actor. ActorRef = Actor [ akka :// HelloSystem / user /$b # -1186551897] Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 11/19

Passing Messages with! akka. actor.{ Actor, Props, ActorSystem class HelloActor extends Actor { def receive = { case " ping " => println (" pong ") case _ => println (" huh?") scala > val system = ActorSystem (" HelloSystem ") scala > val props = Props ( new HelloActor ) scala > val actorref = system. actorof ( props ) actorref : akka. actor. ActorRef = Actor [ akka :// HelloSystem / user /$b # -1186551897] scala > actorref! " ping " pong Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 12/19

Multiple Actors akka. actor.{ Actor, ActorSystem, Props class HelloActor extends Actor { def receive = { case " ping " => { println ( self. path. name ); Thread. sleep (1000); println (" pong ") case _ => { println ( self. path. name ); Thread. sleep (1000); println (" huh?") object HelloActor { def main ( args : Array [ String ]) { val system = ActorSystem (" HelloSystem ") val actor1 = system. actorof ( Props ( new HelloActor ), name = " actor1 ") val actor2 = system. actorof ( Props ( new HelloActor ), name = " actor2 ") actor1! " ping " actor2! " test " actor1! " ping " Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 13/19

Asynchronous I/O akka. actor.{ Actor, ActorSystem, Props scala. io. StdIn. readline sealed abstract class Messages case class Request ( val prompt : String ) extends Messages case class Response ( val response : String ) extends Messages class InputRequest extends Actor { val printer = context. actorof ( Props [ PromptDisplay ]) val reader = context. actorof ( Props [ InputReader ]) def receive = { case Request ( prompt ) => { // Waits for input reader! Request ( prompt ) // Repeatedly prints the prompt printer! Request ( prompt ) case Response ( resp ) => { println (" Input was : "+resp ) context. system. shutdown () Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 14/19

Asynchronous I/O class InputReader extends Actor { def receive = { case Request (_) => { val input = readline () // blocks sender ()! Response ( input ) class PromptDisplay extends Actor { def receive = { case Request ( prompt ) => { println ( prompt ); Thread. sleep (2000); self! Request ( prompt ); Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 15/19

Asking actors for values Retrieve the result of some computation in a non-actor context. akka. pattern. ask akka. util. Timeout scala. concurrent.{ Await, Future scala. concurrent. duration._ class FancyActor extends Actor { def receive = { case Compute ( input ) => { val result =??? // Do some fancy computation sender! result object IoTest { def main ( args : Array [ String ]) { val system = ActorSystem (" System ") val actor = system. actorof ( Props [ FancyActor ]) // needed for ask implicit val timeout = akka.util.timeout (1. second ) val data =??? // Some input data val msg = Compute ( data ) val future : Future [String ] = ask (actor, msg ). mapto [String ] implicit val ec = system. dispatcher val result = Await. result ( future, timeout. duration ) println ( result ) Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 16/19

Scala Futures We can also use futures without actors! Often preferable for parallel computation (if there is no state). scala. concurrent._ scala. concurrent. duration._ scala. concurrent. ExecutionContext. Implicits. global val future = Future { // some Fancy computation here 27*3 println (" Okay ") val result = Await. result ( future, 1. second ) println ( result ) Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 17/19

Futures for Parallel Computation scala. concurrent._ scala. concurrent. duration._ scala. concurrent. ExecutionContext. Implicits. global val future = Future { // some Fancy computation here 27*3 println (" Okay ") val result = Await. result ( future, 1. second ) println ( result ) Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 18/19

Approximating Pi π 4 = ( 1) n 2n + 1 = 1 1 3 + 1 5 1 7 + n=0 scala. concurrent._ scala. concurrent. duration._ scala. concurrent. ExecutionContext. Implicits. global def calc (i: Int, nrofelements : Int ): Future [ Double ] = Future { val start = i * nrofelements var acc = 0.0 for (i <- start until ( start + nrofelements )) acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1) println (" branch "+i+" done ") acc val n = 10 val elements = 1000 val futures = for (i <- 0 until n) yield calc (i, elements ) val result = Future. fold ( futures )(0.0)( _+_) result. onsuccess { case pi => println (pi) Daniel Bauer CS3101-2 Scala - 06 - Actors and Concurrency 19/19