我有下面的代码崩溃:爪哇 - ConcurrentModificationException的多线程程序,设置是包裹着同步
private LocalBundle(Bundle b, Map<Bundle, LocalBundle> clonnedObjs) {
this();
synchronized (b.getVariables()) { // b.getVariables() is Set
addFrom(b, clonnedObjs);
}
}
但addFrom我得到的崩溃:
private synchronized void addFrom(Bundle b, Map<Bundle, LocalBundle> clonnedObjs) {
Set<TaintedVariable> variablesOfBundle = b.getVariables();
for(TaintedVariable v : variablesOfBundle) {
异常消息:
Exception in thread "pool-4-thread-1" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(Unknown Source)
at java.util.HashMap$KeyIterator.next(Unknown Source)
at x.y.z.LocalBundle.addFrom(VisitedNodesWithBundle.java:198)
有人知道它为什么会发生?我用synchronized (b.getVariables())
包装,但它看起来像两个线程正在执行for(TaintedVariable v : variablesOfBundle)
请在增强for循环内显示代码。 –
“它看起来像两个线程正在执行”不一定。您可以使用单个线程获得'ConcurrentModificationException'。 –
虽然使用同步,但可能有[ConcurrentModificationException]重复(http://stackoverflow.com/questions/1655362/concurrentmodificationexception-despite-using-synchronized) – Raedwald