user3715654
user3715654

Reputation: 41

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);
  }
 }

Upvotes: 1

Views: 149

Answers (2)

Faizal
Faizal

Reputation: 33

You can try using Collections.synchronizedList(subscribers) which may help avoiding this problem.

Upvotes: 0

Clive Evans
Clive Evans

Reputation: 658

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.

Upvotes: 2

Related Questions