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
 

huckleberry

Bekanntes Mitglied
Gelöst, aber keine Ahnung wieso?

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

huckleberry

Bekanntes Mitglied

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;
 

Marco13

Top Contributor
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: )
 

Marco13

Top Contributor
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:
 

Schumi

Bekanntes Mitglied
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:
Ähnliche Java Themen
  Titel Forum Antworten Datum
B Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException Java Basics - Anfänger-Themen 8
temi ConcurrentModificationException Java Basics - Anfänger-Themen 32
C ConcurrentModificationException Java Basics - Anfänger-Themen 3
K Collections ConcurrentModificationException Java Basics - Anfänger-Themen 3
F Collections ConcurrentModificationException in ArrayList, mehrere Threads Java Basics - Anfänger-Themen 7
S ConcurrentModificationException Java Basics - Anfänger-Themen 4
H ConcurrentModificationException in HashMap Java Basics - Anfänger-Themen 2
Appleleptiker Datentypen ConcurrentModificationException Java Basics - Anfänger-Themen 5
Luk10 ConcurrentModificationException Java Basics - Anfänger-Themen 35
Luk10 ConcurrentModificationException Java Basics - Anfänger-Themen 15
K java.util.ConcurrentModificationException problem in der Logik? Quaxli-Tutorial Java Basics - Anfänger-Themen 9
T ConcurrentModificationException bei HashMap Operation Java Basics - Anfänger-Themen 2
E java.util.ConcurrentModificationException Problem Java Basics - Anfänger-Themen 5
F java.util.ConcurrentModificationException Java Basics - Anfänger-Themen 8
S ArrayList ConcurrentModificationException Java Basics - Anfänger-Themen 4
J ConcurrentModificationException finden Java Basics - Anfänger-Themen 2
cowabunga1984 Objek löschen -> ConcurrentModificationException Java Basics - Anfänger-Themen 7
N removeAll und ConcurrentModificationException Java Basics - Anfänger-Themen 2

Ähnliche Java Themen

Neue Themen


Oben