Back to the CompletableFuture: Java 8 Feature Highlight
A brief look into the new world of completable future in Java 8, allowing proper asynchronous programming (with superheroes).
Join the DZone community and get the full member experience.Join For Free
completablefuture vs future: going async with java 8 new features
java 8 was released in march 2014 and arrived with a long list of new features. one of the less talked about, extremely useful, yet misunderstood features is a brand new and improved extension to the future interface: completablefuture<t>.
in the following post we’ll present an overall view of completablefuture, exactly how is it different from a simple future and when it can be useful.
— takipi (@takipid) may 26, 2016
asynchronous java code
java 7 introduced us to future, which represents the result of an asynchronous computation. the main advantage of using the future object is that you can do other things while waiting for external resources. a non-blocking way to wait for a resource.
using a future means you can write a method and instead of it immediately returning the result, it will return a future object. when you’ll need the actual result, just use future.get() which will return the value after the computation is done.
you also get methods for checking if the computation is done, and a way to cancel / check if it was canceled.
for example, let’s say you’re making a call to some external resource, like… marvel’s developer api , pulling out all superheroes that have the letter… “c” in their name:
yup, if we do want to go for a fully async non-blocking option we’re out of luck. we have no assurance that the future is actually there and we might have to wait. this is where completablefuture comes in, and helps with a cheeky workaround.
so what’s new in completablefuture?
completablefuture<t> extends future<t> and makes it… completable. this is a big deal, considering that future objects were limited before java 8, with only 5 methods available.
this new and improved completablefuture has 2 main benefits:
- it can be explicitly completed by calling the complete() method without any synchronous wait. it allows values of any type to be available in the future with default return values, even if the computation didn’t complete, using default / intermediate results.
- with tens of new methods, it also allows you to build a pipeline data process in a series of actions. you can find a number of patterns for completablefutures such as creating a completablefuture from a task, or building a completablefuture chain. the full list is available via oracle’s completablefuture documentation .
back to our simple example, let’s say that marvel’s api didn’t return a timely result, and getmarvelherowithcharacter() is still processing, taking its time, while we’re already done with everything else that we wanted to do in the meantime. assuming we don’t want to wait ( for our lives to be over ), a completablefuture can help us return an intermediate result. like… mystique, since at the worst case she can shapeshift into any other superhero.
you can also create a completed completablefuture in advance that returns a known value. that might come in handy in your testing environment , in case you’ll want to combine that known value with one that needs to be computed:
tens of other more useful methods are available and they include transforming and acting on one completablefuture (thenapply), running code on completion (thenaccept/thenrun), combining two completablefuture together and more. for a complete guide we recommend you to read java 8: definitive guide to completablefuture .
meet the alternatives
if you’re using guava or scala (with its futures ), this new feature might sound familiar. it’s similar to guava’s listenablefuture , which defines a consistent api for future objects to register completion callbacks.
similarly to the new completablefuture, the ability to add a callback allows responding to incoming events in an asynchronously and effective way. you can register callbacks to be executed when a computation is complete, and support many operations that the basic future interface can’t support.
when the going gets tough
using completablefuture gives us the ability to run along with our code without having to wait for the results, but it still can’t promise your code won’t break while running in production. when errors occur, you’ll need to identify and analyze them as quickly as you can to deploy a hotfix.
for these kinds of situations, takipi will enable you to fix issues in your code effectively when they arise, without having to “wait” until someone else run into them.
completablefuture fits right in as part of the asynchronous programming trend, that became popular during the past few years. it’s no wonder everyone’s talking about it, since we can use it to run a number of tasks at the same time, which allow an optimal workflow.
in case you’re already a fan of asynchronous programming, you might want to check out our post about 7 reactive programming tools you must know .
Published at DZone with permission of Henn Idan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.