- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在这里,我使用 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/
这个问题已经有答案了: How do I write a correct micro-benchmark in Java? (11 个回答) 已关闭 5 年前。 我正在对 Java 聚合操作进行一些性
我发现我对并行流提供的一致性保证略有挣扎: 1. myList.parallelStream().map(mymapper).forEach(myFn) 2. // Is myFn guarantee
我有一个方法可以并行计算列表中数字的平方并将它们相加: public static double sumSquared(List values) { return values
这个问题已经有答案了: How do Java 8 parallel streams behave on a thrown exception? (1 个回答) 已关闭 4 年前。 我正在研究java
通常当使用 Java 8 的 parallelStream() 时,结果是通过默认的、通用的 fork-join 池(即 ForkJoinPool.commonPool())执行。 这显然是不可取的,
Java 8 parallelStream 似乎使用了比系统属性 java.util.concurrent.ForkJoinPool.common.parallelism 指定的线程更多的线程。这些单
我有以下代码,有时它的行为不确定。例如,我在那里传递了 3 个事件,而输出只有两个!你能解释一下这种行为的原因吗? public List getEventResponse(final List ev
Java8并行流(parallelStream)注意点 在最初使用并行流的时候,查询列表会偶尔性报空指针异常,这令我非常纳闷 代码是这样的: ?
使用示例可能更容易解释我想要做的事情。假设我必须遵循两个数组: int firstArray[] = {1, 2, 3, 4, 5}; int secArray[] = {1, 2, 3, 4, 5}
我正在尝试使用 chronicleMap.parallelStream: myChronicleMap.entrySet().parallelStream().forEach((entry) -> {
我正在使用parallelStream并行上传一些文件,有些是大文件,有些是小文件。我注意到并非所有 worker 都被使用。 一开始一切都运行良好,所有线程都被使用(我将并行度选项设置为 16)。然
这个问题在这里已经有了答案: How can I turn a List of Lists into a List in Java 8? (12 个答案) 关闭 8 年前。 我想将并行流中的列表结果
我有以下代码 public void addNames(){ List names = new ArrayList names.parallelStream().foreach(name-
如果输入大小太小,库 automatically serializes the execution of the maps in the stream ,但这种自动化没有也不能考虑 map 操作的繁重
我创建了一个并行度为 25 的自定义 ForkJoinPool。 customForkJoinPool = new ForkJoinPool(25); 我有一个包含 700 个文件名的列表,我使用这样
我在 Internet 上看到了很多示例,为了使用流 API 来执行并行操作,只需像这样调用 .parallelStream() 方法: mySet .parallelStream()
考虑这个(完全人为的)Java 代码: final List s = Arrays.asList(1, 2, 3); final int[] a = new int[1]; a[0] = 100; s
谁能告诉我为什么会这样,这是预期的行为还是错误 List a = Arrays.asList(1,1,3,3); a.parallelStream().filter(Objects::nonNull)
方法一 通常,非常快,并且效果很好。 public static int loops = 500; private static ExecutorService customPool = Execut
默认情况下,parallelStream 内的 commonPool 大小应为 cpu_cores - 1。 但是,在我的应用程序中,它始终大于硬件 cpu_cores。 VisualVM 截图: 很
我是一名优秀的程序员,十分优秀!