如何调试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屋!