Concurrency and
Parallelism with Scala

       Timothy Perrett
    BrisFunctional, June 2011



           software. done right.
About Me
  Big Scala Geek
  Author of Lift in Action
  Coding Scala since 2007
  Background in both dynamic
   and statically typed languages
  Manufacturing and marketing
   automation is my day job
                                              h"p://manning.com/perre"/	
  



                      software. done right.
Concurrency
       vs
Parallelism

   software. done right.
Concurrency


Mutable state is
   Plain Evil


     software. done right.
Concurrency


Threads and Locks
    are EOL


      software. done right.
Concurrency


 Actors to
the Rescue


  software. done right.
Concurrency

   msg              msg


 msg        msg           msg

                                Mailbox
            msg



           Actor



       software. done right.
Concurrency
  Simpler mental model
  Much easier to avoid common issues:
    Deadlocks
    Race Conditions
    Starvation
    etc




                       software. done right.
Concurrency
case class Increment(amount: Int)	

class Counter extends Actor {	
   private var count = 0	
   def receive = {	
      case Increment(by) =>	
        count += by	
        println(count)	
   }	
}	

                 software. done right.
Concurrency


val counter = actorOf[Counter]	
        counter.start	




            software. done right.
Concurrency



counter ! Increment(10) 	


        Fire	
  and	
  forget	
  




         software. done right.
Concurrency



counter !!! Increment(10) 	


        Fire	
  yielding	
  a	
  Future	
  




             software. done right.
Concurrency




  software. done right.
Concurrency




  software. done right.
Concurrency


Don’t Block.
    Ever.
  (it’s	
  a	
  zen	
  thing)	
  




    software. done right.
Parallelism


   software. done right.
Parallelism


Collections
on crack i-core
      ____________

      Mult

  software. done right.
list.filter(predicate)	




        software. done right.
list.filter(predicate).par	




          software. done right.
Parallelism




Create	
  small	
  units	
  of	
  work	
  to	
  split	
  over	
  available	
  processor	
  capacity	
  



                                      software. done right.
Parallelism

              Stolen!




Processor A                   Processor B



          software. done right.
Plain
Lazy


software. done right.
Not 100%
Toll Free


 software. done right.
Summary
  Manually handling threads and locks sucks
  Actors provide a sane model for concurrency
  Build completely asynchronous systems
  Get the most out of your hardware investment




                    software. done right.
Further Reading
  Scala Language
  http://scala-lang.org

  Akka Actor Toolkit
  http://akka.io/

  State: You’re Doing It Wrong
  http://www.slideshare.net/jboner/state-youre-doing-it-wrong-javaone-2009

  A Generic Parallel Collection Framework
  http://infoscience.epfl.ch/record/150220/files/pc.pdf




                               software. done right.
Questions?
twitter.com/timperrett                      Lift in Action
github.com/timperrett                  manning.com/perrett/
blog.getintheloop.eu




                    software. done right.

Concurrency and Parallelism with Scala