Multithreaded modified ArrayList, how do you know who modified it?

public void method(){
    ...
    for(Object obj: finalObjects){
        ....
        doSth(obj);
        ....
    }
}

when I was traversing ArrayList in the main thread, java.util.ConcurrentModificationException

occurred.

has checked doSth and did not modify the global variable finalObjects , so I suspect that some asynchronous method modified this variable during my traversal. Because the system is so large, it is impossible to directly find out where it has been modified. Is there any way to get it at run time, where the async is, or which thread modifies finalObjects to cause the exception?

ask the bosses, thank you!


to solve this problem, LZ can delve into the cause of ConcurrentModificationException : the enhanced for loop of
java uses the hasNext and next of the iterator iterator by default to traverse the elements. In this way, it is not difficult to understand the cause of the problem.
you execute the remove or add method elsewhere. This method only modifies the modCount value, not the expectedModCount content, so the check throws an exception

when the iterator calls next .
  

Thank you very much for your answers. Just solve the problem, such as @ kevinz, and it will be fine.
but my goal is to track which thread modified this variable during the execution of this piece of code. But it seems very difficult. I don't have a clue.

Menu