gremlintinkerpoptinkerpop3tinkergraph

Should I declare GryoMapper as a static field?


Looked at the following code, it appears to be thread-safe.

https://github.com/apache/tinkerpop/blob/master/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java

Hoping to use it like

class Foo {
    private static final GryoMapper MAPPER = GryoMapper.build().create();
}

instead of


class Foo {
    private final GryoMapper MAPPER = GryoMapper.build().create();
}



Solution

  • Gryo is based on Kryo which is not thread-safe. GryoMapper is basically just a builder for Kryo instances which means that you should be able to initialize it as a member variable without the static declaration. Just be sure that the Kryo instances that you spawn from GryoMapper are not accessed by multiple threads concurrently as described in the Kryo link provided.