SlideShare a Scribd company logo
1 of 48
Download to read offline
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

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

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