解决ConcurrentModificationException
来源:优易学  2011-2-17 13:33:00   【优易学:中国教育考试门户网】   资料下载   IT书店
  原因:一个线程在用迭代器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);}

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训