Difference Between CompletableFuture And Future In Java

Introduction

Java has undergone several changes and enhancements over the years as a programming language, especially in handling asynchronous programming. Two key components in this domain are Future and CompletableFuture. This blog post aims to compare their features and understand their use cases in Java.

Basic Characteristics

Future: Introduced in Java 5, Future represents the result of an asynchronous computation. It provides methods to check if the calculation is done, to wait for its completion, and to retrieve the result. However, its capabilities are pretty basic and limited.

CompletableFuture: Introduced in Java 8, CompletableFuture is an enhancement of Future. It represents a future result and provides many methods to compose, combine, execute asynchronous tasks, and handle their results without blocking

Method Of Use

Future: It is generally used with an ExecutorService, and we get a Future object when we submit tasks to the executor. However, it doesn’t allow us to define any computation steps to be executed once the computation is finished. The get() method is blocking, which means it waits until the task is completed and can make the application less responsive.

CompletableFuture: It can be used as a Future and also allows us to attach callbacks via methods like thenApplythenAccept, and thenRun. These methods let us execute additional actions upon completion of the original task in a non-blocking fashion. CompletableFuture can also be manually completed.

Exception Handling

Future: Does not provide a built-in mechanism for handling exceptions. Exception handling needs to be implemented externally. Exceptions are caught during the get() method call.

CompletableFuture: Provides methods like exceptionally and handles to deal with exceptions in a chain of asynchronous tasks. Allows defining a recovery or fallback mechanism within the computation chain.

Composing and Combining Results

Future: Lacks native support for combining multiple Future instances or creating a sequence of asynchronous operations.

CompletableFuture: This class supports combining multiple CompletableFuture instances using methods like thenCompose, thenCombine, and allOf. It facilitates the composition and sequencing of multiple asynchronous computations.

Blocking vs. Non-Blocking

Future: It primarily relies on blocking operations (get()) to retrieve the result of the asynchronous computation.

CompletableFuture encourages non-blocking programming. It allows us to process results with functions and actions that are applied asynchronously, thus enhancing your application’s responsiveness.

Asynchronous Execution Support

Future: Does not inherently support the execution of asynchronous code. It’s typically used with ExecutorService to execute Runnable or Callable tasks.

CompletableFuture: Offers built-in support for asynchronous execution with methods like supplyAsync, runAsync, and variants with custom Executor.

Use Cases

Future: Suitable for simple asynchronous operations where we must perform tasks in a separate thread and retrieve the results later.

CompletableFuture: Ideal for complex asynchronous programming needs, including chaining multiple asynchronous operations, error handling, combining results, and implementing non-blocking algorithms.

Conclusion

In summary, while Future provides an essential structure for handling asynchronous computation, CompletableFuture offers a much more robust and flexible framework. It caters to complex asynchronous programming paradigms, making it a preferred choice for modern Java applications dealing with concurrent tasks and asynchronous programming.