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 =
source share