There's more...

In the example of this recipe, we used the complete(), get(), join(), supplyAsync(), thenApplyAsync(), thenAcceptAsync(), and allOf() methods of the CompletableFuture class. However, this class has a lot of useful methods that help increase the power and flexibility of this class. These are the most interesting ones:

  • Methods to complete a CompletableFuture object: In addition to the complete() method, the CompletableFuture class provides the following three methods:
    • cancel(): This completes CompletableFuture with a CancellationException exception.
    • completeAsync(): This completes CompletableFuture with the result of the Supplier object passed as a parameter. The Supplier object is executed in a different thread by the default executor.
    • completeExceptionally(): This method completes CompletableFuture with the exception passed as a parameter.
  • Methods to execute a task: In addition to the supplyAsync() method, the CompletableFuture class provides the following method:
    • runAsync(): This is a static method of the CompletableFuture class that returns a CompletableFuture object. This object will be completed when the Runnable interface is passed as a parameter to finish its execution. It will be completed with a void result.
  • Methods to synchronize the execution of different tasks: In addition to the allOf(), thenAcceptAsync(), and thenApplyAsync() methods, the CompletableFuture class provides the following methods to synchronize the execution of tasks:
    • anyOf(): This is a static method of the CompletableFuture class. It receives a list of CompletableFuture objects and returns a new CompletableFuture object. This object will be completed with the result of the first CompletableFuture parameter that is completed.
    • runAfterBothAsync(): This method receives CompletionStage and Runnable objects as parameters and returns a new CompletableFuture object. When CompletableFuture (which does the calling) and CompletionStage (which is received as a parameter) are completed, the Runnable object is executed by the default executor and then the CompletableFuture object returned is completed.
    • runAfterEitherAsync(): This method is similar to the previous one, but here, the Runnable interface is executed after one of the two (CompletableFuture or CompletionStage) are completed.
    • thenAcceptBothAsync(): This method receives CompletionStage and BiConsumer objects as parameters and returns CompetableFuture as a parameter. When CompletableFuture (which does the calling) and CompletionStage (which is passed as a parameter), BiConsumer is executed by the default executor. It receives the results of the two CompletionStage objects as parameters but it won't return any result. When BiConsumer finishes its execution, the returned CompletableFuture class is completed without a result.
    • thenCombineAsync(): This method receives a CompletionStage object and a BiFunction object as parameters and returns a new CompletableFuture object. When CompletableFuture (which does the calling) and CompletionStage (which is passed as a parameter) are completed, the BiFunction object is executed; it receives the completion values of both the objects and returns a new result that will be the completion value of the returned CompletableFuture class.
    • thenComposeAsync():This method is analogous to thenApplyAsync(), but it is useful when the supplied function returns CompletableFuture too.
    • thenRunAsync(): This method is analogous to the thenAcceptAsync() method, but in this case, it receives a Runnable object as a parameter instead of a Consumer object.
  • Methods to obtain the completion value: In addition to the get() and join() methods, the CompletableFuture object provides the following method to get the completion value:
    • getNow(): This receives a value of the same type of the completion value of CompletableFuture. If the object is completed, it returns the completion value. Else, it returns the value passed as the parameter.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.188.121