SlideShare a Scribd company logo
albertlr.ro #BTechWeek
Asynchronous
CompletableFuture
László-Róbert Albert
May 18th, Bucharest
albertlr.ro #BTechWeek
albertlr.ro #BTechWeek
Asynchronous
CompletableFuture
László-Róbert Albert
May 18th, Bucharest
albertlr.ro #BTechWeek
albertlr.ro #BTechWeek
About me
Chief Software Architect @ Crossover

10+ years in Java

Speaker

MsC in Computer Science

Proud father of 3
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• @since Java 8
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• @since Java 8

• not used in Java 8
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• @since Java 8

• not used in Java 8
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• Usage in Java 9
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• Usage in Java 9

• Process API

• HttpClient
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
...
}
albertlr.ro #BTechWeek
j.u.c.Future
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
albertlr.ro #BTechWeek
j.u.c.CompletionStage
public interface CompletionStage<T> {
public <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn);
public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn);
public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor);
public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor);
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action, Executor executor);
public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor);
public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor);
public CompletionStage<Void> runAfterBoth(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor);
public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor);
public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor);
public CompletionStage<Void> runAfterEither(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor);
public <U> CompletionStage<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn);
public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn);
public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor);
public CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn);
public CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable> action);
public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action);
public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor);
public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor);
public CompletableFuture<T> toCompletableFuture();
}
albertlr.ro #BTechWeek
j.u.c.CompletionStage
public interface CompletionStage<T> {
public <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn);
public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn);
public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor);
public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor);
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action, Executor executor);
public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor);
public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor);
public CompletionStage<Void> runAfterBoth(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor);
public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor);
public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor);
public CompletionStage<Void> runAfterEither(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor);
public <U> CompletionStage<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn);
public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn);
public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor);
public CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn);
public CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable> action);
public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action);
public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor);
public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor);
public CompletableFuture<T> toCompletableFuture();
}
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• Contains 38 methods
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• Contains 38 methods

• 36 of them has 3 forms
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• Contains 38 methods

• 36 of them has 3 forms:

public CompletionStage<?> somethingAsync(..., Executor executor);
public CompletionStage<?> somethingAsync(...);
public CompletionStage<?> something(...);
albertlr.ro #BTechWeek
j.u.c.CompletionStage
public CompletionStage<?> somethingAsync(..., Executor executor);
• runs action chain in executor

public CompletionStage<?> somethingAsync(...);
• Same as somethingAsync(..., ForkJoinPool.commonPool())

public CompletionStage<?> something(...);
• default execution
albertlr.ro #BTechWeek
j.u.c.CompletionStage
public CompletionStage<?> somethingAsync(..., Executor executor);
• runs action chain in executor

public CompletionStage<?> somethingAsync(...);
• Same as somethingAsync(..., ForkJoinPool.commonPool())

public CompletionStage<?> something(...);
• default execution
albertlr.ro #BTechWeek
j.u.c.CompletionStage
public CompletionStage<?> somethingAsync(..., Executor executor);
• runs action chain in executor

public CompletionStage<?> somethingAsync(...);
• Same as somethingAsync(..., ForkJoinPool.commonPool())

public CompletionStage<?> something(...);
• default execution
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• 12 methods remain
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• 12 methods remain

• 9 of them has 3 forms
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• 12 methods remain

• 9 of them has 3 forms:

Apply - function from input to R, result is a CompletableFuture<R>
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• 12 methods remain

• 9 of them has 3 forms:

Apply - function from input to R, result is a CompletableFuture<R>

Accept - consumer of input, result is a CompletableFuture<Void>
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• 12 methods remain

• 9 of them has 3 forms:

Apply - function from input to R, result is a CompletableFuture<R>

Accept - consumer of input, result is a CompletableFuture<Void>
Run - just execute a Runnable, result is a CompletableFuture<Void>
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• single input

thenApply, thenAccept, thenRun
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• single input

thenApply, thenAccept, thenRun
• binary <<or>>

applyToEither, acceptEither, runAfterEither
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• single input

thenApply, thenAccept, thenRun
• binary <<or>>

