gpt4 book ai didi

用于并发数据库/REST 调用的 Java 8 parallelStream

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:43 25 4
gpt4 key购买 nike

在这里,我使用 Javaparallel 流遍历列表并调用 REST 调用,每个列表元素作为输入。我需要将 REST 调用的所有结果添加到我正在使用 ArrayList 的集合中。下面给出的代码工作正常,除了 ArrayList 的非线程安全会导致不正确的结果,并且添加所需的同步会导致争用,破坏并行的好处。

有人可以建议我在我的案例中使用并行流的正确方法吗。

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);
}

最佳答案

您可以使用 map 而不是 forEach 来执行操作 - 这将保证线程安全(并且从函数式编程的角度来看更清晰):

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

还有您的callRestAPI 方法:

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

关于用于并发数据库/REST 调用的 Java 8 parallelStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30758304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com