java - Tinkergraph的多线程阅读

标签 java multithreading tinkerpop tinkergraph

尝试从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/

    相关文章:

    Java嵌套ConcurrentHashMap是线程安全的吗?

    python-3.x - 亚马逊海王星 : How to create custom vertex id in Python using tinkerpop/gremlinpython package?

    java - 现代 Android 中的非合作 Thread.stop() 替代方案

    java - 如何在Java中实现自动更新?

    .net - Dapper QueryMultiple/Query/Execute 方法线程安全吗?

    azure - 查找与当前所有顶点相连的顶点

    titan - 如何在 TinkerPop 3 中找到所有没有传入边的顶点?

    java - AST 解析器获取 Java 类的名称

    java - 发送 HTTP POST 请求

    .net - 线程可以在应用程序终止后继续存在吗?