Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Back to the [Completable] Future

This presentation should give you an insight on how to work with Java 8's and 9's CompletableFuture class.

  • Identifiez-vous pour voir les commentaires

Back to the [Completable] Future

  1. 1. Back to the [Completable] Future
  2. 2. Intro { "name":"Sophia", "job":"Java developer", "employer":"N-iX", "experience":"2+ years" } Image URL: https://cdn-images-1.medium.com/max/800/1*oZA7pTY3OqNYGBO9xcuIJQ.jpeg
  3. 3. Agenda
  4. 4. Why now?1
  5. 5. Why now? Era of… Image URLs: http://img.clipartall.com/free-mini-images-arts-clip-art-mixing-bowl-clipart-648_603.gif Mash-up apps
  6. 6. What for?2
  7. 7. What for? Speed up the application... Image URL: https://www.thewebmaster.com/media/uploads/files/why-carry-out-a-speed-test-on-website_UczLvGZ.jpg
  8. 8. What for? ... in a simpler way than working directly with threads Image URL: http://static.codeceo.com/images/2015/01/33095007e134e8630fe35771795f3ac8.jpg
  9. 9. So, we need to do asynchronous programming …
  10. 10. Right
  11. 11. Speeding up3
  12. 12. Speeding up URLs: http://www.wanicreative.com/wp-Image content/uploads/2015/04/HiRes-1200x800.jpg;http://www.effevicomunicazione.it/sitenew/wp-content/uploads/2015/08/hairglam1.jpg Concurrency Parallelismvs.
  13. 13. Concurrency Multiple tasks make progress at the same time Time CPU
  14. 14. Concurrency Time In concurrency, tasks do compete for processor time
  15. 15. Parallelism Each task is broken into sub tasks which can be processed in parallel CPU 1 CPU 2 Time
  16. 16. Parallelism Time In parallelism, tasks do not compete for processor time
  17. 17. Variations Image URLs: https://graphicpolicy.files.wordpress.com/2014/11/back-to-the-portal.jpg?w=350&h=200&crop=1; https://cache.popcultcha.com.au/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/d/e/delorean- back-to-future-hove-time-amachine-electronic-vehicle-replica-14-dstfeb172621.png
  18. 18. Concurrency is an illusion of parallelism, don‘t you agree?
  19. 19. Sync vs. Async4
  20. 20. Sync vs. Async Asynchronous Synchronous
  21. 21. Well, it can be Is asynchronous any faster?
  22. 22. Sync vs. Async The advantage is obvious, isn't it? Image URL: https://image.slidesharecdn.com/javainhft-141222112349-conversion-gate02/95/java-in-high-frequency-trading-23-638.jpg?cb=1419247883, http://www.pngall.com/wp-content/uploads/2016/04/Red-Cross-Mark-Transparent.png
  23. 23. Sync vs. Async Make your calls non-blocking! Image URL: https://s-media-cache-ak0.pinimg.com/600x315/09/60/a3/0960a3cf49ff2c1a23e8b67997b17be6.jpg
  24. 24. So, what about Java?
  25. 25. Java retirement party5
  26. 26. Java retirement party How it used to be… Image URL: https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTAc9Up87H315tKinSkf8EZ8VfoR46gZfDGR2SjTCG_yOwAJvJ8MA • Threads • Future<T> • ForkJoinPool • Streams (parallel)
  27. 27. Threads Thread 1 Thread 2 Object 1 Object 2 Image URL: https://s-media-cache-ak0.pinimg.com/originals/09/1b/f8/091bf88ab19547360376dc24014addfd.gif;https://cdn4.iconfinder.com/data/icons/meBaze-Freebies/512/lock.png
  28. 28. Threads
  29. 29. Threads Image URL: https://avaldes.com/wp-content/uploads/2013/03/multithreading_starvation.png?2d262d, http://blog.takipi.com/wp-content/uploads/2014/05/Blog_key_.png. http://blog.takipi.com/wp-content/uploads/2013/08/blog_trampoline.png
  30. 30. Callables & Future<T> Image URL: http://www.deadcoderising.com/content/images/2015/Jan/timeout2.png
  31. 31. ForkJoinPool
  32. 32. Parallel streams Splitting Parallel processing Combining
  33. 33. 6 Image URL: https://purelyconsumed.files.wordpress.com/2013/07/embrace-imperfection.jpg
  34. 34. More… ◦ Chain callbacks ◦ Wait for completion ◦ Retrieve the value ◦ Complete a Future ◦ React & Recover
  35. 35. CF: let's get acquainted!7
  36. 36. CompletableFuture ◦ java.util.concurrent Image URL: http://cdn.ishaya.ir/ponila_bucket/images/00/17/00175721ebb9add9498d86bda11e0ec0/736x/00175721ebb9add9498d86bda11e0ec0.jpg ◦ implements Future<T> ◦ implements CompletionStage<T>
  37. 37. CompletableFuture Image URL: http://wallpaperscraft.ru/image/odin_doma_makoley_kalkin_macaulay_culkin_malchik_kevin_krik_strah_ispug_home_alone_346_2732x2732.jpg
  38. 38. Concept Brings the concept of promises to Java. Image URL: https://pbs.twimg.com/media/CzTp1YBW8AEFAMI.jpg
  39. 39. API8 Image URL: https://www.amisw.com/en/wp-content/uploads/2015/08/image-demo-b.png
  40. 40. API: Simplest asynchronous computation ◦ Pitfall #1: Specify custom executor to avoid using default common pool. Image URL: http://cdn.ishaya.ir/ponila_bucket/images/00/17/00175721ebb9add9498d86bda11e0ec0/736x/00175721ebb9add9498d86bda11e0ec0.jpg
  41. 41. API Image URL: http://www.sqlhammer.com/wp-content/uploads/2016/10/demo-god-meme.jpg
  42. 42. API: Chaining callbacks ◦ Pitfall #2: Use thenCompose instead of thenApply to avoid the nestings. Image URL: http://cdn.ishaya.ir/ponila_bucket/images/00/17/00175721ebb9add9498d86bda11e0ec0/736x/00175721ebb9add9498d86bda11e0ec0.jpg
  43. 43. API Image URL: http://bladerunnerjs.org/blog/img/demo-time.jpg
  44. 44. API: Parallel computations ◦ Pitfall #3: Keep in mind that the order of execution is not guaranteed. Image URL: http://cdn.ishaya.ir/ponila_bucket/images/00/17/00175721ebb9add9498d86bda11e0ec0/736x/00175721ebb9add9498d86bda11e0ec0.jpg
  45. 45. API Image URL: http://i.memecaptain.com/gend_images/PssWXg.jpg
  46. 46. API: Dealing with many CompletableFuture instances ◦ Pitfall #4: We get CompletableFuture<Void> as the result of execution of all futures. Image URL: http://cdn.ishaya.ir/ponila_bucket/images/00/17/00175721ebb9add9498d86bda11e0ec0/736x/00175721ebb9add9498d86bda11e0ec0.jpg
  47. 47. API Image URL: https://cdn.meme.am/instances/500x/61002060/grumpy-cat-demo-time.jpg
  48. 48. API: Dealing with many CompletableFuture instances ◦ Pitfall #5: We get CompletableFuture<Object> as the result of execution of the fastest future. Image URL: http://cdn.ishaya.ir/ponila_bucket/images/00/17/00175721ebb9add9498d86bda11e0ec0/736x/00175721ebb9add9498d86bda11e0ec0.jpg
  49. 49. API Image URL: http://www.memegen.com/m/kwfxrc.jpg
  50. 50. API: Exceptions ◦ Pitfall #6: Exceptions can not be handled with usual CompletableFuture methods. Image URL: http://cdn.ishaya.ir/ponila_bucket/images/00/17/00175721ebb9add9498d86bda11e0ec0/736x/00175721ebb9add9498d86bda11e0ec0.jpg
  51. 51. Java 9 concurrency improvements 9
  52. 52. Java 9 concurrency improvements JEP 266: More Concurrency Updates 8 new methods; 5 new static methods. Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  53. 53. Java 9 concurrency improvements: API Additions orTimeout(long timeout, TimeUnit unit) Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  54. 54. Java 9 concurrency improvements: API Additions completeOnTimeout(T value, long timeout, TimeUnit unit) Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  55. 55. Java 9 concurrency improvements: API Additions completeAsync(Supplier<? extends T> supplier, Executor executor) Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  56. 56. Java 9 concurrency improvements: API Additions completeAsync(Supplier<? extends T> supplier) Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  57. 57. Java 9 concurrency improvements: API Additions defaultExecutor() Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  58. 58. Java 9 concurrency improvements: API Additions newIncompleteFuture() Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  59. 59. Java 9 concurrency improvements: API Additions copy() Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  60. 60. Java 9 concurrency improvements: API Additions minimalCompletionStage() Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  61. 61. Static API additions10
  62. 62. Java 9 concurrency improvements: Static API Additions ◦ Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) ◦ Executor delayedExecutor(long delay, TimeUnit unit) Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  63. 63. Java 9 concurrency improvements: Static API Additions ◦ <U> CompletionStage<U> completedStage(U value) ◦ <U> CompletionStage<U> failedStage(Throwable ex) ◦ <U> CompletableFuture<U> failedFuture(Throwable ex) Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
  64. 64. Takeaways11
  65. 65. Takeaways understand the input; avoid the pitfalls; use Java 9 before the release!
  66. 66. Q&A12
  67. 67. Thanks! Find me at sophia.khomyn@gmail.com https://github.com/KhomynSophia/completable-future COMPLETABLEFUTURE OVERVIEW http://javaday.org.ua/completablefuture-overview/
  68. 68. CREDITS Special thanks to all the people who made and released these awesome resources for free: ◦ Presentation template by SlidesCarnival

×