user1375155 user1375155 - 8 months ago 43
Java Question

Java: concurrent modification exception

I am getting an error in the for(Entry...) loop where after calling dfs(), it will say concurrentmodificationexception. I don't know why it is happening even though visitedOrder is not related with the foreach loop. How can this be fixed?

public TreeMap<Integer, Integer> DFS()
TreeMap<Integer, Integer> stack = new TreeMap<Integer, Integer>();
TreeMap<Integer, Integer> visitedOrder = stack;
for(int i = 1; i < graph[0].length-1; i++)
stack.put(i, 0);
for(Entry<Integer, Integer> vertex : stack.entrySet())
if(vertex.getValue() == 0)
dfs(vertex.getKey(), visitedOrder);
return visitedOrder;

public void dfs(int vertex, TreeMap<Integer, Integer> visited)
visited.put(vertex, order++);
int currVertex = vertex;
for(int i = vertex; i < graph[0].length-1;i++)
if(graph[vertex][i+1] == 1)
dfs(++currVertex, visited);

Answer Source

Here is the Javadoc for "Class ConcurrentModificationException":

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

As it happens, that's precisely what you're doing: modifying the very structure you're using in your "foreach" loop.


If you believe your design is correct, then substitute a simple for loop: for (int i=0; i < myContainer.size(); i++) ...