0

I have the following code:

static final Map<String, String> map = Collections.synchronizedMap(new HashMap());

    public static void main(String[] args) throws InterruptedException {
        map.put("1", "1");
        map.put("2", "2");
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
                    System.out.println("after iterator");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();

        Thread.sleep(50);
        map.remove("1");
        System.out.println("removed");
    }

It produces java.util.ConcurrentModificationException

How can I avoid it ?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • Make the run method synchronized.. or your can use concurrent hashmap . https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html – Pranav Maniar Mar 28 '16 at 14:15
  • Possible duplicate of [How to debug ConcurrentModificationException?](http://stackoverflow.com/questions/840165/how-to-debug-concurrentmodificationexception) – Raedwald Mar 28 '16 at 14:18
  • 1
    Looks like essentially the same as you previous question: http://stackoverflow.com/questions/36228084/map-concurrentmodificationexception-in-for-header – Raedwald Mar 28 '16 at 14:20
  • use `ConcurrentHashMap`. Iterators in `.synchronizedMap()` are not synchronized. – Alex Salauyou Mar 28 '16 at 14:26

3 Answers3

1

You cannot remove an element from a collection while iterating it, unless you use an Iterator. Use Iterator.remove() method

Dark Knight
  • 8,218
  • 4
  • 39
  • 58
1

you could use static final Map<String, String> map = new ConcurrentHashMap<>();

    static final Map<String, String> map = new ConcurrentHashMap<>();

public static void main(String[] args) throws InterruptedException {
    map.put("1", "1");
    map.put("2", "2");
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
                System.out.println("after iterator");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }).start();

    Thread.sleep(50);
    map.remove("1");
    System.out.println("removed");
}
Deendayal Garg
  • 5,030
  • 2
  • 19
  • 33
0

If you want to iterate over a Collections.synchronizedMap you must explicitly synchronize the iteration:

        synchronized (map) {
            for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
                System.out.println("after iterator");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
         }
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413