1

I have method which has iterator. it sometimes give me ConcurrentModificationException. can anyone suggest me a better solution for this. This is my code

private void addRequest(HttpUriRequest request) {
    Iterator<Entry<String, List<ComBase.OnReceiveListener<Entity>>>> iterator = ((Comm) _com)
        .getReceiveListeners().entryset().iterator();

    synchronized (iterator) {
        StringBuilder builder = new StringBuilder();
        boolean first = true;

        while (iterator.hasNext()) {
            Entry<String, List<ComBase.OnReceiveListener<Entity>>> entry = iterator.next();

            if (entry.getValue() != null && entry.getValue().size() > 0) {

                if (first) {
                    first = false;
                } else {
                    builder.append(", ");
                }
                builder.append(entry.getKey());
            }
        }
        request.setHeader(HttpHeaders.XAcceptEntities, builder.toString());
    }
}

This is how my stack-race looks

02-24 16:22:38.601: E/Application(11567):   at java.util.HashMap$EntryIterator.next(HashMap.java:827)
02-24 16:22:38.601: E/Application(11567):   at org.example.abc.com.HttpTransport.addRequest(HttpTransport.java:222)
02-24 16:22:38.601: E/Application(11567):   at org.example.abc.com.HttpTransport.createOperation(HttpTransport.java:161)
02-24 16:22:38.601: E/Application(11567):   at org.example.abc.com.HttpTransport.orgndMessage(HttpTransport.java:109)
02-24 16:22:38.601: E/Application(11567):   at org.example.abc.com.MessagePoller.queryMessage(QueueMessagePoller.java:36)
02-24 16:22:38.601: E/Application(11567):   at org.example.abc.com.MessagePoller.worker(MessagePoller.java:23)
02-24 16:22:38.601: E/Application(11567):   at org.example.abc.com.MessagePoller.access$1(MessagePoller.java:14)
02-24 16:22:38.601: E/Application(11567):   at org.example.abc.com.MessagePoller$2.run(Poller.java:21)
02-24 16:22:38.601: E/Application(11567):   at java.lang.Thread.run(Thread.java:856)
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • Possible duplicate of [Iterating through a list, avoiding ConcurrentModificationException when removing in loop](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing) – seenukarthi Feb 25 '16 at 04:07
  • Why is every `se` replaced by `org`? – Andreas Feb 25 '16 at 04:07
  • 1
    I don't see anything in the loop that would cause `ConcurrentModificationException`, which means that you seem to have a true concurrency issue, i.e. the map returned by `getReceiveListeners()` is being modified by another thread at the same time. – Andreas Feb 25 '16 at 04:10
  • does it solve if I use ConcurrentHashMap ? – user3565768 Feb 25 '16 at 05:05
  • make the method addRequest() synchronized instead of having a synchronized block – Phanindra Gopishetty Feb 25 '16 at 05:55
  • I added synchronized to addRequest() but it didn't work. still give same exception – user3565768 Feb 25 '16 at 06:41
  • You need to figure out what else is modifying the map. If you don't know, it's likely you're not using the map as intended. – dimo414 Nov 12 '16 at 02:16

0 Answers0