Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Async Programming & Actor Model

Pishen Tsai
July 24, 2015
68

Async Programming & Actor Model

Pishen Tsai

July 24, 2015
Tweet

Transcript

  1. Asynchronous programming val a: Future[Int] = ??? def f(x: Int):

    Future[Int] = ??? val r = a.map(x => f(x)) Future[Future[Int]]
  2. Asynchronous programming val a: Future[Int] = ??? def f(x: Int):

    Future[Int] = ??? val r = a.flatMap(x => f(x)) Future[Int]
  3. Asynchronous programming val a: Future[Int] = ??? def f(x: Int):

    Future[Int] = ??? val r = a.flatMap(x => f(x)) val p = Await.result(r, 3.seconds) Int
  4. T T T T T T T Future { ???

    } a.map(???) ??? Await(???)
  5. T T T T T T T Future { ???

    } a.map(???) F ??? Await(???)
  6. T T T T T T T Future { ???

    } a.map(???) F ??? Await(???)
  7. T T T T T T T Future { ???

    } a.map(???) F ??? F Await(???)
  8. T T T T T T T Future { ???

    } a.map(???) F ??? F Await(???)
  9. T T T T T T Future { ??? }

    a.map(???) F F ??? Await(???) T
  10. T T T T T T Future { ??? }

    a.map(???) F F ??? Await(???) T
  11. T T T T T T Future { ??? }

    a.map(???) F F ??? Await(???) T
  12. T T T T T T Future { ??? }

    a.map(???) F F ??? Await(???) T
  13. T T T T T T Future { ??? }

    a.map(???) F F ??? Await(???) T
  14. T T T T T T Future { ??? }

    a.map(???) F F ??? Await(???) T C C
  15. T R

  16. T R C C C C C C C C

    T T R R R R R R R R R R R R R R 1000+
  17. Orz T T R C C C C C C

    C T T R R R R R R R R R R R R R R 1000+ T T T T T T T T T switching thread takes time... crashed, nothing get computed after all...
  18. R C C C C C C C T R

    R R R R R R R R R R R R R 1000+ T thread pool C T T T T T T
  19. R C C C C C C C T R

    R R R R R R R R R R R R R T C T T T T T T access DB long computing
  20. R C C C C C C C T R

    R R R R R R R R R R R R R T C T T T T T T access DB long computing blocking
  21. R C C C C C C C T R

    R R R R R R R R R R R R R C T T T T T T T T T T T T T T T T T T T T T T T T T T T T T F F F F F F 1000+ async squeeze out the performance of your machine
  22. HP MP position weapon exp hunger Running Attacked Attacking Need

    lock(s) to prevent strange things happen. Eating
  23. HP MP position weapon exp hunger Running Attacked Attacking No

    matter you forward the job to DB or what. Eating
  24. T T T T T T T T T T

    T T T T T T T front m back
  25. T T T T T T T T T T

    T T T T T T T front m l back
  26. T T T T T T T T T T

    T T T T T T T front m l back Disk
  27. T T T T T T T T T T

    T T T T T T T front m l back Machine A T T T T T T s Machine B Disk
  28. Tell & Ask val a: ActorRef = ctx.actorOf(???) a !

    “Hello!” (a ? “What’s your name?”) .mapTo[String] .map(name => ???)
  29. Receive messages class MyActor extends Actor { def receive =

    { case “Hello!” => ??? case “What’s your name?” => sender ! “Pishen” } }