Wednesday, March 7, 2018

Java 8 at a glance

runAsync() method:
=================
If you want to run some background task asynchronously and don’t want to return anything from the task, then you can use CompletableFuture.runAsync() method.

supplyAsync() method:
=====================
Run a task asynchronously and return the result using supplyAsync()

get() method:
=============
The CompletableFuture.get() method is blocking. It waits until the Future is completed and returns the result after its completion.

Example: String result = completableFuture.get()

CompletableFuture.complete() method:
===================================
CompletableFuture.complete() method is used to manually complete a Future.

Example: completableFuture.complete("Future's Result")

All the clients waiting for this Future will get the specified result. And, Subsequent calls to completableFuture.complete() will be ignored.

thenApply() method: [Will return]
===================
done sequentially after completion one by one. First one is completed first, then second one will start taking the first one's output.

thenApply() method is used to process and transform the result of a CompletableFuture when it arrives.

You can also write a sequence of transformations on the CompletableFuture by attaching a series of thenApply() callback methods. The result of one thenApply() method is passed to the next in the series


thenAccept() method: [Wouldn't return]
===================
thenRun() method:
=================

If you don’t want to return anything from your callback function and just want to run some piece of code after the completion of the Future, then you can use thenAccept() and thenRun() methods. These methods are consumers and are often used as the last callback in the callback chain.

CompletableFuture.thenAccept() takes a Consumer<T> and returns CompletableFuture<Void>

thenCompose() method:
=====================

thenCombine() method:
=====================

thenApplyAsync() method:
========================

CompletableFuture.allOf() method:
=================================

CompletableFuture.anyOf() method:
=================================


Difference between thenAccept() and thenRun():
==============================================
CompletableFuture.thenAccept() takes a Consumer<T> and returns CompletableFuture<Void>. It has access to the result of the CompletableFuture on which it is attached.

// thenAccept() example
CompletableFuture.supplyAsync(() -> {
return ProductService.getProductDetail(productId);
}).thenAccept(product -> {
System.out.println("Got product detail from remote service " + product.getName())
});
While thenAccept() has access to the result of the CompletableFuture on which it is attached, thenRun() doesn’t even have access to the Future’s result. It takes a Runnable and returns CompletableFuture<Void> -

// thenRun() example
CompletableFuture.supplyAsync(() -> {
    // Run some computation 
}).thenRun(() -> {
    // Computation Finished.
});

Resource Link: https://www.callicoder.com/java-8-completablefuture-tutorial/


No comments:

Post a Comment