Java 8 parallelStream to call / REST database at the same time

Here I use a Javaparallel stream to iterate through a list and call a REST call with each list item as input. I need to add all the results of a REST call to the collection for which I am using ArrayList . The code below works just fine, except that non-thread-specific ArrayList stream security will produce incorrect results, and adding the necessary synchronization will lead to a conflict, undermining the parallelism advantage.

Can someone please suggest me the correct way to use parallel thread 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); } 
+5
source share
2 answers

You can perform a map operation instead of forEach - this will ensure thread safety (and be cleaner in terms of functional programming):

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

And your callRestAPI method :

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

I would not shy away from synchronizing access to your ArrayList . Given that you are accessing the remote service through Rest, I suspect that the cost of synchronization will be negligible. I would measure the effect before spending time optimizing.

+2
source

All Articles