Search code examples
javasynchronizationconcurrentmodification

Spurious ConcurrentModificationException


I'm having some issues with a piece of java code which keeps triggering a ConcurrentModificationException. I can't really figure out what is going on, this is a fairly simple static class, not sure why it would be throwing this exception as everything is synchronized. This piece of code has been heavily used for several years, so it's odd that it would start having issues at this point:

java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:953)
        at java.util.LinkedList$ListItr.next(LinkedList.java:886)
        at DataSubscriptionManager.sendMessages(DataSubscriptionManager.java:18)


private static HashMap<DataClass,LinkedList<DataSubscriber>> subscriberMap = new HashMap();

 public static void sendMessages(LinkedList messages, DataClass dataClass) {
  synchronized (subscriberMap) {
   LinkedList<DataSubscriber> subscribers = subscriberMap.get(dataClass);
   if (subscribers != null) {
    for (DataSubscriber sub: subscribers) { *** EXCEPTION HAPPENS HERE***
     if (sub != null) {
      sub.sendMessages(messages);
     }
    }
   }
  }
 }


 public static void addDataSubscriber(DataSubscriber sub, DataClass dataClass) {
  synchronized (subscriberMap) {
   LinkedList<DataSubscriber> subscribers = subscriberMap.get(dataClass);
   if (subscribers == null) {
    subscribers = new LinkedList();
    subscriberMap.put(dataClass,subscribers);
   }
   while (subscribers.remove(sub)) {}
   subscribers.add(sub);
  }
 }

 public static void removeDataSubscriber(DataSubscriber sub, DataClass dataClass) {
  synchronized (subscriberMap) {
   LinkedList<DataSubscriber> subscribers = subscriberMap.get(dataClass);

   subscribers.remove(sub);
  }
 }

Solution

  • What's happening is that your collection is being modified while you are iterating over it.

    It's could be another thread, or it's possible one of your subscribers is either unsubscribing, or subscribing to a different dataClass in response to the message it receives.