原因:一個(gè)線程在用迭代器iterator的同時(shí),另一個(gè)線程又在修改list數(shù)據(jù)
一種解決辦法是
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);}
一種解決辦法是
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);}