applyToEither, acceptEither, runAfterEither
• binary <<and>>

thenCombine, thenAcceptBoth, runAfterBoth
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• 3 methods remained:

thenCompose
handle
whenComplete
albertlr.ro #BTechWeek
j.u.c.CompletionStage
thenCompose
• Function from input to CompletableFuture<R>, result
CompletableFuture<R>

• a.k.a flatMap
albertlr.ro #BTechWeek
j.u.c.CompletionStage
handle
• Function from input and exception to R, result CompletableFuture<R>
albertlr.ro #BTechWeek
j.u.c.CompletionStage
whenComplete
• Consumer from input and exception

• Similar to Accept methods above

• Result - the same as input
albertlr.ro #BTechWeek
j.u.c.CompletionStage
• 2 methods remained (doesn't have async versions):

public CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn);
public CompletableFuture<T> toCompletableFuture();
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
public CompletableFuture() { .. }
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) { .. }
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) { .. }
public static <U> CompletableFuture<U> completedFuture(U value) { .. }
public static CompletableFuture<Void> runAsync(Runnable runnable) { .. }
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) { .. }
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) { .. }
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { .. }
public boolean cancel(boolean mayInterruptIfRunning) { .. }
public boolean complete(T value) { .. }
public boolean completeExceptionally(Throwable ex) { .. }
public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn) { .. }
public T get() throws InterruptedException, ExecutionException { .. }
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { .. }
public T getNow(T valueIfAbsent) { .. }
public int getNumberOfDependents() { .. }
public boolean isCancelled() { .. }
public boolean isCompletedExceptionally() { .. }
public boolean isDone() { .. }
public T join() { .. }
public void obtrudeException(Throwable ex) { .. }
public void obtrudeValue(T value) { .. }
...
}
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
public CompletableFuture() { .. }
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) { .. }
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) { .. }
public static <U> CompletableFuture<U> completedFuture(U value) { .. }
public static CompletableFuture<Void> runAsync(Runnable runnable) { .. }
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) { .. }
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) { .. }
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { .. }
public boolean cancel(boolean mayInterruptIfRunning) { .. }
public boolean complete(T value) { .. }
public boolean completeExceptionally(Throwable ex) { .. }
public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn) { .. }
public T get() throws InterruptedException, ExecutionException { .. }
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { .. }
public T getNow(T valueIfAbsent) { .. }
public int getNumberOfDependents() { .. }
public boolean isCancelled() { .. }
public boolean isCompletedExceptionally() { .. }
public boolean isDone() { .. }
public T join() { .. }
public void obtrudeException(Throwable ex) { .. }
public void obtrudeValue(T value) { .. }
...
}
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• Contains 38 methods inherited from CompletionStage
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• Contains 38 methods inherited from CompletionStage

and
• 14 other instance methods

• 7 static methods
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• 6 ways to complete future

complete/completeAsync/completeExceptionally

cancel
obtrudeValue/obtrudeException
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• 4 ways to get value

get/join - blocking

get(timeout, timeUnit) - not so blocking
getNow(valueIfAbsent) - non-blocking
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• 3 ways to know status

isDone

isCompletedExceptionally
isCancelled
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• 5 static methods to create future

completedFuture
runAsync(Runnable, Executor) -> CompletableFuture<Void>
supplyAsync(Supplier<U>, Executor) -> CompletableFuture<U>
albertlr.ro #BTechWeek
j.u.c.CompletableFuture
• 2 static methods to chain futures

allOf(CompletableFuture<?>...) -> CompletableFuture<Void>
anyOf(CompletableFuture<?>...) -> CompletableFuture<Object>
albertlr.ro #BTechWeek
Blocking or asynchronous?
albertlr.ro #BTechWeek
Blocking or asynchronous?
• Blocking

R doSomething(...);
• Asynchronous:

