2016-01-22 64 views
2

尝试从修补程序段中读取多线程时,出现非常奇怪的行为。我有以下情况:从修补程序段中读取多线程

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

上面的代码工作时,它完全是单线程,但一旦我公司推出的线程池我得到一个奇怪的各种各样的错误。包括:

  1. 顶点属性"type"不存在。
  2. java.lang.IndexOutOfBoundsException: Index: 0, Size: -1当试图访问某些顶点属性。
  3. 验证规则最初通过时失败。

从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(同样适用于泰坦支持交易)的回报从图形中读取时结果不一致。

+0

您的代码片段没有清楚说明您的交易界限在哪里,特别是在您提交交易的地方。你能否添加信息? – Ralf

+0

TinkerGraph不支持事务。虽然TinkerGraph没有正式的“线程安全”,但我感到半惊讶的是,您会发现TinkerGraph的这些问题与您使用它的方式有关。我认为更多的信息是必需的,正如杰森所暗示的。 –

+0

我编辑了我的示例。我应该强调,在不同线程中读取图表时会出现这些问题。我甚至都没有看到'commit'语句。 –

回答

0

试试看。更换

String prop = vertex.property("type"); 

与此

String type = vertex.value("type"); 

前者返回一个VertexProperty而后者返回VertexProperty,这是我认为你正在寻找的value

至于你的#2和#3子弹,你需要提供更多的细节。

+0

该更改无法解决问题。一切工作都是单线程的,但是当我添加线程池时,就会出现这些问题。 –