删除

时间:2015-06-01 19:27:01

标签: java concurrentmodification

通常当我看到ConcurrentModificationException时,它是由于迭代列表并删除某些内容。在我的情况下,我只是引用了列表,并试图从中删除一些东西。这是我的代码:

getList(Client.class).remove(client);

这会抛出ConcurrentModificationExceptionclassToListMap声明是这样的

private Map<Class, ObservableList> classToListMap;

这是getList()函数:

public <T> List<T> getList(Class T) {
    return classToListMap.get(T);
}

有关为什么会抛出异常的任何想法?

以下是抛出异常的完整函数:

private synchronized void deleteClient() {
    DefaultMutableTreeNode node = getRightClickedNode();
    if (node.getUserObject() instanceof Client == false) {
        JOptionPane.showMessageDialog(null, "Please select a client to delete");
        return;
    }
    TreeSelectionListener[] listeners = getTreeSelectionListeners();
    for(TreeSelectionListener listener : listeners){
        removeTreeSelectionListener(listener);
    }
    ((DefaultTreeModel)getModel()).removeNodeFromParent(node);
    Client client = (Client) node.getUserObject();
    applicationContext.getDataRepository().getList(Client.class).remove(client);  //!!!!!!<-- Throws exception
    applicationContext.fireUpdate(new UpdateEvent(UpdateEvent.Type.DELETE, client, new UnknownEvent(this)));
    for(TreeSelectionListener listener : listeners){
        addTreeSelectionListener(listener);
    }
}

这是完整的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.remove(AbstractList.java:380)
    at java.util.AbstractCollection.remove(AbstractCollection.java:293)
    at com.dvsd.profmetric.ui.gui.client.gui.ClientTree.deleteClient(ClientTree.java:147)
    at com.dvsd.profmetric.ui.gui.client.gui.ClientTree.access$300(ClientTree.java:39)
    at com.dvsd.profmetric.ui.gui.client.gui.ClientTree$3.actionPerformed(ClientTree.java:121)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6292)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4883)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

classToListMap正如此填补:

private void createClassToListMap() {
        classToListMap = new LinkedHashMap<Class, ObservableList>();

        classToListMap.put(ManagementProfessional.class, managementProfessionalList);
        classToListMap.put(AdministrativeProfessional.class, administrativeProfessionalList);
...

创建的ObservableLists org.jdesktop.observablecollections.ObservableList;如下:

List<ProfitMargin> profitMargins = simulation.getProfitMarginList();
profitMarginList = ObservableCollections.observableList(profitMargins);

ObservableCollectionsorg.jdesktop.observablecollections.ObservableCollections;

的位置

1 个答案:

答案 0 :(得分:3)

您尚未提供完整的信息,但我猜测Observable的{​​{1}}部分意味着列表内容的更改会报告给观察者,并且该观察者可能正在迭代列表或尝试在删除元素时触发的回调中进行自己的修改。如果您需要更多帮助,请发布完整的堆栈跟踪和有关列表观察者回调的信息。