【发布时间】:2016-01-22 16:24:49
【问题描述】:
尝试从 tinkergraph 中进行多线程读取时,我遇到了非常奇怪的行为。我有以下情况:
Graph graph = TinkerGraph.open();
Set<Vertex> verticesAdded = randomGraph(graph);
verticesAdded 是我在randomGraph(graph) 过程中添加的一组顶点。一旦我有了这个列表,我就会检查这个顶点的内部属性,并根据该属性将顶点传递给线程以进行一些额外的工作。我遵循的过程大致是:
public class ValidateVerticies(){
private Set<Vertex> vertices;
private ExecutorService executor;
public ValidateVerticies(Set<Vertex> verticesAdded){
vertices = verticesAdded;
executor = Executors.newSingleThreadExecutor(); //Single just for testing purposes
}
public validate(){
for(Vertex vertex: vertices){
String prop = vertex.property("type");
if(type.equals("1"))
executor.submit(() -> validateRule1(vertex))
else if(type.equals("2"))
executor.submit(() -> validateRule2(vertex))
else if(type.equals("n"))
executor.submit(() -> validateRule3(vertex))
...
else if(type.equals("n"))
executor.submit(() -> validateRulen(vertex))
}
}
}
上面的代码在完全是单线程的情况下可以工作,但是一旦我引入了线程池,我就会遇到各种奇怪的错误。其中:
- 顶点属性
"type"不存在。 -
java.lang.IndexOutOfBoundsException: Index: 0, Size: -1尝试访问某些顶点属性时。 - 验证规则在最初通过时失败。
从 tinkergraph 进行多线程读取时有什么微妙之处吗?
编辑:
我会尽量简化问题: 以下作品:
public class ValidateVerticies(){
private Set<Vertex> vertices;
public ValidateVerticies(Set<Vertex> verticesAdded){
vertices = verticesAdded;
}
public validate(){
for(Vertex vertex: vertices){
String prop = vertex.property("type");
if(type.equals("1"))
validateRule1(vertex);
...
else if(type.equals("n"))
validateRulen(vertex);
}
}
}
虽然上面的多线程版本因 TinkerGraph 失败(同样适用于支持事务的 Titan)从图读取时返回不一致的结果。
【问题讨论】:
-
您的代码 sn-ps 无法明确您的事务边界在哪里,尤其是您提交事务的位置。你能补充一下信息吗?
-
TinkerGraph 不支持事务。虽然 TinkerGraph 不是正式的“线程安全”,但我对您会以使用 TinkerGraph 的方式看到这些问题感到半惊讶。我认为下面提到的 jason 需要更多信息。
-
我已经编辑了我的示例。我应该强调在不同线程中读取图表时会出现问题。我什至没有看到
commit声明。
标签: java multithreading tinkerpop tinkergraph