ConcurrentModificationException

huckleberry

Bekanntes Mitglied
Hallo Leute,

ich bekomme eine java.util.ConcurrentModificationException.

Java:
HashMap<String, Buch> tmpRegal = new HashMap<String, Buch>(regal);
Iterator<Buch> myIt = tmpRegal.values().iterator();
// Iterator<Buch> myIt = tmpRegal.keySet().iterator(); WÄRE MIR LIEBER ÜBER keys zu iterieren
// BLABLA
if (tmpRegal.size() > anotherLib.size()) {
	while (myIt.hasNext()) {
		String strC = myIt.next().getISBN(); // damit ich hier String strC = myIt.next(); mache
		log.info("\t\t\t[ISBN] Check: "+strC);
		if (!anotherLib.containsKey(strC)){
			log.info("\t\t\t[defaultCAM] Request PK from: "+strC);
			doRequestBook(strC);
		}
		tmpRegal.values().remove(strC);
	}
}

Kann jemand sagen wieso ich diese Exception bekomme?

Ich danke für Hinweise.. Gruss Huck
 
Gelöst, aber keine Ahnung wieso?

Java:
HashMap<String, Buch> tmpRegal = regal;
Statt wie oben. Weis jemand wieso? Ich danke.
 

regal ist mein eigentliches Objekt, da in meinen Berechnungen was entfernt wird, wollte ich es irgendwo hin kopieren (tmpRegal) und mit dieser Kopie weiterrechnen, sodass mein original unverändert bleibt.
Also ist tmpRegal == regal.

Java:
HashMap<String, Buch> tmpRegal = new HashMap<String, Buch>(regal);
HashMap<String, Buch> tmpRegal = regal;
 
Ja, mit
HashMap<String, Buch> tmpRegal = new HashMap<String, Buch>(regal);
KÖNNTE es funktionieren, mit
HashMap<String, Buch> tmpRegal = regal;
eher weniger. (Weiter oben klingt es aber, als wäre es bei dir umgekehrt... :bahnhof: )
 
Irritierend ... und dass sowas wie [c]tmpRegal.values().remove(strC);[/c] funktionieren soll, leuchtet mir auch nicht ganz ein, aber ... aber wenn's jetzt geht, ist's ja OK... :bahnhof:
 
Hast Du denn mal geschaut, was die Exception Dir sagt?
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
...
Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

Einfach nicht so gut zu löschen aus einer Map über die man gerade iteriert.

Lieber den Iterator nutzen:
remove()
Removes from the underlying collection the last element returned by the iterator (optional operation).

Eta: Noch mehr Infos aus der HashMap:
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
 
Zuletzt bearbeitet:

Zurück
Oben