I have been completely puzzled by an issue I just came over in my Java application.
When I attempt to run the code given below, Java raises a ConcurrentModificationException on the line that says "if ( this.vertexStore.get ( v ).addAll ( output ) )".
I find this very strange considering this a completely single-threaded application, and that I am not actually modifying anything that I am looping over as far as I can tell?
In fact, the only place I can see the error occurring is inside the addAll method, but that shouldn't happen either as I'm using HashMap and LinkedList from the Java class library...
private Queue<Vertex> worklist = new LinkedList<Vertex> ( );
protected Map<Vertex, Set<T>> vertexStore = new HashMap<Vertex, Set<T>> ( );
// . . .
while ( this.worklist.size ( ) > 0 ) {
Vertex vertex = this.worklist.remove ( );
Set<T> output = this.processVertice ( vertex, this.vertexStore.get ( vertex ) );
this.vertexStore.put ( vertex, output );
for ( Vertex v : vertex.edgesTo ( ) ) {
// Conveniently, addAll returns true if the set changed
if ( this.vertexStore.get ( v ).addAll ( output ) )
this.worklist.add ( v );
}
}
EDIT: Error trace:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
at DataFlowAnalyser.process(DataFlowAnalyser.java:41) (the if line)
Any good ideas are very welcome!
PS: Full source here (sorry for lack of comments, code not completed)
Cheers, Jon