Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 CompletableFuture vs Netty Future

How does the CompletableFuture introduced in JDK 8 compare with the io.netty.util.concurrent.Future provided by Netty ?

Netty documentation mentions that

JDK 8 adds CompletableFuture which somewhat overlaps io.netty.util.concurrent.Future http://netty.io/wiki/using-as-a-generic-library.html

The questions I'm trying to get answers to are:

  1. What would their similarities and differences be?
  2. How would the performance characteristics of the two differ? Which one would be able to scale better?

With respect to the similarities/ differences, I have been able to come up with the following:

Similarities: The fundamental similarity being that both are non-blocking as compared to the Java Future. Both the classes have methods available to add a listener to the future, introspect failure and success of the task and get results from the task.

Differences: CompletableFuture seems to have a much richer interface for things like composing multiple async activities etc. Netty's io.netty.util.concurrent.Future on the other hand allows for multiple listeners to be added to the same Future, and moreover allows for listeners to be removed.

like image 761
Prateek Avatar asked Dec 07 '15 04:12

Prateek


1 Answers

If we look at that whole paragraph (especially the first sentence)

Java sometimes advances by adopting ideas that subsume constructs provided by Netty. For example, JDK 8 adds CompletableFuture which somewhat overlaps io.netty.util.concurrent.Future. In such a case, Netty's constructs provide a good migration path to you; We will diligently update the API with future migration in mind.

What it's basically saying is that the netty Future and CompletableFuture are the same concept, but implemented at different times by different people.
Netty made their future because there wasn't one available in java, and they didn't want to pull one in as a dependency from something like Guice. But now, java has created one, and it's available for use.
In the end of the paragraph they're basically saying that the netty API may replace Future with CompletableFuture in the future.
As far as similarities/differences, they're both just one of many implementations of the future/promise pattern. Use the netty one when you're using the netty api and netty specific stuff, otherwise use CompletableFuture.

like image 124
kag0 Avatar answered Sep 22 '22 14:09

kag0