Why concurrentmodificationexception when print a set with System.out.println()?

有些话、适合烂在心里 提交于 2019-12-13 21:12:47

问题


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??


回答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.



来源:https://stackoverflow.com/questions/19616864/why-concurrentmodificationexception-when-print-a-set-with-system-out-println

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!