dayscott dayscott - 3 months ago 6x
Java Question

ConcurrentModificationException despite using synchronized

public synchronized X getAnotherX(){
X b =;
String name = b.getInputFileName();
return b;
else{return null;}

despite the synchronized statement in the declaration header, i still get a ConcurrentModificationException Exception at the line where i use; whats wrong here ?


ConcurrentModificationException usually has nothing to do with multiple threads. Most of the time it occurs because you are modifying the collection over which it is iterating within the body of the iteration loop. For example, this will cause it:

Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
    Item item = (Item);
    if (item.satisfiesCondition()) {

In this case you must use the iterator.remove() method instead. This occurs equally if you are adding to the collection, in which case there is no general solution. However, the subtype ListIterator can be used if dealing with a list and this has an add() method.