尝试从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),但从图形读取时返回的结果不一致。
最佳答案
试试看。代替
String prop = vertex.property("type");
有了这个
String type = vertex.value("type");
前者返回
VertexProperty
,而后者返回VertexProperty的value
,这就是我认为您要查找的。至于#2和#3子弹,您需要提供更多详细信息。
关于java - Tinkergraph的多线程阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34951365/