Back to the
[Completable] Future
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
Agenda
Why now?1
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
What for?2
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
What for?
... in a simpler way than working directly
with threads
Image URL: http://static.codeceo.com/images/2015/01/33095007e134e8630fe35771795f3ac8.jpg
So, we need to do
asynchronous
programming …
Right
Speeding up3
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.
Concurrency
Multiple tasks
make progress at
the same time
Time
CPU
Concurrency
Time
In concurrency,
tasks do
compete for
processor time
Parallelism
Each task is
broken into sub
tasks which can
be processed in
parallel
CPU 1 CPU 2
Time
Parallelism
Time
In parallelism,
tasks do not
compete for
processor time
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
Concurrency is an
illusion of parallelism,
don‘t you agree?
Sync vs. Async4
Sync vs. Async
Asynchronous
Synchronous
Well, it can be
Is asynchronous
any faster?
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
Sync vs. Async
Make your calls non-blocking!
Image URL: https://s-media-cache-ak0.pinimg.com/600x315/09/60/a3/0960a3cf49ff2c1a23e8b67997b17be6.jpg
So, what about Java?
Java retirement party5
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)
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
Threads
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
Callables & Future<T>
Image URL: http://www.deadcoderising.com/content/images/2015/Jan/timeout2.png
ForkJoinPool
Parallel streams
Splitting Parallel processing Combining
6
Image URL: https://purelyconsumed.files.wordpress.com/2013/07/embrace-imperfection.jpg
More…
◦ Chain callbacks
◦ Wait for completion
◦ Retrieve the value
◦ Complete a Future
◦ React & Recover
CF: let's get acquainted!7
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>
CompletableFuture
Image URL: http://wallpaperscraft.ru/image/odin_doma_makoley_kalkin_macaulay_culkin_malchik_kevin_krik_strah_ispug_home_alone_346_2732x2732.jpg
Concept
Brings the concept of promises to Java.
Image URL: https://pbs.twimg.com/media/CzTp1YBW8AEFAMI.jpg
API8
Image URL: https://www.amisw.com/en/wp-content/uploads/2015/08/image-demo-b.png
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
API
Image URL: http://www.sqlhammer.com/wp-content/uploads/2016/10/demo-god-meme.jpg
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
API
Image URL: http://bladerunnerjs.org/blog/img/demo-time.jpg
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
API
Image URL: http://i.memecaptain.com/gend_images/PssWXg.jpg
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
API
Image URL: https://cdn.meme.am/instances/500x/61002060/grumpy-cat-demo-time.jpg
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
API
Image URL: http://www.memegen.com/m/kwfxrc.jpg
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
Java 9 concurrency
improvements
9
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
Java 9 concurrency improvements: API Additions
orTimeout(long timeout,
TimeUnit unit)
Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
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
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
Java 9 concurrency improvements: API Additions
completeAsync(Supplier<?
extends T> supplier)
Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
Java 9 concurrency improvements: API Additions
defaultExecutor()
Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
Java 9 concurrency improvements: API Additions
newIncompleteFuture()
Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
Java 9 concurrency improvements: API Additions
copy()
Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
Java 9 concurrency improvements: API Additions
minimalCompletionStage()
Image URL: https://ilaborie.github.io/devfest-toulouse-16-slides/pictures/Java9.png
Static API additions10
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
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
Takeaways11
Takeaways
understand the input;
avoid the pitfalls;
use Java 9 before the release!
Q&A12
Thanks!
Find me at
sophia.khomyn@gmail.com
https://github.com/KhomynSophia/completable-future
COMPLETABLEFUTURE OVERVIEW
http://javaday.org.ua/completablefuture-overview/
CREDITS
Special thanks to all the people who made and
released these awesome resources for free:
◦ Presentation template by SlidesCarnival

Back to the [Completable] Future