原因:一个线程在用迭代器iterator的同时,另一个线程又在修改list数据
一种解决办法是
synchronize all access to the List, using the list itself as the monitor
code:
synchronized (list) {
for (Iterator it = list.iterator(); it.hashNext(); ) {
Foo f = (Foo) it.next();
// do what you need with f
}
}
Whatever other threads are modifying the list, they need to be synchronized too, also on the list. E.g.:
code:
syncronized (list) {
list.add(new Foo());
}
or
code:
syncronized (list) {
list.remove(oldFoo);
}