I am looking for a good Java Graph Library which is thread safe for concurrent access.
JGraphT, JUNG, JTS are very good but again for concurrent access I will have to synchronize it externally which is becoming a pain.
It is a pain because say If thread A have to access 50 vertices, Thread B for another 50 with the intersection of vertices being 20 vertices. Now while writing code I need to know this 20 before so that I can synchronize it accordingly.
Pl suggest
I am looking for a good Java Graph Library which is thread safe for
Share
I’m afraid what you’re looking for is impossible, because thread-safety is a property of algorithms, not a property of data structures. Here’s an example:
Let’s say your graph library has a main
Graphclass with a number of methods, all of which aresynchronized. For example,addVertex(),removeVertex(),addEdge(),removeEdge(), etc. Let’s also say that theVertexclass has some useful methods likegetAdjacentEdges(), for example, also synchronized on the containingGraphinstance.Now clearly because everything is synchronized, it’s impossible to corrupt the data structure. For example, you’ll never have a situation where
v.getAdjacentEdges()gives you an edge that’s not actually in the graph containing vertexv. The graph structure is always internally consistent thanks to its internal synchronization.However, your algorithms operating on the graph can still easily break. For example, let’s say you write:
The call to
getAdjacentEdges()is atomic, as is each call toremoveEdge()in the loop, but the algorithm itself is not. Another thread may add a new edge adjacent tovwhile this loop is running, or remove an edge, or whatever. To be safe, you still need a way of ensuring that the loop as a whole is atomic, and the graph itself cannot provide that.My best advice, I think, is to use JGraphT in combination with Akka’s STM implementation (or similar), so that you can write your algorithms without needing to determine ahead of time which objects will require locking. If you’re not familiar with STM and its performance characteristics, Wikipedia’s article on the topic does a decent job of explaining.