Pure Future

Please download to get full document.

View again

of 59
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Similar Documents
Information Report
Category:

Software

Published:

Views: 0 | Pages: 59

Extension: PDF | Download: 0

Share
Description
This presentation explains the difference between concurrency and parallelism, and how could we make parallel computations and how could we design an API for parallel computation following the structure presented in the Functional Programming in Scala book
Transcript
  • 1. Pure Future Wiem Zine Elabidine Krakow Scala User Group
  • 2. HELLO! I am Wiem Scala Backend Engineer at MOIA @WiemZin @wi101 2
  • 3. Plan Parallelism Vs Concurrency Thread Vs Callable Pure Future 3
  • 4. Plan Parallelism vs Concurrency Runnable vs Callable vs Future Pure Future 4
  • 5. 5 Place your screenshot here
  • 6. Parallelism vs Concurrency 6
  • 7. 7 Parallelism vs Concurrency Runtime.getRuntime.availableProcessors() Parallelism is the simultaneous execution of multiple things using the capability of the multi-core hardware.
  • 8. 8 Place your screenshot here Parallelism Image from: https://computing.llnl.gov/tutorials/parallel_comp/ Parallelism vs Concurrency
  • 9. Concurrency is the ability for many tasks to be executed out-of-order and coordinate the result. 9 Parallelism vs Concurrency
  • 10. 10 Place your screenshot here Concurrency Image from: http://adit.io/posts/2013-05-11-The-Dining-Philosophers-Problem-With-Ron-Swanson.html Parallelism vs Concurrency
  • 11. Concurrency - Dealing with MANY things at ONCE - Structure - Communication with independent components and coordinating with them Parallelism - Doing MANY things at ONCE - Execution - Executing simultaneous processes 11 Parallelism vs ConcurrencyParallelism vs Concurrency Summary
  • 12. Runnable vs Callable vs Future 12 Define parallel tasks
  • 13. Thread ✘ Thread is a linear flow of execution ✘ Managed by a Scheduler 13 Runnable vs Callable vs Future
  • 14. Thread ✘ Thread is a linear flow of execution ✘ Managed by a Scheduler 14 Runnable vs Callable vs Future def task() = new Thread(() => println(s"Hi Thread: ${Thread.currentThread.getName}")) task().start()
  • 15. Thread ✘ Thread is a linear flow of execution ✘ Managed by a Scheduler 15 Runnable vs Callable vs Future def task() = new Thread(() => println(s"Hi Thread: ${Thread.currentThread.getName}")) task().start() ✘ 1 Thread = 1 OS Thread
  • 16. Thread pool Execution ✘ Uses blocking queue 16 Runnable vs Callable vs Future
  • 17. Execution Service 17 Runnable vs Callable vs Future class ExecutorService { def submit(a: Runnable): Future[Unit] def submit[A](a: Callable[A]): Future[A] } val service: ExecutorService = Executors.newFixedThreadPool(2)
  • 18. Execution Service 18 Runnable vs Callable vs Future class ExecutorService { def submit(a: Runnable): Future[Unit] def submit[A](a: Callable[A]): Future[A] } val service: ExecutorService = Executors.newFixedThreadPool(2)
  • 19. Execution Service 19 Runnable vs Callable vs Future class ExecutorService { def submit(a: Runnable): Future[Unit] def submit[A](a: Callable[A]): Future[A] } val service: ExecutorService = Executors.newFixedThreadPool(2)
  • 20. Runnable 20 Runnable vs Callable vs Future trait Runnable { def run: Unit }
  • 21. Runnable ✘ Define tasks that extend Runnable 21 Runnable vs Callable vs Future
  • 22. Runnable ✘ Define tasks that extend Runnable 22 Runnable vs Callable vs Future val t1 = task() val t2 = task() val t3 = task()
  • 23. Runnable ✘ Define tasks that extend Runnable ✘ Submit the parallel tasks 23 Runnable vs Callable vs Future service.submit(t1) service.submit(t2) service.submit(t3)
  • 24. Runnable ✘ Define tasks that extend Runnable ✘ Submit the parallel tasks 24 Runnable vs Callable vs Future How could we get meaningful values from the separate Threads?
  • 25. Runnable 25 Runnable vs Callable vs Future trait Runnable { def run: Unit } ✘ Define tasks that extend Runnable ✘ Submit the parallel tasks How could we get meaningful values from the separate Threads?
  • 26. Runnable 26 Runnable vs Callable vs Future class ExecutorService { def submit(a: Runnable): Future[Unit] def submit[A](a: Callable[A]): Future[A] }
  • 27. Runnable 27 Runnable vs Callable vs Future class ExecutorService { def submit(a: Runnable): Future[Unit] def submit[A](a: Callable[A]): Future[A] }
  • 28. Callable 28 Runnable vs Callable vs Future trait Callable[A] { def call: A }
  • 29. Callable ✘ Define tasks that extend Callable 29 Runnable vs Callable vs Future def increment(i: Int) = new Callable[Int] { def call(): Int = i +1 }
  • 30. Callable ✘ Define tasks that extend Callable ✘ Submit the parallel tasks 30 Runnable vs Callable vs Future import java.util.concurrent.Future val v1: Future[Int] = service.submit(increment(1)) val v2: Future[Int] = service.submit(increment(2))
  • 31. Callable ✘ Define tasks that extend Callable ✘ Submit the parallel tasks 31 Runnable vs Callable vs Future import java.util.concurrent.Future val v1: Future[Int] = service.submit(increment(1)) val v2: Future[Int] = service.submit(increment(2)) How could we use the value of every Future?
  • 32. Callable ✘ Define tasks that extend Callable ✘ Submit the parallel tasks 32 Runnable vs Callable vs Future import java.util.concurrent.Future val v1: Future[Int] = service.submit(increment(1)) val v2: Future[Int] = service.submit(increment(2)) How could we use the value of every Future? trait Future[A] { def get: A }
  • 33. Callable ✘ Define tasks that extend Callable ✘ Submit the parallel tasks 33 Runnable vs Callable vs Future import java.util.concurrent.Future val v1: Future[Int] = service.submit(increment(1)) val v2: Future[Int] = service.submit(increment(2)) STATEMENTS
  • 34. ✘ Define futures Future 34 Runnable vs Callable vs Future def increment(i: Int) = Future.successful(i + 1) val f1: Future[Int] = increment(1) val f2: Future[Int] = increment(2) val result: Future[Int] = for { v1 <- f1 v2 <- f2 } yield v1 + v2
  • 35. ✘ Define futures Future 35 Runnable vs Callable vs Future def increment(i: Int) = Future.successful(i + 1) val f1: Future[Int] = increment(1) val f2: Future[Int] = increment(2) val result: Future[Int] = for { v1 <- f1 v2 <- f2 } yield v1 + v2 Cannot find an implicit ExecutionContext. You might pass [error] an (implicit ec: ExecutionContext) parameter to your method [error] or import scala.concurrent.ExecutionContext.Implicits.global. [error] v2 <- f2 [error] ^
  • 36. ✘ Define futures Future 36 Runnable vs Callable vs Future import scala.concurrent.ExecutionContext.Implicits.global def increment(i: Int) = Future.successful(i + 1) val f1: Future[Int] = increment(1) val f2: Future[Int] = increment(2) val result: Future[Int] = for { v1 <- f1 v2 <- f2 } yield v1 + v2
  • 37. 37 Pure Future
  • 38. 38 Future Pure Future ✘ Asynchronous ✘ Resumes when the value is ready!
  • 39. 39 Future Pure Future ✘ Asynchronous ✘ Resumes when the value is ready! trait Future[A] { def apply(cb: A => Unit): Unit }
  • 40. 40 Future Pure Future ✘ Asynchronous ✘ Resumes when the value is ready! trait Future[A] { private[api] def apply(cb: A => Unit): Unit }
  • 41. 41 The Data Type for parallel computations Pure Future ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable
  • 42. 42 The Data Type for parallel computations Pure Future type Par[A] = ExecutorService => Future[A] ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable
  • 43. 43 The Data Type for parallel computations Pure Future type Par[A] = ExecutorService => Future[A] ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable
  • 44. 44 The Data Type for parallel computations Pure Future type Par[A] = ExecutorService => Future[A] ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable
  • 45. 45 The Data Type for parallel computations Pure Future object Par { def unit[A](a: A): Par[A] = ??? } ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable
  • 46. 46 Par.unit Pure Future type Par[A] = ExecutorService => Future[A] object Par { def unit[A](a: A): Par[A] = (_: ExecutorService) => new Future[A] { def apply(cb: A => Unit): Unit = cb(a) } }
  • 47. 47 The Data Type for parallel computations Pure Future object Par { def fork[A](a: Par[A]): Par[A] = ??? } ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable
  • 48. 48 Par.fork Pure Future type Par[A] = ExecutorService => Future[A] object Par { def fork[A](a: Par[A]): Par[A] = es => new Future[A] { def apply(cb: A => Unit): Unit = es.submit(new Callable[Unit] { def call: Unit = a(es)(cb) // a(es).apply(cb) }) } }
  • 49. 49 The Data Type for parallel computations Pure Future object Par { def run[A](es: ExecutorService)(par: Par[A]): A = ??? } ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable
  • 50. 50 Par.run Pure Future type Par[A] = ExecutorService => Future[A] object Par { def run[A](es: ExecutorService)(p: Par[A]): A = { val ref = new AtomicReference[A] val latch = new CountDownLatch(1) p(es) { a => ref.set(a) latch.countDown() } latch.await() ref.get } }
  • 51. 51 Par.run Pure Future type Par[A] = ExecutorService => Future[A] object Par { def run[A](es: ExecutorService)(p: Par[A]): A = { val ref = new AtomicReference[A] val latch = new CountDownLatch(1) p(es) { a => ref.set(a) latch.countDown() } latch.await() ref.get } } val one = Par.unit(1) run(es)(one) ⇒ 1
  • 52. “Programming with mutability is like working with the mother-in-law who’s waiting you to fail” - Venkat Subramaniam 52
  • 53. object Par { def map[A, B](a: Par[A])(f: A => B): Par[B] def flatMap[A, B](a: Par[A])(f: A => Par[B]): Par[B] def parMap[A, B](l: List[A])(f: A => B): Par[List[B]] def map2[A, B, C](a: Par[A], b: Par[B])(f: (A, B) => C): Par[C] ... } 53 The Data Type for parallel computations Pure Future ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable
  • 54. 54 The Data Type for parallel computations Pure Future ✘ Requires ExecutorService ✘ Asynchronously performs a given computation of any type ✘ Contains a result of the computation ✘ Composable object Par { def map[A, B](a: Par[A])(f: A => B): Par[B] def flatMap[A, B](a: Par[A])(f: A => Par[B]): Par[B] def parMap[A, B](l: List[A])(f: A => B): Par[List[B]] def map2[A, B, C](a: Par[A], b: Par[B])(f: (A, B) => C): Par[C] ... }
  • 55. Example Pure Future 1 2 3 4 5 6 7 8 9
  • 56. 56 Example Pure Future 1 2 3 4 5 6 7 8 9 def sum(ints: IndexedSeq[Int]): Par[Int] = if (ints.size <= 1) Par.unit(ints.headOption getOrElse 0) else { val (l, r) = ints.splitAt(ints.size / 2) Par.fork(Par.map2(sum(l), sum(r))(_ + _)) }
  • 57. FP Scala libraries ZIO: zio.dev Cats-effect: typelevel.org/cats-effect Monix: monix.io 57 Pure Future
  • 58. References ✘ Functional programming in Scala Book ✘ https://medium.com/@wiemzin/purely-functional-parallelism-in-scala-37ecb1e9999 ✘ https://github.com/fpinscala/fpinscala/blob/master/answers/src/main/scala/fpinscala/paralle lism/Nonblocking.scala 58
  • 59. THANKS! You can find me at @WiemZin @wi101 59
  • We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks
    SAVE OUR EARTH

    We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

    More details...

    Sign Now!

    We are very appreciated for your Prompt Action!

    x