How to make parallel calls in Java with CompletableFuture example


Octocat Save up to a workweek a year by efficiently managing your coding bookmarks, aka #codingmarks, on www.codingmarks.org. Share your favorites with the community and they will be published weekly on Github. Help us build the programming-resources location - Star


Some time ago I wrote how elegant and rapid is to make parallel calls in NodeJS with async-await and Promise.all capabilities. Well, it turns out in Java is just as elegant and succinct with the help of CompletableFuture which was introduced in Java 8. To demonstrate that let’s imagine that you need to retrieve a list of ToDos from a REST service, given their Ids. Of course you could iterate through the list of Ids and sequentially call the web service, but it’s much more performant to do it in parallel with asynchronous calls.

Here is the piece of code that might do just that:

import org.codingpedia.example;

import javax.inject.Inject;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class ParallelCallsDemoService {

    @Inject
    RestApiClient restApiClient;

    public List<ToDo> getToDos(List<String> ids){

        List<CompletableFuture<ToDo>> futures =
                ids.stream()
                          .map(id -> getToDoAsync(id))
                          .collect(Collectors.toList());

        List<ToDo> result =
                futures.stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList());

        return result;
    }


    CompletableFuture<ToDo> getToDoAsync(String id){

        CompletableFuture<ToDo> future = CompletableFuture.supplyAsync(new Supplier<ToDo>() {
            @Override
            public ToDo get() {
                final ToDo toDo = restApiClient.getToDo(id);

                return toDo;
            }
        });

        return future;
    }

}

Notes:

  • the supplyAsync method takes a Supplier which contains the code you want to execute asynchronously - in this case this is where the REST call takes place…
  • you fire all the rest calls and you collect all the futures in a list
  • in the end you collect all the results with the help of the join() method - this returns the value of the CompletableFuture when complete, or throws an (unchecked) exception if completed exceptionally.

Adrian Matei

Adrian Matei
Life force expressing itself as a coding capable human being

How I save a workweek a year by efficiently managing my codingmarks

Finding a desired link, you already visited, can be a tedious job and sometimes even frustrating. It normally takes 30 seconds or more to look for a link the traditional way, and I do that at least 20 times a day. With codingmarks.org it takes about 10 seconds, so I get a time saving of around 20 * 20 / 60 = 6.66 minutes a day. Over a year's time this translates to around 40+ hours of saved time. Continue reading

New codingmarks published in week 52 of 2018

Published on January 01, 2019