Wednesday, November 22, 2017

Why I use CompletableFuture over Future?

Limitations of Future:

  1.  If you are calling a remote api and after sometimes the remote API service is down, then you want to complete the Future manually by the last cached data. But it cannot be done by Future.
  2. Future has methods like get() and isDone(). Future does not notify you about its completion. It provides a get() method which blocks until the result is available.
  3. Multiple Futures can not be chained together. 
  4. Multiple Futures can not be combined together.
  5. No exception handling is available in Future.
But  CompletableFuture has overcome all those issues.
  • CompletableFuture implements Future and CompletionStage interfaces
  • It provides a huge set of convenience methods for creating, chaining and combining multiple Futures.
  • It also provides a very comprehensive exception handling support.


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

No comments:

Post a Comment