0

I am reading Java Concurrency in Practice, according to some java code in it, System.out.println() will led to ConcurrentModificationException. The code is below :

private final Set<Integer> set = new HashSet<Integer>();

public synchronized void add(Integer i) {set.add(i); }

public synchronized void remove(Integer i) {set.remove(i);}

public void addTenThings() {

    Random r = new Random();
    for (int i = 0; i < 10; i++) {
        add(r.nextInt());
    }

    System.out.println("DEBUG: add ten elements to " + set );
}

Since the System.out.println() method will just call the toString method:

public String toString() {
    Iterator<E> i = iterator();
if (! i.hasNext())
    return "[]";

StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
    E e = i.next();
    sb.append(e == this ? "(this Collection)" : e);
    if (! i.hasNext())
    return sb.append(']').toString();
    sb.append(", ");
}
}

I still can not understand why ConcurrentModificationException be throw??

znlyj
  • 1,109
  • 3
  • 14
  • 34
  • You are iterating over the collection while another thread could be modifying it. Your `" + set` calls toString() on the `set` object. – Peter Lawrey Oct 27 '13 at 11:12

1 Answers1

1

Suppose 2 threads - A and B executing the method addTenThings() at the same time. They can do it, as the method isn't synchronized.

Then if during the time the thread A is executing toString() method of set, thus iterating over it, the thread B is still executing the loop, and invokes the add() method, that might cause ConcurrentModificationException, as both the threads are operating on the same set only. Nothing stops a thread to execute the add(r.nextInt()) statement, while a different thread is executing the print statement in the method.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • I did this for server times as you said, but nothing happened, did I miss something? – znlyj Oct 27 '13 at 10:35
  • @znlyj You could do it a billion times and not see this bug on one machine but when you ran it on a different machine, JVM version or OS you might see it all the time. 10 things is very small so one thread can run to completion before the other one starts. – Peter Lawrey Oct 27 '13 at 11:14
  • Got it. Both **add** and **remove** method might throw the exception when these modify operation and iterate happened at the same time. – znlyj Oct 28 '13 at 14:35