带有for循环的ConcurrentModificationException

时间:2014-05-24 12:14:43

标签: java exception java.util.concurrent concurrentmodification

我的for循环得到一个ConcurrentModificationException,我不知道如何解决这个问题。

这是我的代码:

for(Map.Entry<String,ControlFieldVO> entry:oldPresentationMap.entrySet()) {
                                    if(!keyValue.contains(entry.getKey())) {
                                        oldPresentationMap.remove(entry.getKey());
}

在第一行获取concurrentModification异常请帮助以下是堆栈跟踪:

<<<[2014/05/24 17:29:03.595]:WebContainer : 10:1400932743595:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:isParameterNotNull::Paremeter :nullControlFieldVO :com.td.mbnasales.core.jackrabbit.vo.PresentationImageVO@5f415f41[sameParentImgName=<null>,imgName=<null>,value=,isParentValueOverwritten=false,isErrorInValue=<null>,isValueDiffFromPublishedVersion=false,sameParentValue=,publishedValue=<null>,isMultiEditFieldValueDiff=false,isChanged=<null>]  >>>
<<<[2014/05/24 17:29:03.595]:WebContainer : 10:1400932743595:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:isParameterNotNull::Paremeter :nullControlFieldVO :com.td.mbnasales.core.jackrabbit.vo.PresentationImageVO@5f205f20[sameParentImgName=<null>,imgName=<null>,value=<null>,isParentValueOverwritten=false,isErrorInValue=<null>,isValueDiffFromPublishedVersion=false,sameParentValue=<null>,publishedValue=<null>,isMultiEditFieldValueDiff=<null>,isChanged=<null>]  >>>
<<<[2014/05/24 17:29:03.600]:WebContainer : 10:1400932743600:java.lang.String:    ERROR:c7WtHlNAp7cfpveDNFSG8Iq:resolveException::Exception Occured:java.util.ConcurrentModificationException  >>>
<<<[2014/05/24 17:29:03.603]:WebContainer : 10:1400932743603:java.lang.String:    ERROR:c7WtHlNAp7cfpveDNFSG8Iq:resolveException::Stack Trace :java.util.ConcurrentModificationException
    at java.util.LinkedHashMap$AbstractMapIterator.checkConcurrentMod(Unknown Source)
    at java.util.LinkedHashMap$AbstractMapIterator.makeNext(Unknown Source)
    at java.util.LinkedHashMap$EntryIterator.next(Unknown Source)
    at java.util.LinkedHashMap$EntryIterator.next(Unknown Source)
    at com.td.mbnasales.wwwcms.controller.helper.EntityHelper.mergePresentationSessionMultiEntityVO(EntityHelper.java:5849)
    at com.td.mbnasales.wwwcms.controller.helper.EntityHelper.multiUpdateSessionEntityVO(EntityHelper.java:6010)
    at com.td.mbnasales.wwwcms.controller.EntityController.multiUpdateSessionEntityVO(EntityController.java:5999)
    at sun.reflect.GeneratedMethodAccessor9348.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:599)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1597)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:131)
    at com.td.mbnasales.wwwcms.filters.AccessControlFilter.doFilter(AccessControlFilter.java:77)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at com.td.mbnasales.wwwcms.filters.BreadCrumbFilter.doFilter(BreadCrumbFilter.java:336)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at com.td.mbnasales.wwwcms.filters.LocaleFilter.doFilterInternal(LocaleFilter.java:110)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:322)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:184)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:155)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:908)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:934)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
    at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:864)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1613)
  >>>
<<<[2014/05/24 17:29:25.392]:WebContainer : 10:1400932765392:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:validateLocalParam -->::called  >>>
<<<[2014/05/24 17:29:25.393]:WebContainer : 10:1400932765393:com.td.mbnasales.core.helper.BreadCrumbHelper:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:getBreadCrumbDetails::---- Enter ---   >>>
<<<[2014/05/24 17:29:25.393]:WebContainer : 10:1400932765393:com.td.mbnasales.core.helper.BreadCrumbHelper:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:getBreadCrumbDetails:: --- Exit ---   >>>
<<<[2014/05/24 17:29:25.485]:WebContainer : 10:1400932765485:com.td.mbnasales.core.service.impl.MarketingZoneServiceImpl:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:getContent::---- Enter ---   >>>
<<<[2014/05/24 17:29:25.485]:WebContainer : 10:1400932765485:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:get::---- Enter ---   >>>
<<<[2014/05/24 17:29:25.485]:WebContainer : 10:1400932765485:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:New JackRabbit Session created for the user::JackRabbitManagerImpl  >>>
<<<[2014/05/24 17:29:25.486]:WebContainer : 10:1400932765486:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:     PERF:c7WtHlNAp7cfpveDNFSG8Iq::TIME:<TIMESTAMP> (0) 2014/05/24 17:29:25:485 to 17:29:25:486 1 ms  Taken for getOCMnullnull
  >>>
<<<[2014/05/24 17:29:25.744]:WebContainer : 10:1400932765744:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:     PERF:c7WtHlNAp7cfpveDNFSG8Iq:CECOM:TIME:<TIMESTAMP> (0) 2014/05/24 17:29:25:485 to 17:29:25:743 258 ms  Taken for get(ContentItem contentItem,ContentItemAttribute contentItemAttribute)null - CECOM
  >>>
<<<[2014/05/24 17:29:25.744]:WebContainer : 10:1400932765744:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:get:: --- Exit ---   >>>
<<<[2014/05/24 17:29:25.745]:WebContainer : 10:1400932765745:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:getPublishedVersion::---- Enter ---   >>>
<<<[2014/05/24 17:29:25.745]:WebContainer : 10:1400932765745:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:New JackRabbit Session created for the user::JackRabbitManagerImpl  >>>
<<<[2014/05/24 17:29:25.745]:WebContainer : 10:1400932765745:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:     PERF:c7WtHlNAp7cfpveDNFSG8Iq::TIME:<TIMESTAMP> (0) 2014/05/24 17:29:25:745 to 17:29:25:745 0 ms  Taken for getOCMnullnull
  >>>
<<<[2014/05/24 17:29:25.948]:WebContainer : 10:1400932765948:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:New JackRabbit Session created for the user::JackRabbitManagerImpl  >>>
<<<[2014/05/24 17:29:25.949]:WebContainer : 10:1400932765949:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:     PERF:c7WtHlNAp7cfpveDNFSG8Iq::TIME:<TIMESTAMP> (0) 2014/05/24 1

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

如果您尝试在迭代时修改地图,则会获得ConcurrentModificationException。你可以通过以下方式解决这个问题:

  • 存储要在列表,集合或其他单独数据结构中删除的元素,并在循环后删除它们
  • 使用Iterator
  • 提供的安全删除语义

答案 1 :(得分:0)

您已在地图的键集上获得了一个迭代器,并在结构上直接修改了地图。此类操作的行为由map实现定义。如果你看一下hash map的javadoc:

  

所有这类&#34;集合视图返回的迭代器   方法&#34;快速失败:如果地图在任何地方进行了结构修改   创建迭代器之后的时间,除了通过之外的任何方式   迭代器自己的remove方法,迭代器会抛出一个   ConcurrentModificationException的。因此,面对并发   修改,迭代器快速而干净地失败,而不是   在不确定的时间冒着任意的,非确定性的行为   在将来。

它描述了您所看到的行为。

不要使用for:next语法,而是使用Iterator的旧方法并调用Iterator.remove删除项目。