欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Caused by: java.util.ConcurrentModificationException

程序员文章站 2022-06-23 12:50:40
...

Caused by: java.util.ConcurrentModificationException

报错信息
Caused by: java.util.ConcurrentModificationException
	at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
	at java.util.AbstractList$Itr.next(AbstractList.java:343)
	at com.svw.bnk.peel.formal.FormalPeelUploadResultListAction.getDelpeel(FormalPeelUploadResultListAction.java:158)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
	at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
	at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
	at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
	at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
	at org.jboss.seam.core.SynchronizationInterceptor.aroundInvoke(SynchronizationInterceptor.java:32)
	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
	at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
	at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
	at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
	at com.svw.bnk.peel.formal.FormalPeelUploadResultListAction_$$_javassist_seam_11.getDelpeel(FormalPeelUploadResultListAction_$$_javassist_seam_11.java)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:335)
	at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:348)
	at org.jboss.el.parser.AstBracketSuffix.invoke(AstBracketSuffix.java:63)
	at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
	at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
	at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
	... 76 more

项目代码:

public void getDelpeel() {
        List<Long> list = getDataGridDataStore().getSelectedId();
        if (list != null) {
            this.formalPeelService.doDelpeel(list);
            addMessage(MessageUtil.getMessage("bnk.peel.formalPeel.delpeelSuccess"));
        }
        @SuppressWarnings("unchecked")
		List<FormalPeel> result = (List<FormalPeel>) this.getSession().getAttribute("fpList");
        for (Long selectedId : list) {
	        for (FormalPeel formalPeel : result) {
	        	if (formalPeel.getId().equals(selectedId)) {
	        		result.remove(formalPeel);
	        	}
	        }
        }
        this.getSession().removeAttribute("fpList");
        // 新的session
        this.getSession().setAttribute("fpList", result);
    }

导致报错代码如下:
Caused by: java.util.ConcurrentModificationException
原因:在循环删除时,迭代器的modCount(修改次数)和expectedModCount(期望修改次数)的值不一致。(具体可自行百度)
解决一:迭代器的remove方法中,有一行代码 expectedModCount = modCount; 可以保证在修改之后两个变量的值相等

public void getDelpeel() {
        List<Long> list = getDataGridDataStore().getSelectedId();
        if (list != null) {
            this.formalPeelService.doDelpeel(list);
            addMessage(MessageUtil.getMessage("bnk.peel.formalPeel.delpeelSuccess"));
        }
        @SuppressWarnings("unchecked")
		List<FormalPeel> result = (List<FormalPeel>) this.getSession().getAttribute("fpList");
        Iterator it = result.iterator();
        for (Long selectedId : list) {
            while (it.hasNext()){
                String s = it.next().toString();
                if (s.equals(selectedId)){
                    it.remove(); // 注意这里
                }
            }
        }
        this.getSession().removeAttribute("fpList");
        // 新的session
        this.getSession().setAttribute("fpList", IteratorUtils.toList(it));
    }

解决方法二:该方法是改成索引遍历,但是需要在删除之后保证索引的正常。

public void getDelpeel() {
        List<Long> list = getDataGridDataStore().getSelectedId();
        if (list != null) {
            this.formalPeelService.doDelpeel(list);
            addMessage(MessageUtil.getMessage("bnk.peel.formalPeel.delpeelSuccess"));
        }
        @SuppressWarnings("unchecked")
		List<FormalPeel> result = (List<FormalPeel>) this.getSession().getAttribute("fpList");
        for (Long selectedId : list) {
	        for (int i=0; i < result.size(); i++) {
	        	if (result.get(i).getId().equals(selectedId)) {
	        		result.remove(i);
	        	}
	        }
        }
        this.getSession().removeAttribute("fpList");
        // 新的session
        this.getSession().setAttribute("fpList", result);
    }
相关标签: Java