CompletionStage<R> doSomethingAsync(..., Executor executor);
CompletionStage<R> doSomethingAsync(...);
albertlr.ro #BTechWeek
Blocking or asynchronous?
R doSmth(...) {
return doSmthAsync(...).join();
}
albertlr.ro #BTechWeek
Blocking or asynchronous?
R doSmth(...) {
return doSmthAsync(...).join();
}
albertlr.ro #BTechWeek
Blocking or asynchronous?
albertlr.ro #BTechWeek
Let's measure
albertlr.ro #BTechWeek
Reference
• JavaDoc & the JDK

• https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/
CompletableFuture.html 

• HttpClient - http://openjdk.java.net/groups/net/httpclient/intro.html

• https://docs.oracle.com/javase/9/docs/api/jdk/incubator/http/
HttpClient.html 

• Process API - http://openjdk.java.net/jeps/102 

• practice 😀
albertlr.ro #BTechWeek
Asynchronous CompletableFuture
László-Róbert Albert
May 18th, Bucharest
albertlr.ro #BTechWeek
Thank you

More Related Content

What's hot

Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jscacois
 
Callbacks and control flow in Node js
Callbacks and control flow in Node jsCallbacks and control flow in Node js
Callbacks and control flow in Node jsThomas Roch
 
Boom! Promises/A+ Was Born
Boom! Promises/A+ Was BornBoom! Promises/A+ Was Born
Boom! Promises/A+ Was BornDomenic Denicola
 
Asynchronní programování
Asynchronní programováníAsynchronní programování
Asynchronní programováníPeckaDesign.cz
 
Redux Sagas - React Alicante
Redux Sagas - React AlicanteRedux Sagas - React Alicante
Redux Sagas - React AlicanteIgnacio Martín
 
Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...DroidConTLV
 
JavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good PartsJavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good PartsKonrad Malawski
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingVisual Engineering
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creatorsGeorge Bukhanov
 
JavaScript promise
JavaScript promiseJavaScript promise
JavaScript promiseeslam_me
 
Domo Arigato, Mr(uby) Roboto
Domo Arigato, Mr(uby) RobotoDomo Arigato, Mr(uby) Roboto
Domo Arigato, Mr(uby) Robotoyamanekko
 
activity_and_fragment_may_2020_lakopi
activity_and_fragment_may_2020_lakopiactivity_and_fragment_may_2020_lakopi
activity_and_fragment_may_2020_lakopiToru Wonyoung Choi
 
Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.UA Mobile
 
The redux saga begins
The redux saga beginsThe redux saga begins
The redux saga beginsDaniel Franz
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageDroidConTLV
 
You will learn RxJS in 2017
You will learn RxJS in 2017You will learn RxJS in 2017
You will learn RxJS in 2017名辰 洪
 

What's hot (20)

Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.js
 
Vue next
Vue nextVue next
Vue next
 
Callbacks and control flow in Node js
Callbacks and control flow in Node jsCallbacks and control flow in Node js
Callbacks and control flow in Node js
 
Boom! Promises/A+ Was Born
Boom! Promises/A+ Was BornBoom! Promises/A+ Was Born
Boom! Promises/A+ Was Born
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Asynchronní programování
Asynchronní programováníAsynchronní programování
Asynchronní programování
 
Redux Sagas - React Alicante
Redux Sagas - React AlicanteRedux Sagas - React Alicante
Redux Sagas - React Alicante
 
Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...
 
JavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good PartsJavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good Parts
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creators
 
Coroutines in Kotlin
Coroutines in KotlinCoroutines in Kotlin
Coroutines in Kotlin
 
JavaScript promise
JavaScript promiseJavaScript promise
JavaScript promise
 
Domo Arigato, Mr(uby) Roboto
Domo Arigato, Mr(uby) RobotoDomo Arigato, Mr(uby) Roboto
Domo Arigato, Mr(uby) Roboto
 
Retrofit
RetrofitRetrofit
Retrofit
 
activity_and_fragment_may_2020_lakopi
activity_and_fragment_may_2020_lakopiactivity_and_fragment_may_2020_lakopi
activity_and_fragment_may_2020_lakopi
 
Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.
 
The redux saga begins
The redux saga beginsThe redux saga begins
The redux saga begins
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritage
 
You will learn RxJS in 2017
You will learn RxJS in 2017You will learn RxJS in 2017
You will learn RxJS in 2017
 

