如何调试 ConcurrentModificationException? [英] How to debug ConcurrentModificationException?
问题描述
我遇到了 ConcurrentModificationException 并且通过查看它我看不出它发生的原因;抛出异常的区域和所有修改集合的地方都被
I encountered ConcurrentModificationException and by looking at it I can't see the reason why it's happening; the area throwing the exception and all the places modifying the collection are surrounded by
synchronized (this.locks.get(id)) {
...
} // locks is a HashMap<String, Object>;
我试图捕获讨厌的线程,但我所能确定的(通过在异常中设置断点)是抛出线程拥有监视器,而另一个线程(程序中有两个线程)休眠.
I tried to catch the the pesky thread but all I could nail (by setting a breakpoint in the exception) is that the throwing thread owns the monitor while the other thread (there are two threads in the program) sleeps.
我应该如何进行?当您遇到类似的线程问题时,您通常会怎么做?
How should I proceed? What do you usually do when you encounter similar threading issues?
推荐答案
可能与同步块无关.ConcurrentModificationException
经常发生在您在迭代集合元素时修改集合.
It may have nothing to do with the synchronization block. ConcurrentModificationException
s often occur when you're modifying a collection while you are iterating over its elements.
List<String> messages = ...;
for (String message : messages) {
// Prone to ConcurrentModificationException
messages.add("A COMPLETELY NEW MESSAGE");
}
这篇关于如何调试 ConcurrentModificationException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!