Tech Master Tutorials
Email Facebook Google LinkedIn Pinterest Twitter
Home Java Java 8 Java Interview Questions Java8 Interview Questions JVM Java Programming

Functional Interfaces

Prior to understanding CompletionStage, good to have knowledge on the below
Future : A Future represents the result of an asynchronous computation.
Future provides below useful methods –
  • get() – get is used to get the result of the asynchronous computation. If computation is still in progress then get() method will block the program in which the get() is called. So get should be used with proper thought otherwise execution may block.
  • isDone() – isDone() does not block. Simple returns true if thread is executed and result is available. In program isDone() can be used to check if asynchronous computation is done and then get()call can b made.
  • Cancel() – cancellation of the task is done using the cancel method.

    • As part of the below example we have a Future object which holds the result returned from the thread.
      import java.util.concurrent.ExecutionException;
      import java.util.concurrent.ExecutorService;
      import java.util.concurrent.Executors;
      import java.util.concurrent.Future;
      public class MainClass_Callable {
      	public static void main(String[] args) {
      		ExecutorCallable callableObj = new ExecutorCallable();
      		ExecutorService executor = Executors.newSingleThreadScheduledExecutor();
      		Future intResult = executor.submit(callableObj);
      		try {
      			if (!intResult.isDone()) {
      			int result = intResult.get();
      			System.out.println("result from future = " + result);
      		} catch (InterruptedException | ExecutionException e) {

      Limitation of Future :
      • Future object’s get() method blocks the execution, trying to get the thread’s output blocks further processing.
      • We cannot chain and combine multiple futures.
      • Does not provide exception handling

      CompletableFuture: CompletableFuture adds to the Asynchronous computation capability of the Java that was powered by Future earlier.
      CompletableFuture implements Future and CompletionStage both interfaces. CompletableFuture is a Future with many other capabilities CompletionStage interface consists of many methods for chaining and combining the computation results as well as to handle the exceptions.
      Some of the frequently used methods from CompletionStage:
      • thenApply() : Supplies the result of the completion stage as input and returns a new CompletionStage that holds the result of the computation passed inside the function. This method wraps the final output inside a CompletionStage.
      • thenAccept(): Accept the result of the CompletionStage, and computation passed inside the method completes, then return a CompletionStage of void type.
      • thenCombine() : Combine the execution of two different completion stages.
      • thenCompose () : Supplies the result of the completion stage as input and returns a new CompletionStage that holds the result of the computation passed inside the function.
      • exceptionally() : Whenever there is any exception inside the completion stage or completion stage chain, program control will come inside this method. This method will be executed whenever there is any exception and the statement written inside this block will be executed.
      • handle() : Receives the result of the completion stage and exception if any.
      • toCompletableFuture(): Util method to convert from CompletionStage to CompletableFuture.