日期:2014-05-20  浏览次数:20867 次

对一集合进行添加和删除操作
Java code

//我要完成的功能如下面代码所示,请教,此功能该如何实现
Iterator it = list.iterator();
while(true){

while(it.hasNext()){
  Object obj = it.next();//当我进行遍历的时候,另一程序对list添加数据,这句话就报错了,报错信息如下
  //TODO 这里处理obj
  it.remove();
}

sleep(20)
}


报错信息
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
  at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
  at java.util.AbstractList$Itr.next(AbstractList.java:343)
  at desktopapplication2.collection.server.SaveData.run(SaveData.java:32)
  at desktopapplication2.collection.util.SocketSwitch.start(SocketSwitch.java:65)
  at desktopapplication2.DesktopApplication2View.jButton1ActionPerformed(DesktopApplication2View.java:619)
  at desktopapplication2.DesktopApplication2View.access$400(DesktopApplication2View.java:26)
  at desktopapplication2.DesktopApplication2View$4.actionPerformed(DesktopApplication2View.java:377)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
  at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
  at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
  at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
  at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
  at java.awt.Component.processMouseEvent(Component.java:6134)
  at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
  at java.awt.Component.processEvent(Component.java:5899)
  at java.awt.Container.processEvent(Container.java:2023)
  at java.awt.Component.dispatchEventImpl(Component.java:4501)
  at java.awt.Container.dispatchEventImpl(Container.java:2081)
  at java.awt.Component.dispatchEvent(Component.java:4331)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
  at java.awt.Container.dispatchEventImpl(Container.java:2067)
  at java.awt.Window.dispatchEventImpl(Window.java:2458)
  at java.awt.Component.dispatchEvent(Component.java:4331)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)





------解决方案--------------------
1 使用 CopyOnWriteArrayList 
2 lock
------解决方案--------------------
把你list类型都换成CopyOnWriteArrayList
------解决方案--------------------
Java code

List list = new CopyOnWriteArrayList();
for(Iterator iter = list.iterator(); iter.hasNext();) {
    Object obj = iter.next();
    ...
    if(***) {
        list.remove(obj);
    }
}

------解决方案--------------------