简体   繁体   中英

Java 8 parallelStream for concurrent Database / REST call

Here I am using Javaparallel stream to iterate through a List and calling a REST call with each list element as input. I need to add all the results of the REST call to a collection for which I am using an ArrayList . The code given below is working fine except that the non-thread-safety of ArrayList would cause incorrect results, and adding needed synchronization would cause contention, undermining the benefit of parallelism.

Can someone please suggest me a proper way of using parallel stream for my case.

public void myMethod() {
    List<List<String>> partitions = getInputData();
    final List<String> allResult = new ArrayList<String>();
    partitions.parallelStream().forEach(serverList -> callRestAPI(serverList, allResult);
}

private void callRestAPI(List<String> serverList, List<String> allResult) {
    List<String> result = //Do a REST call.
    allResult.addAll(result);
}

You can do the operation with map instead of forEach - that will guarantee thread safety (and is cleaner from a functional programming perspective):

List<String> allResult = partitions.parallelStream()
          .map(this::callRestAPI)
          .flatMap(List::stream) //flattens the lists
          .collect(toList());

And your callRestAPI method:

private List<String> callRestAPI(List<String> serverList) {
    List<String> result = //Do a REST call.
    return result;
}

I wouldn't shy away from synchronising access to your ArrayList . Given that you're accessing a remote service via Rest, I suspect the cost of synchronisation would be negligible . I would measure the effect before you spend time optimising.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM