Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Flying Futures at the same sky can make the sun rise at midnight

241 views

Published on

Template made by Slidesgo
Implementing responsive and high-performance applications is the most obvious challenge that we face in our programming life. It’s interesting to deeply study concurrency and parallelism on the JVM. In this talk you will learn how to describe parallel tasks and the idea behind Futures and the execution context. I will cover the tricky part of concurrency when the concurrent tasks share and use the same resources and how flying Futures in the same sky can make the sun rise at midnight! At the end I will talk about some possible solutions that you can use to reduce your worries about the pitfalls of concurrency.

Published in: Software
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Flying Futures at the same sky can make the sun rise at midnight

  1. 1. Flying Futuresat thesameskycan makethesunriseatmidnight! Wiem Zine Elabidine
  2. 2. ...The Journey... Parallelism &Concurrency Threads andFutures Concurrency problems Solutions
  3. 3. Parallelism&Concurrency01
  4. 4. Parallelism “Parallel computing is a form of computation in which many calculations are carried out simultaneously” - Wikipedia
  5. 5. Concurrency “Concurrency refers to the ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or in partial order, without affecting the final outcome.” - Wikipedia
  6. 6. 4Cores Parallelism Concurrency
  7. 7. 4Cores Parallelism Concurrency
  8. 8. 1Core Parallelism Concurrency
  9. 9. ThreadsandFutures02Describe parallel computations
  10. 10. Thread A linear flow of execution managed by a Scheduler. def task(i: Int) = new Thread{() => println(s"Instruction 1: Task $i") println(s"Instruction 2: Task $i") } task(1).start() task(2).start()
  11. 11. Thread A linear flow of execution managed by a Scheduler. > run instruction 1: Task 1 instruction 1: Task 2 instruction 2: Task 1 instruction 2: Task 2
  12. 12. Thread A linear flow of execution managed by a Scheduler. > run instruction 1: Task 1 instruction 1: Task 2 instruction 2: Task 2 instruction 2: Task 1
  13. 13. Thread A linear flow of execution managed by a Scheduler. > run instruction 1: Task 2 instruction 2: Task 2 instruction 1: Task 1 instruction 2: Task 1
  14. 14. Depends to the number of the instructions and the number of threads. Executionorder
  15. 15. Possibleexec Paths Depends to the number of the instructions and the number of threads. Executionorder
  16. 16. PossibleexecutionPaths - 2 Instructions - 2 Threads - 24/4 = 6 ⇒ 6 Possible results def task(i: Int) = new Thread{() => println(s"Instruction 1: Task $i") println(s"Instruction 2: Task $i") } task(1).start() task(2).start()
  17. 17. 1Thread=1OSThread
  18. 18. 1Thread=1OSThread Runtime.getRuntime.availableProcessors
  19. 19. ThreadPool Task1 Task2 Task3 Task4 ...Tasks Queue Thread Pool
  20. 20. ThreadPoolExecutor . Helps to save resources . Control the number of the created threads in the application. def task(i: Int) = new Thread{() => println(s"Iteration 1: Task $i") println(s"Iteration 2: Task $i") } val service: ExecutorService = Executors.newFixedThreadPool(2) (1 to 1000).foreach(i => service.submit(task(i)))
  21. 21. def task(i: Int) = new Thread{() => println(s"Iteration 1: Task $i") println(s"Iteration 2: Task $i") } val service: ExecutorService = Executors.newFixedThreadPool(2) (1 to 1000).foreach(i => service.submit(task(i))) ThreadPoolExecutor . Helps to save resources . Control the number of the created threads in the application.
  22. 22. def task(i: Int): Future[Unit] = Future{ println(s"Iteration 1: Task $i") println(s"Iteration 2: Task $i") } (1 to 1000).foreach(task) ScalaFuture Describe a non blocking computation.
  23. 23. def task(i: Int): Future[Unit] = Future{ println(s"Iteration 1: Task $i") println(s"Iteration 2: Task $i") } (1 to 1000).foreach(task) ScalaFuture Describe a non blocking computation.
  24. 24. The performance of the program depends to your CPU
  25. 25. The performance of the program depends to your CPU implicit val es = ExecutionContext.fromExecutor( Executors.newFixedThreadPool(2))
  26. 26. Futures Composabledef deleteProfile(userId: UserId) = for { d1 <- deleteUser(userId) d2 <- deletePicture(userId) d3 <- deleteEducation(userId) } yield d1 && d2 && d3
  27. 27. def deleteProfile(userId: UserId) = { val f1 = users.deleteUser(userId) val f2 = users.deletePicture(userId) val f3 = users.deleteEducation(userId) users.exists(userId).flatMap { check => if(check) for { _ <- f1 _ <- f2 _ <- f3 } yield () } } FlyingFutures
  28. 28. def deleteProfile(userId: UserId) = { users.exists(userId).flatMap { check => if(check) for { _ <- users.deleteUser(userId) _ <- users.deletePicture(userId) _ <- users.deleteEducation(userId) } yield () } } FlyingFutures
  29. 29. Await.result lazy val flyingFuture = Future { Thread.sleep(2000) println( "after 2 seconds I am still flying!" )} Await.result(flyingFuture, 1.second)
  30. 30. TimeOutException
  31. 31. TimeOutException
  32. 32. TimeOutExceptionwillnotstoptheflyingFuture!
  33. 33. Not referential transparent
  34. 34. Futures aren’treferentialtransparent def deleteProfile(userId: UserId) = for { d1 <- deleteUser(userId) d2 <- deletePicture(userId) d3 <- deleteEducation(userId) } yield d1 && d2 && d3 def deleteProfile(userId: UserId) = { val f1 = deleteUser(userId) val f2 = deletePicture(userId) val f3 = deleteEducation(userId) for { d1 <- f1 d2 <- f2 d3 <- f3 } yield d1 && d2 && d3 }
  35. 35. LazyFutures don’tfly def deleteProfile(userId: UserId) = for { d1 <- deleteUser(userId) d2 <- deletePicture(userId) d3 <- deleteEducation(userId) } yield d1 && d2 && d3 def deleteProfile(userId: UserId) = { lazy val f1 = deleteUser(userId) lazy val f2 = deletePicture(userId) lazy val f3 = deleteEducation(userId) for { d1 <- f1 d2 <- f2 d3 <- f3 } yield d1 && d2 && d3 }
  36. 36. No Future, No fly
  37. 37. ConcurrencyProblems
  38. 38. case class State( sunState: Sun.State, moonState: Moon.State, earthState: Earth.State )
  39. 39. val changeSunState: Future[State] = computeSunState.map { v => state = state.copy(sunState = v) } val changeMoonState: Future[State] = computeMoonState.map { v => state = state.copy(moonState = v) } val changeEarthState: Future[State] = computeEarthState.map { v => state = state.copy(earthState = v) } case class State( sunState: Sun.State, moonState: Moon.State, earthState: Earth.State )
  40. 40. The devil is in mutable state
  41. 41. MemoryArchitecture: 4Cores Core 1 Core 2 Core 3 Core 4 registers registers registers registers LC LC LC LC Main memory
  42. 42. MutableState Sun Earth Moon 23:58 Core 1 Core 2 Core 3 registers registers registers LC LC LC Main memory state state state state
  43. 43. MutableState Sun Earth Moon 23:58 Core 1 Core 2 Core 3 registers registers registers LC LC LC Main memory state state state state
  44. 44. MutableState Sun Earth Moon 23:58 Core 1 Core 2 Core 3 registers registers registers LC LC LC Main memory state state state state
  45. 45. Raceconditions
  46. 46. MutableState Sun Earth Moon 23:58 00:00 Core 1 Core 2 Core 3 registers registers registers LC LC LC Main memory state state state state
  47. 47. Flying Futuresinthesameskycan makethesunriseatmidnight
  48. 48. Solutions
  49. 49. Solutions - For the visibility problem: ● volatile
  50. 50. Solutions - For the visibility problem: ● volatile - For the synchronization problem: ● synchronized ● AtomicReference ● CountDownLatch ● Locks ● Semaphores
  51. 51. Control the interactions with the outside World Functionaleffects
  52. 52. DatatypesforParallel computation IO[E,A] Models Sync, Async effects Fiber[E,A] Models a running IO value
  53. 53. DatatypesforParallel computation IO[E,A] Models Sync, Async effects Fiber[E,A] Models a running IO value val f: IO[Nothing, Fiber[Nothing, Unit] = io.fork val io: IO[Nothing, Unit] = IO.effectTotal(println("Hello"))
  54. 54. ZIOisreferentialtransparent def deleteProfile(userId: UserId) = for { d1 <- deleteUser(userId) d2 <- deletePicture(userId) d3 <- deleteEducation(userId) } yield () def deleteProfile(userId: UserId) = { val f1 = deleteUser(userId) val f2 = deletePicture(userId) val f3 = deleteEducation(userId) for { d1 <- f1 d2 <- f2 d3 <- f3 } yield () }
  55. 55. ZIOisreferentialtransparent def deleteProfile(userId: UserId) = for { d1 <- deleteUser(userId).fork d2 <- deletePicture(userId).fork d3 <- deleteEducation(userId).fork } yield () def deleteProfile(userId: UserId) = { val f1 = deleteUser(userId).fork val f2 = deletePicture(userId).fork val f3 = deleteEducation(userId).fork for { d1 <- f1 d2 <- f2 d3 <- f3 } yield () }
  56. 56. def deleteProfile(userId: UserId) = ZIO.collectAllPar( deleteUser(userId) :: deletePicture(userId) :: deleteEducation(userId) :: Nil) Paralleltasks
  57. 57. ZIOSTM Provides the ability to atomically commit a series of reads and writes to transactional memory when a set of conditions is satisfied. STM[E,A] Describes a transaction Describes the transactional memory that will be read and written inside the transaction TRef[A]
  58. 58. Example def changeSunState(tState: TRef[State]): STM[Nothing, State] = computeSunState.map { v => state = tState.update(_.copy(sunState = v)) }
  59. 59. def changeSunState(tState: TRef[State]) = computeSunState.map { v => state = tState.update(_.copy(sunState = v)) } def changeMoonState(tState: TRef[State]) = computeMoonState.map { v => state = tState.update(_.copy(moonState = v)) } def changeEarthState(tState: TRef[State])= computeEarthState.map { v => state = tState.update(_.copy(earthState = v)) } Transaction 1 Transaction 2 Transaction 3 tState
  60. 60. Example def changeSunState(tState: TRef[State]) = computeSunState.map { v => state = tState.update(_.copy(sunState = v)) } def changeMoonState(tState: TRef[State]) = computeMoonState.map { v => state = tState.update(_.copy(moonState = v)) } def changeEarthState(tState: TRef[State])= computeEarthState.map { v => state = tState.update(_.copy(earthState = v)) } object SolarSystem { def make(initialState: State) = for { tstate <- TRef.make(initialState).commit _ <- changeSunState(tstate).commit.fork _ <- changeMoonState(tstate).commit.fork _ <- changeEarthState(tstate).commit.fork } yield () }
  61. 61. Execution Sun Earth Moon def changeSunState(tState: TRef[State]) = computeSunState.map { v => state = tState.update(_.copy(sunState = v)) } def changeMoonState(tState: TRef[State]) = computeMoonState.map { v => state = tState.update(_.copy(moonState = v)) } def changeEarthState(tState: TRef[State])= computeEarthState.map { v => state = tState.update(_.copy(earthState = v)) }
  62. 62. Execution Sun Earth Moon def changeSunState(tState: TRef[State]) = computeSunState.map { v => state = tState.update(_.copy(sunState = v)) } def changeMoonState(tState: TRef[State]) = computeMoonState.map { v => state = tState.update(_.copy(moonState = v)) } def changeEarthState(tState: TRef[State])= computeEarthState.map { v => state = tState.update(_.copy(earthState = v)) }
  63. 63. Execution Sun Earth Moon def changeSunState(tState: TRef[State]) = computeSunState.map { v => state = tState.update(_.copy(sunState = v)) } def changeMoonState(tState: TRef[State]) = computeMoonState.map { v => state = tState.update(_.copy(moonState = v)) } def changeEarthState(tState: TRef[State])= computeEarthState.map { v => state = tState.update(_.copy(earthState = v)) }
  64. 64. Example def changeSunState(tState: TRef[State]) = computeSunState.map { v => state = tState.update(_.copy(sunState = v)) } def changeMoonState(tState: TRef[State]) = computeMoonState.map { v => state = tState.update(_.copy(moonState = v)) } def changeEarthState(tState: TRef[State])= computeEarthState.map { v => state = tState.update(_.copy(earthState = v)) } object SolarSystem { def make(initialState: State) = for { tstate <- TRef.makeCommit(initialState) _ <- changeSunState(tstate).commit.fork _ <- changeMoonState(tstate).commit.fork _ <- changeEarthState(tstate).commit.fork } yield () }
  65. 65. Concurrencyistrickybutitmakes yourapplicationresponsiveand faster
  66. 66. Useful links Concurrency is complicated ★ https://medium.com/@wiemzin ★ ZIO-STM: https://github.com/zio/zio ★ Akka Actor ★ Upgrade your Future by John De Goes ★ Functional programminglibraries: ZIO, Cats effects, Monix ★ Presentation template by Slidesgo
  67. 67. Wiem Zine Elabidine twitter: @WiemZin github: wi101 THANKS

×