Similar to Asynchronous CompletableFuture Presentation by László-Róbert Albert @Crossover

Leverage CompletableFutures to handle async queries. DevNexus 2022
Leverage CompletableFutures to handle async queries. DevNexus 2022Leverage CompletableFutures to handle async queries. DevNexus 2022
Leverage CompletableFutures to handle async queries. DevNexus 2022David Gómez García
 
Leveraging Completable Futures to handle your query results Asynchrhonously
Leveraging Completable Futures to handle your query results AsynchrhonouslyLeveraging Completable Futures to handle your query results Asynchrhonously
Leveraging Completable Futures to handle your query results AsynchrhonouslyDavid Gómez García
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureJosé Paumard
 
iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기Wanbok Choi
 
Reactive programming with tracker
Reactive programming with trackerReactive programming with tracker
Reactive programming with trackerDesignveloper
 
The 2016 Android Developer Toolbox [NANTES]
The 2016 Android Developer Toolbox [NANTES]The 2016 Android Developer Toolbox [NANTES]
The 2016 Android Developer Toolbox [NANTES]Nilhcem
 
Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Yoshifumi Kawai
 
Java 8 Puzzlers as it was presented at Codemash 2017
Java 8 Puzzlers as it was presented at Codemash 2017Java 8 Puzzlers as it was presented at Codemash 2017
Java 8 Puzzlers as it was presented at Codemash 2017Baruch Sadogursky
 
The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]Nilhcem
 
Orsiso
OrsisoOrsiso
Orsisoe27
 
TPSE Thailand 2015 - Rethinking Web with React and Flux
TPSE Thailand 2015 - Rethinking Web with React and FluxTPSE Thailand 2015 - Rethinking Web with React and Flux
TPSE Thailand 2015 - Rethinking Web with React and FluxJirat Kijlerdpornpailoj
 
README.MD for building the first purely digital mobile bank in Indonesia
README.MD for building the first purely digital mobile bank in Indonesia README.MD for building the first purely digital mobile bank in Indonesia
README.MD for building the first purely digital mobile bank in Indonesia Richard Radics
 
Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonfNataliya Patsovska
 
SwiftUI와 TCA로 GitHub Search앱 만들기
SwiftUI와 TCA로 GitHub Search앱 만들기SwiftUI와 TCA로 GitHub Search앱 만들기
SwiftUI와 TCA로 GitHub Search앱 만들기규영 허
 
CSharp for Unity Day2
CSharp for Unity Day2CSharp for Unity Day2
CSharp for Unity Day2Duong Thanh
 

Similar to Asynchronous CompletableFuture Presentation by László-Róbert Albert @Crossover (20)

Leverage CompletableFutures to handle async queries. DevNexus 2022
Leverage CompletableFutures to handle async queries. DevNexus 2022Leverage CompletableFutures to handle async queries. DevNexus 2022
Leverage CompletableFutures to handle async queries. DevNexus 2022
 
Leveraging Completable Futures to handle your query results Asynchrhonously
Leveraging Completable Futures to handle your query results AsynchrhonouslyLeveraging Completable Futures to handle your query results Asynchrhonously
Leveraging Completable Futures to handle your query results Asynchrhonously
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFuture
 
iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기
 
Reactive programming with tracker
Reactive programming with trackerReactive programming with tracker
Reactive programming with tracker
 
The 2016 Android Developer Toolbox [NANTES]
The 2016 Android Developer Toolbox [NANTES]The 2016 Android Developer Toolbox [NANTES]
The 2016 Android Developer Toolbox [NANTES]
 
Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)
 
Java 8 Puzzlers as it was presented at Codemash 2017
Java 8 Puzzlers as it was presented at Codemash 2017Java 8 Puzzlers as it was presented at Codemash 2017
Java 8 Puzzlers as it was presented at Codemash 2017
 
The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]
 
Orsiso
OrsisoOrsiso
Orsiso
 
Iniciación rx java
Iniciación rx javaIniciación rx java
Iniciación rx java
 
