[英]Why concurrentmodificationexception when print a set with System.out.println()?
I am reading Java Concurrency in Practice , according to some java code in it, System.out.println()
will led to ConcurrentModificationException
. 我正在实践中阅读Java Concurrency ,根据其中的一些Java代码, System.out.println()
将导致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: 由于System.out.println()
方法将仅调用toString
方法:
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?? 我仍然不明白为什么会抛出ConcurrentModificationException
?
Suppose 2 threads - A
and B
executing the method addTenThings()
at the same time. 假设2个线程A
和B
执行方法addTenThings()
。 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. 然后,如果在线程A
执行set
toString()
方法的过程中,从而对其进行迭代,则线程B
仍在执行循环,并调用add()
方法,这可能导致ConcurrentModificationException
,因为两个线程都在运行仅在同一set
。 Nothing stops a thread to execute the add(r.nextInt())
statement, while a different thread is executing the print
statement in the method. 什么都不会停止线程来执行add(r.nextInt())
语句,而另一个线程正在执行方法中的print
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.