我有下面的代碼崩潰:爪哇 - 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