TPSE Thailand 2015 - Rethinking Web with React and Flux
TPSE Thailand 2015 - Rethinking Web with React and FluxTPSE Thailand 2015 - Rethinking Web with React and Flux
TPSE Thailand 2015 - Rethinking Web with React and Flux
 
README.MD for building the first purely digital mobile bank in Indonesia
README.MD for building the first purely digital mobile bank in Indonesia README.MD for building the first purely digital mobile bank in Indonesia
README.MD for building the first purely digital mobile bank in Indonesia
 
Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonf
 
SwiftUI와 TCA로 GitHub Search앱 만들기
SwiftUI와 TCA로 GitHub Search앱 만들기SwiftUI와 TCA로 GitHub Search앱 만들기
SwiftUI와 TCA로 GitHub Search앱 만들기
 
Android Pro Recipes
Android Pro RecipesAndroid Pro Recipes
Android Pro Recipes
 
CSharp for Unity Day2
CSharp for Unity Day2CSharp for Unity Day2
CSharp for Unity Day2
 
Day 5
Day 5Day 5
Day 5
 
@Anywhere
@Anywhere@Anywhere
@Anywhere
 
Reactive fsharp
Reactive fsharpReactive fsharp
Reactive fsharp
 

Recently uploaded

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...Elena Simperl
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2DianaGray10
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeCzechDreamin
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaCzechDreamin
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backElena Simperl
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsPaul Groth
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...CzechDreamin
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxDavid Michel
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...Sri Ambati
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...CzechDreamin
 

Recently uploaded (20)

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 

