Manikandan Kbk
Manikandan Kbk

Reputation: 151

CompletableFuture vs ExecutorService

I have tried complex usecases which can be done by CompletableFuture can be done by ExecutorService as well. That includes, handling exceptions as well.

The only difference I can see between them is, CompletableFuture gives better readability and convenience during coding.

Is there any practical advantage / use-case that one can solve using CompletableFuture, but not with ExecutorService?

Upvotes: 2

Views: 3994

Answers (2)

Eugene
Eugene

Reputation: 121048

Imagine the simple case, you do action A, when it is complicated, do B with the result of A operation - be that a Throwable or a valid result. Add to that that there is no CompletableFuture yet, and you do not want to block. Is it doable with Futures only? Well, yes. Is it painful to achieve? Very.

Do not get fooled by the "simplicity" of its API. In your previous question you already saw what not specifying an ExecutorService to some actions does. There are many, many more subtle things that comes with the package of that "simplicity".

The non-blocking capabilities of CompletableFuture made it highly usable and highly adopted by the developers. Not only us, usual developers, but the jdk ones too - the entire jdk http client is build around CompletableFuture, for a reason.

And your question is not vs ExecutorService, but should really be vs Future. An ExecutorService can still be passed to methods that CompletableFuture provides. For example read this Q&A for what various methods do and act like.

Upvotes: 2

kofemann
kofemann

Reputation: 4423

The main advantage of the CompletableFuture (at least for me) is that it allows to build a complex asynchronous processes within a higher probability to get it right from the first time and still be able to understand the logic after some time.

Of course the most of it can be implemented using ExecutorService, which will require to subclass it, override some methods, re-submit the task... Most of it is covered by CompletableFuture out of the box. Moreover, with every jdk update the best practices get integrated into the CompletableFuture class.

Thus, the practical reason to use CompletableFuture is the simplicity (well, you still need to understand how to use it).

For example :

CompletableFuture.supplyAsync(...)
    .completeOnTimeout("foo" , 1, TimeUnit.SECONDS);

Is not that simple with ExecutorService to implement.

Upvotes: 1

Related Questions