This document discusses actor-based programming in Erlang, Scala, and F#. It provides overviews of the actor model and how it is implemented in each language. In Erlang, actors are lightweight processes that communicate asynchronously via message passing without shared state. Scala leverages the actor model with both reactive and event-driven styles. F# uses MailboxProcessors to implement the actor model on the .NET framework. The document also covers supervision strategies for fault tolerance in Erlang OTP and Scala Akka frameworks.
6. Shared memory model
Shared
memory
Process Process
thread thread
Process
thread
Page 6
Actor model baksia , 29.09.2012
7. Problems with shared memory
• Race conditions
• Block contention
• Deadlock
Page 7
Actor model baksia , 29.09.2012
8. What is the Actor Model
• Light weight processes/threads communicating through
messaging
• Messages are buffered in a “mailbox”
• No shared state
• Isolated from other processes
• Normally event systems are inversion of control
• Adding a listener to an object
• Hard to maintain in large systems
• Humans do not share memory, we communicate through
messages
Page 8
Actor model baksia , 29.09.2012
10. Erlang
• The world is concurrent
• Things in the world don't share data
• Things communicate with messages
• Things fail
- Joe Armstrong
Page 10
Actor model baksia , 29.09.2012
11. Crash course in Erlang
Variables are immutable
Page 11
Actor model baksia , 29.09.2012
12. Atoms and tuples
• An atom is a global constant starting with lower case
• A tuple is an ordered set of elements
• Your Java or C# class would be a tuple with an atom
identifier
Page 12
Actor model baksia , 29.09.2012
13. Spawn and process ID
• Spawn “spawns” a new Erlang process (light weight)
• Spawn returns a PID (Process identifier)
• Receive waits for a message
Page 13
Actor model baksia , 29.09.2012
15. Crash course in Scala
• Object functional programming language
• Everything in Scala is an object, even functions
• Running on JVM and interoperable with Java language
• Also runs on Dalvik VM for Android (Java bytecode)
• Concurrent oriented programming language
• Actor model programming style from Erlang
• Interactive shell
• No attachment to Oracle
Page 15
Actor model baksia , 29.09.2012
17. Scala Actors
Page 17
Actor model baksia , 29.09.2012
18. React or receive
• React is the “normal” actor, lightweight process
• Receive spins out a new Java thread
• In the paper *
• 5000 threads
• 1 200 000 actors
• React uses partial functions to send the process to the
heap
• Receive runs on the stack and is heavyweight JVM
threads (like Java)
*Actors That Unify Threads and Events P. Haller, M. Odersky
Page 18
Actor model baksia , 29.09.2012
19. Scala Actor on tuples
Page 19
Actor model baksia , 29.09.2012
20. Scala Actor with case classes
Page 20
Actor model baksia , 29.09.2012
21. Crash course in F#
• F# is a object functional programming language for the
.Net Framework
• Derived from ML and is largely compatible with OCaml
• Interactive shell, script and compiled language
• Runs on mono for Posix based systems
Page 21
Actor model baksia , 29.09.2012
24. Let it crash (supervision)
• Erlang/OTP
• Scala Akka
• Erlang designed for five nines 99.999 uptime, record is
nine nines 99.999999999
• That is 31ms downtime a year
Page 24
Actor model baksia , 29.09.2012
25. Supervisors in Erlang/OTP and AKKA
Supervisor
Process
Process
Process
Page 25
Actor model baksia , 29.09.2012
26. Supervising supervisors
Root
Supervisor
Supervisor
Supervisor
Process
Process
Process Process
Process Process
Page 26
Actor model baksia , 29.09.2012