Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I return CompletableFuture or Future when defining API?

In Java 8, is it better for interface or abstract class to define APIs returning CompletableFuture instead of returning Future? Considering it is ugly converting Future to CompletableFuture and the fact that CompletableFuture will give the caller more flexibility of using functional style directly, what could be a good reason for an API to just return Future?

like image 339
derrdji Avatar asked Jan 21 '16 17:01

derrdji


People also ask

When should I use CompletableFuture?

CompletableFuture is used for asynchronous programming in Java. Asynchronous programming is a means of writing non-blocking code by running a task on a separate thread than the main application thread and notifying the main thread about its progress, completion or failure.

What is the difference between future and CompletableFuture?

As a result: Future transferes single value using synchronous interface. CompletableFuture transferes single value using both synchronous and asynchronous interfaces. Rx transferes multiple values using asynchronous interface with backpressure.

What is the purpose of CompletableFuture class?

Class CompletableFuture<T> A Future that may be explicitly completed (setting its value and status), and may be used as a CompletionStage , supporting dependent functions and actions that trigger upon its completion.

Is Completable future get blocking?

It just provides a get() method which blocks until the result is available to the main thread. Ultimately, it restricts users from applying any further action on the result. You can create an asynchronous workflow with CompletableFuture. It allows chaining multiple APIs, sending ones to result to another.


2 Answers

Thought I would come back to this and provide some updates on my final decisions:

For my own code/design, I went with using CompletableFuture as the return type, because

  • this is a protected abstract method of an internal part that I want to make extensible;
  • I do not need an interface to define the binding;
  • the main purpose of this return type is a Future (for async IO), I personally feel the functional-styled API provided by CompletableFuture is an added benefit/reminder/encouragement of using functional style to the future devs.

With that being said, I would definitely use the CompletableStage interface as the return type, had I been designing a public API, because:

  • what @assylias and @Holger said about interface vs. realization and chaining ability
  • and the fact that CompletableStage has a method CompletableFuture<T> toCompletableFuture().
like image 146
derrdji Avatar answered Sep 22 '22 05:09

derrdji


My 2 cts:

  • by returning a Future, you keep your options open and can return a Future, or a CompletableFuture - it makes no difference from the caller's perspective.
  • by returning a CompletableFuture, you give the caller more options (they get more methods) but you also commit to returning that type of Future - if in two years you realise that returning a BetterFuture would make more sense, you will have to change the API, which is not good.

So you should probably assess the likelihood that you will want to return something other than a CompletableFuture in the future (haha) and decide accordingly.

like image 32
assylias Avatar answered Sep 21 '22 05:09

assylias