Future vs CompletableFuture
A Future is used as a reference to the result of an asynchronous computation. It provides an isDone() method to check whether the computation is done or not, and a get() method to retrieve the result of the computation when it is done.
CompletableFuture is an extension to Java’s Future API which was introduced in Java.
Limitations of future
It can not be manually completed
Let’s say that you’ve written a function to fetch the latest price of an e-commerce product from a remote API. Since, this API call is time consuming, you’re running it in a separate thread and returning a Future from your function Now, let’s say that If the remote API service is down, then you want to complete the Future manually by the last cached price of the product.
Can you do this with Future? No!
You can not perform further action on a Future’s result without blocking Future does not notify you about its completion. It provides a get() method which blocks until the result is available.
You don’t have the ability to attach a callback function to the Future and have it get called automatically when the Future’s result is available.
Multiple Futures can not be chained together
Sometimes you need to execute a long running computation and when the computation is done, you need to send its result to another long running computation, and so on.
You can not create such asynchronous work flow with Futures. You can not combine multiple Futures together
Let’s say that you have 10 different Futures that you want to run in parallel and then run some function after all of them completes. You can’t do this as well with Future.
No Exception Handling
Future API does not have any exception handling construct. Whoa! So many limitations right? Well, That’s why we have CompletableFuture. You can achieve all of the above with CompletableFuture.
CompletableFuture implements Future and CompletionStage interfaces and provides a huge set of convenience methods for creating, chaining and combining multiple Futures. It also has a very comprehensive exception handling support.
Comments
Post a Comment