【发布时间】:2020-11-20 12:01:16
【问题描述】:
对于 same 输入,以下代码在每 10-15 次尝试中抛出 IllegalArgumentException:
AllDirectedPaths<Vertex, Edge> allDirectedPaths = new AllDirectedPaths<>(graph);
List<GraphPath<Vertex, Edge>> paths = allDirectedPaths.getAllPaths(entry, exit, true, null);
return paths.parallelStream().map(path -> path.getEdgeList().parallelStream()
.map(edge -> {
Vertex source = edge.getSource();
Vertex target = edge.getTarget();
if (source.containsInstruction(method, instructionIndex)) {
return source;
} else if (target.containsInstruction(method, instructionIndex)) {
return target;
} else {
return null;
}
}).filter(Objects::nonNull)).findAny().flatMap(Stream::findAny)
.orElseThrow(() -> new IllegalArgumentException("Given trace refers to no vertex in graph!"));
代码的想法是找到一个包装特定指令的顶点(参见containsInstruction()),而该顶点至少位于从entry 到exit 顶点的一条路径上。我知道代码在性能方面并不是最优的(路径上的每个中间顶点都被查找两次),但这并不重要。
输入只是一个跟踪(字符串),可以从中导出method 和instructionIndex。从这个意义上说,所有其他变量都是固定的。而且containsInstruction()这个方法没有任何副作用。
将“findAny()”流操作放在哪里重要吗?我应该把它直接放在过滤器操作之后吗?还是嵌套的并行流是问题所在?
【问题讨论】:
标签: java parallel-processing java-stream