Asynchronous CompletableFuture Presentation by László-Róbert Albert @Crossover

  • 3. albertlr.ro #BTechWeek About me Chief Software Architect @ Crossover 10+ years in Java Speaker MsC in Computer Science Proud father of 3
  • 8. albertlr.ro #BTechWeek j.u.c.CompletableFuture • Usage in Java 9 • Process API • HttpClient
  • 9. albertlr.ro #BTechWeek j.u.c.CompletableFuture public class CompletableFuture<T> implements Future<T>, CompletionStage<T> { ... }
  • 10. albertlr.ro #BTechWeek j.u.c.Future public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
  • 11. albertlr.ro #BTechWeek j.u.c.CompletionStage public interface CompletionStage<T> { public <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn); public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn); public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor); public CompletionStage<Void> thenAccept(Consumer<? super T> action); public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action); public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor); public CompletionStage<Void> thenRun(Runnable action); public CompletionStage<Void> thenRunAsync(Runnable action); public CompletionStage<Void> thenRunAsync(Runnable action, Executor executor); public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn); public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn); public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor); public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action); public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action); public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor); public CompletionStage<Void> runAfterBoth(CompletionStage<?> other, Runnable action); public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action); public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor); public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn); public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn); public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor); public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action); public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action); public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor); public CompletionStage<Void> runAfterEither(CompletionStage<?> other, Runnable action); public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action); public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor); public <U> CompletionStage<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn); public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn); public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor); public CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn); public CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable> action); public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action); public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor); public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn); public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn); public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor); public CompletableFuture<T> toCompletableFuture(); }
  • 12. albertlr.ro #BTechWeek j.u.c.CompletionStage public interface CompletionStage<T> { public <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn); public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn); public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor); public CompletionStage<Void> thenAccept(Consumer<? super T> action); public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action); public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor); public CompletionStage<Void> thenRun(Runnable action); public CompletionStage<Void> thenRunAsync(Runnable action); public CompletionStage<Void> thenRunAsync(Runnable action, Executor executor); public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn); public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn); public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor); public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action); public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action); public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor); public CompletionStage<Void> runAfterBoth(CompletionStage<?> other, Runnable action); public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action); public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor); public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn); public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn); public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor); public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action); public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action); public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor); public CompletionStage<Void> runAfterEither(CompletionStage<?> other, Runnable action); public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action); public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor); public <U> CompletionStage<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn); public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn); public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor); public CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn); public CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable> action); public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action); public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor); public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn); public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn); public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor); public CompletableFuture<T> toCompletableFuture(); }
  • 14. albertlr.ro #BTechWeek j.u.c.CompletionStage • Contains 38 methods • 36 of them has 3 forms
  • 15. albertlr.ro #BTechWeek j.u.c.CompletionStage • Contains 38 methods • 36 of them has 3 forms: public CompletionStage<?> somethingAsync(..., Executor executor); public CompletionStage<?> somethingAsync(...); public CompletionStage<?> something(...);
  • 16. albertlr.ro #BTechWeek j.u.c.CompletionStage public CompletionStage<?> somethingAsync(..., Executor executor); • runs action chain in executor public CompletionStage<?> somethingAsync(...); • Same as somethingAsync(..., ForkJoinPool.commonPool()) public CompletionStage<?> something(...); • default execution
  • 17. albertlr.ro #BTechWeek j.u.c.CompletionStage public CompletionStage<?> somethingAsync(..., Executor executor); • runs action chain in executor public CompletionStage<?> somethingAsync(...); • Same as somethingAsync(..., ForkJoinPool.commonPool()) public CompletionStage<?> something(...); • default execution
  • 18. albertlr.ro #BTechWeek j.u.c.CompletionStage public CompletionStage<?> somethingAsync(..., Executor executor); • runs action chain in executor public CompletionStage<?> somethingAsync(...); • Same as somethingAsync(..., ForkJoinPool.commonPool()) public CompletionStage<?> something(...); • default execution
  • 20. albertlr.ro #BTechWeek j.u.c.CompletionStage • 12 methods remain • 9 of them has 3 forms
  • 21. albertlr.ro #BTechWeek j.u.c.CompletionStage • 12 methods remain • 9 of them has 3 forms: Apply - function from input to R, result is a CompletableFuture<R>
  • 22. albertlr.ro #BTechWeek j.u.c.CompletionStage • 12 methods remain • 9 of them has 3 forms: Apply - function from input to R, result is a CompletableFuture<R> Accept - consumer of input, result is a CompletableFuture<Void>
  • 23. albertlr.ro #BTechWeek j.u.c.CompletionStage • 12 methods remain • 9 of them has 3 forms: Apply - function from input to R, result is a CompletableFuture<R> Accept - consumer of input, result is a CompletableFuture<Void> Run - just execute a Runnable, result is a CompletableFuture<Void>
  • 24. albertlr.ro #BTechWeek j.u.c.CompletionStage • single input thenApply, thenAccept, thenRun
  • 25. albertlr.ro #BTechWeek j.u.c.CompletionStage • single input thenApply, thenAccept, thenRun • binary <<or>> applyToEither, acceptEither, runAfterEither
  • 26. albertlr.ro #BTechWeek j.u.c.CompletionStage • single input thenApply, thenAccept, thenRun • binary <<or>> applyToEither, acceptEither, runAfterEither • binary <<and>> thenCombine, thenAcceptBoth, runAfterBoth
  • 27. albertlr.ro #BTechWeek j.u.c.CompletionStage • 3 methods remained: thenCompose handle whenComplete
  • 28. albertlr.ro #BTechWeek j.u.c.CompletionStage thenCompose • Function from input to CompletableFuture<R>, result CompletableFuture<R> • a.k.a flatMap
  • 29. albertlr.ro #BTechWeek j.u.c.CompletionStage handle • Function from input and exception to R, result CompletableFuture<R>
  • 30. albertlr.ro #BTechWeek j.u.c.CompletionStage whenComplete • Consumer from input and exception • Similar to Accept methods above • Result - the same as input
  • 31. albertlr.ro #BTechWeek j.u.c.CompletionStage • 2 methods remained (doesn't have async versions): public CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn); public CompletableFuture<T> toCompletableFuture();
  • 32. albertlr.ro #BTechWeek j.u.c.CompletableFuture public class CompletableFuture<T> implements Future<T>, CompletionStage<T> { public CompletableFuture() { .. } public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) { .. } public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) { .. } public static <U> CompletableFuture<U> completedFuture(U value) { .. } public static CompletableFuture<Void> runAsync(Runnable runnable) { .. } public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) { .. } public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) { .. } public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { .. } public boolean cancel(boolean mayInterruptIfRunning) { .. } public boolean complete(T value) { .. } public boolean completeExceptionally(Throwable ex) { .. } public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn) { .. } public T get() throws InterruptedException, ExecutionException { .. } public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { .. } public T getNow(T valueIfAbsent) { .. } public int getNumberOfDependents() { .. } public boolean isCancelled() { .. } public boolean isCompletedExceptionally() { .. } public boolean isDone() { .. } public T join() { .. } public void obtrudeException(Throwable ex) { .. } public void obtrudeValue(T value) { .. } ... }
  • 33. albertlr.ro #BTechWeek j.u.c.CompletableFuture public class CompletableFuture<T> implements Future<T>, CompletionStage<T> { public CompletableFuture() { .. } public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) { .. } public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) { .. } public static <U> CompletableFuture<U> completedFuture(U value) { .. } public static CompletableFuture<Void> runAsync(Runnable runnable) { .. } public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) { .. } public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) { .. } public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { .. } public boolean cancel(boolean mayInterruptIfRunning) { .. } public boolean complete(T value) { .. } public boolean completeExceptionally(Throwable ex) { .. } public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn) { .. } public T get() throws InterruptedException, ExecutionException { .. } public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { .. } public T getNow(T valueIfAbsent) { .. } public int getNumberOfDependents() { .. } public boolean isCancelled() { .. } public boolean isCompletedExceptionally() { .. } public boolean isDone() { .. } public T join() { .. } public void obtrudeException(Throwable ex) { .. } public void obtrudeValue(T value) { .. } ... }
  • 34. albertlr.ro #BTechWeek j.u.c.CompletableFuture • Contains 38 methods inherited from CompletionStage
  • 35. albertlr.ro #BTechWeek j.u.c.CompletableFuture • Contains 38 methods inherited from CompletionStage and • 14 other instance methods • 7 static methods
  • 36. albertlr.ro #BTechWeek j.u.c.CompletableFuture • 6 ways to complete future complete/completeAsync/completeExceptionally cancel obtrudeValue/obtrudeException
  • 37. albertlr.ro #BTechWeek j.u.c.CompletableFuture • 4 ways to get value get/join - blocking get(timeout, timeUnit) - not so blocking getNow(valueIfAbsent) - non-blocking
  • 38. albertlr.ro #BTechWeek j.u.c.CompletableFuture • 3 ways to know status isDone isCompletedExceptionally isCancelled
  • 39. albertlr.ro #BTechWeek j.u.c.CompletableFuture • 5 static methods to create future completedFuture runAsync(Runnable, Executor) -> CompletableFuture<Void> supplyAsync(Supplier<U>, Executor) -> CompletableFuture<U>
  • 40. albertlr.ro #BTechWeek j.u.c.CompletableFuture • 2 static methods to chain futures allOf(CompletableFuture<?>...) -> CompletableFuture<Void> anyOf(CompletableFuture<?>...) -> CompletableFuture<Object>
  • 42. albertlr.ro #BTechWeek Blocking or asynchronous? • Blocking R doSomething(...); • Asynchronous: CompletionStage<R> doSomethingAsync(..., Executor executor); CompletionStage<R> doSomethingAsync(...);
  • 43. albertlr.ro #BTechWeek Blocking or asynchronous? R doSmth(...) { return doSmthAsync(...).join(); }
  • 44. albertlr.ro #BTechWeek Blocking or asynchronous? R doSmth(...) { return doSmthAsync(...).join(); }
  • 47. albertlr.ro #BTechWeek Reference • JavaDoc & the JDK • https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ CompletableFuture.html • HttpClient - http://openjdk.java.net/groups/net/httpclient/intro.html • https://docs.oracle.com/javase/9/docs/api/jdk/incubator/http/ HttpClient.html • Process API - http://openjdk.java.net/jeps/102 • practice 😀
  • 48. albertlr.ro #BTechWeek Asynchronous CompletableFuture László-Róbert Albert May 18th, Bucharest albertlr.ro #BTechWeek Thank you