Sunday, October 16, 2022

Stages and Futures



API:
  1. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CompletionStage.html
  2. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CompletableFuture.html


public class CompletableFuture<T> implements Future<T>, CompletionStage<T> 
- execute on ForkJoinPool.commonPool or pass own pool

runAsync - returns CompletableFuture<Void> ,takes Runnable<T>   
-  i.e. doesn't bring back an result

supplyAsync -  returns CompletableFuture<T>, takes Supplier<T>   
-  i.e.  brings back a result

Methods from CompletableFuture you can call once it is done completing its work:

theApply  :   CompletionStage<U> thenApply​(Function<? super T,​? extends U> fn)
- transform the response you received after Computable Future returns
- specify Function<T,R> as argument
- thenApplyAsync method is for running on different thread pool than the default

thenAccept:   CompletionStage<Void> thenAccept​(Consumer<? super T> action)
- its simply just consumes, it takes response Consumer<T> as argument but does not return anything
- thenAcceptAsync method is for running on different thread pool than the default


thenRun:  CompletionStage<Void> thenRun​(Runnable action)
- takes Runnable as argument, doesn't leverage the response
-  thenRunAsync  method is for running on different thread pool than the default

dependent future-  a stage takes in result of a stage it has dependency on:

thenCompose :  CompletionStage<U> thenCompose​(Function<? super T,​? extends CompletionStage<U>> fn)
- consumes, it takes  Function<T,R> as argument , and returns a Completable Future
- chains Futures and run them sequentially
- thenComposeAsync  method is for running on different thread pool than the default
 
combining future-  a stage takes in results of independent stage(s)

thenCombine  :  <U,​V> CompletionStage<V> thenCombine​(CompletionStage<? extends U> other, BiFunction<? super T,​? super U,​? extends V> fn)
- takes BiFunction<U,V,R> as argument and used to run multiple stages in parallel
- thenCombineAsync  method is for running on different thread pool than the default


-returns CompletableFuture<Void>

-returns CompletableFuture<Void>


-its simply just consumes and returns exception 

- its simply just consumes exception as argument





"CompletableFuture ... it allows you to build a pipeline of steps which can each be executed asynchronously, each step depending on the result of one or more previous steps."


"CompletionStage, the superinterface of CompletableFuture, which declares most of the methods that comprise the functionality of CompletableFuture."

" A stage completes upon termination of its computation, but this may in turn trigger other dependent stages"

"CompletionStage specifies most of the functionality of CompletableFuture. Class CompletableFuture adds a number of extra methods"

  • thenApply: Apply a function to the result of the previous stage 
  • thenAccept : Let a consumer deal with the result of the previous stage 
  • thenCombine: Apply a function to the results of both of two previous stages 
  • thenAcceptBoth: Let a consumer deal with the results of both of two previous stages thenCompose: Similar to thenApply, except that the function returns CompletionStage instead of MyClass
  • whenComplete : Handle the outcome of a stage, whether it's a result value or an exception
  • exceptionally: When an exception happened, replace the exception with a result value
  • handle : Handle the outcome of a stage and return a new value
  • toCompletableFuture : Convert the CompletionStage to a CompletableFuture