2

我正在开发一个 Tree 组件,它可能会根据用户输入进行修剪。我缓存了所有节点,以便为 primefaces 组件模型提供始终相同的树节点实例。

但问题是,当我更改父子关系(删除其中一些)时,我总是会得到一个并发修改异常......有没有办法解决这个问题或者我错过了什么。

提前致谢。

/**
 * Recursively copy the hierarchy and prune not visible branches
 * @param originalNode node of the original hierarchy to be copied
 * @param parent parent node in the new hierarchy
 * @param visibleNodes set of nodes, which should be included in the new hierarchy
 * @param model model of the TreeAutompleteComponent
 * @return copy of the oroginal node in the new hierarchy
 */
private TreeNode copyHierarchy(TreeNode originalNode, TreeNode parent, Set<TreeNode> visibleNodes, TreeAutocompleteModel model) {
    if (!visibleNodes.contains(originalNode)) {
        return null;
    }

    TreeNode newNode = model.getCompleteModelNodes().get(((NodeWrapper)originalNode.getData()).getIri());//new DefaultTreeNode(originalNode.getData(), parent);
    for (int i = 0; i < originalNode.getChildCount(); i++) {
        copyHierarchy((TreeNode) originalNode.getChildren().get(i), newNode, visibleNodes, model);
    }
    newNode.setParent(parent); //when removed, no exception occurs
    return newNode;
}

例外

ava.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at org.primefaces.component.tree.TreeRenderer.encodeTreeNodeChildren(TreeRenderer.java:284)
    at org.primefaces.component.tree.TreeRenderer.encodeTreeNode(TreeRenderer.java:274)
    at org.primefaces.component.tree.TreeRenderer.encodeMarkup(TreeRenderer.java:165)
    at org.primefaces.component.tree.TreeRenderer.encodeEnd(TreeRenderer.java:96)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at com.sun.faces.renderkit.html_basic.CompositeRenderer.encodeChildren(CompositeRenderer.java:78)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:424)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
4

2 回答 2

2

您正在尝试修改您当前正在迭代的集合。
您是否尝试过处理相关集合的副本而不是修改当前集合?

来自http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

请注意,此异常并不总是表示对象已被不同的线程同时修改。如果单个线程发出一系列违反对象约定的方法调用,则该对象可能会抛出此异常。例如,如果线程在使用快速失败迭代器迭代集合时直接修改了集合,则迭代器将抛出此异常。

在您的递归函数中,您有以下场景:

在您来到树的叶子之前的 1 步:
假设您有一个父节点“5”和一个唯一的子(叶子)节点“11”。(“11”有“5”作为其父级)您开始迭代子级(“11”),并在他身上调用 copyHierarchy。
由于他没有孩子,因此跳过了循环,他的父母变成了 newNode,例如“17”。
然后你返回一个级别。
您应该迭代以“5”为父节点的节点,但节点“11”实际上已将其父节点从“5”更改为“17”。

于 2012-07-18T11:23:54.400 回答
1

这是解决方案,有效。我还没有本地化实际问题,但我已经成功地解决了所有问题......

public TreeNode constructActualModel(TreeAutocompleteMatcherIface matcher) {
    if (prunedModel == null) {
        eraseHierarchyStructure();
        prunedModel = copyHierarchy((TreeNode) completeModel, getVisibleNodes(matcher));
    }
    return prunedModel;
}

/**
 * Recursively copy the hierarchy and prune not visible branches
 * @param originalNode node of the original hierarchy to be copied
 * @param visibleNodes set of nodes, which should be included in the new hierarchy
 * @param model model of the TreeAutompleteComponent
 * @return copy of the oroginal node in the new hierarchy
 */
private TreeNode copyHierarchy(TreeNode originalNode, Set<TreeNode> visibleNodes) {
    if (!visibleNodes.contains(originalNode)) {
        return null;
    }

    TreeNode newNode = this.getCompleteModelNodes().get(((NodeWrapper) originalNode.getData()).getIri());//new DefaultTreeNode(originalNode.getData(), parent);
    List<TreeNode> children = new ArrayList<TreeNode>();
    for (int i = 0; i < originalNode.getChildCount(); i++) {
        TreeNode child = copyHierarchy((TreeNode) originalNode.getChildren().get(i), visibleNodes);
        if(child != null){
            children.add(child);
        }
    }
    ((DefaultTreeNode) newNode).setChildren(children);
    return newNode;
}

private void eraseHierarchyStructure() {
    for (TreeNode node : duplicateCompleteModelNodes.values()) {
        node.setParent(null);
    }       
}

有必要以相反的方式进行 - 让 impl 能够调用 setChildren 方法并首先删除层次结构......

于 2012-07-18T12:59:21.840 回答