Your SlideShare is downloading. ×
  • Like
Actor-based concurrency and Akka Fundamentals
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Actor-based concurrency and Akka Fundamentals



Published in Software
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Actor-based concurrency and Akka Fundamentals Bon Festival 2014 Ngoc Dao
  • 2. Benefit of actor in one sentence C vs Java: You can use memory without having to free memory. Thread vs actor: You can use concurrency without having to create threads. Don't communicate by sharing memory; share memory by communicating.
  • 3. Actor model ● Actor = states + mailbox + behaviors (msg handlers) s/csys-2013/2013-12-03/ ● From outside, can’t manipulate actors directly. ● To interact with an actor, must send msgs to it. ● Each actor has a mailbox, msgs are put to mailbox, and processed one by one. ← An actor is like a single threaded process; it doesn’t do more than one thing at a time.
  • 4. Concurrency: Actor vs Thread Thread: ● Heavy weight: Can only create not too many threads; usually: 2000~5000 ● Shared state ← Source of bugs ● Passive: Have to call object.method() to make the object alive. Actor: ● Light weight: Can create millions of actors; usually: ~2.5 million actors/GB ● Shared nothing ● Active: Actors are alive by themselves. ← Easy to model programs that have millions of on-going things (very high level of concurrency), like MMOG games.
  • 5. Concurrency: Actor vs Thread ● Thread: n dimensions, hard to reason about. ● Actor: 1D, one thing at a time. var1 var2
  • 6. Concurrency: Actor vs Thread ● Actor is a high level logical way to think, to model programs. ● At lower level, actors run above a thread pool.
  • 7. Akka (an implementation of actor model)
  • 8. Akka: Demo ● Live coding demo
  • 9. Demo: Counter // First, define messages object Counter { case class Inc(amount: Int) case class Dec(amount: Int) }
  • 10. import // Define actor state and msg handler class Counter(name: String) extends Actor { // Actor state private var value = 0 override def preStart() { println(s"I'm alive: $name") } override def postStop() { println(s"I'm dead: $name") } ...
  • 11. ... import Counter._ // Msg handler def receive = { case Inc(amount) => value += amount println(s"Value: $value") case Dec(amount) => value -= amount println(s"Value: $value") } }
  • 12. import{ActorSystem, Props} // Think of this as a thread pool val system = ActorSystem("mysystem") // Create actor reference (instance) val c1 = system.actorOf( Props(new Counter("mylock")) ) // Send messages to the actor c1 ! Counter.Inc(1) c1 ! Counter.Dec(2)
  • 13. Demo: Behavior transition CodeLock:
  • 14. Some actor antipatterns Send mutable msgs between actors. ← May lead to bug, if actor A sends msg M to actor B, state of B incorporates M, then M is later changed by A. Fix: Use immutable msgs.
  • 15. Some actor antipatterns From inside Callback { def onCallback() { directlyModifyActorState() } }) ← May lead to bug, because the actor’s thread and the callback’s thread may be 2 different threads. Remember: An actor is like a single threaded process, can’t do more than one thing at a time. Fix: self ! msgFromCallback
  • 16. References ● Concurrent Programming for Scalable Web Architectures index.html ● Functions + Messages + Concurrency = Erlang erlang-qcon08 ● Akka doc