dayscott dayscott - 1 year ago 105
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 ?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download