以下是我存储数据的数据结构和更新数据的方法。
ConcurrentHashMap<String, MyOrder> myOrder = new ConcurrentHashMap<String, MyOrder>();
public void updateOrder(MyData DR) {
MyOrder Orderlist = myOrder.get(DR.keyID);
Orderlist.getCanceled().add(DR);
if (Orderlist.getCanceled().size() == 10) {
Orderlist.getCanceled().remove(0);
}
/* remove order when cancelled */
Iterator<MyData> itr = Orderlist.getNewOrder().iterator();
MyData drm = null;
while (itr.hasNext()) {
drm = itr.next();
if (drm.ClOrdID.equalsIgnoreCase(DR.ClOrdID)) {
Orderlist.getNewOrder().remove(drm);
getTable().get(drm.InsKey).getCompOrder().remove(drm);
break;
}
}
}
我有以下方法,每秒通过轮询访问,以显示通过上述并发哈希映射检索的网页中的一些数据,该哈希映射也会不时更新数据,并在订单被删除时更新。
/* accessed for data display every second */
public List<MyOrder> getMyDataList(String keyID) {
List<MyOrder> orderList = new ArrayList<MyOrder>();
try {
if (myOrder.containsKey(keyID)) {
orderList.add(myOrder.get(keyID));
}
} catch (Exception e) {
e.printStackTrace();
}
return orderList;
}
我的问题是当方法同时执行一段时间时,它会给出以下并发修改异常,我该如何克服这个问题?申请这个的最佳解决方案是什么?异常指向上述方法“getMyDataList”
Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception: java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source) [:1.6.0_22]
at java.util.AbstractList$Itr.next(Unknown Source) [:1.6.0_22]
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(DefaultTypeAdapters.java:637) [:]
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(DefaultTypeAdapters.java:624) [:]
at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerializer(JsonSerializationVisitor.java:184) [:]
at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler(JsonSerializationVisitor.java:204) [:]
at com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63) [:]
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120) [:]
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationContextDefault.java:62) [:]
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationContextDefault.java:53) [:]
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(DefaultTypeAdapters.java:643) [:]
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(DefaultTypeAdapters.java:624) [:]
at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerializer(JsonSerializationVisitor.java:184) [:]
at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(JsonSerializationVisitor.java:160) [:]
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101) [:]
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationContextDefault.java:62) [:]
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationContextDefault.java:53) [:]
at com.google.gson.Gson.toJsonTree(Gson.java:220) [:]
at com.google.gson.Gson.toJson(Gson.java:260) [:]
at com.google.gson.Gson.toJson(Gson.java:240) [:]
at com.company.jsf.order.MyBean.myOrdersData(MyBean.java:488) [:]
at sun.reflect.GeneratedMethodAccessor342.invoke(Unknown Source) [:1.6.0_22]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [:1.6.0_22]
at java.lang.reflect.Method.invoke(Unknown Source) [:1.6.0_22]
at org.apache.el.parser.AstValue.invoke(AstValue.java:196) [:6.0.0.Final]
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) [:6.0.0.Final]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) [:2.0.3-FCS]
at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.java:124) [:2.0.3-FCS]
at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.java:2378) [:2.0.3-FCS]
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:102) [:2.0.3-FCS]
at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2040) [:2.0.3-FCS]
at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.java:1988) [:2.0.3-FCS]
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:284) [:2.0.3-FCS]
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:242) [:2.0.3-FCS]
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:104) [:2.0.3-FCS]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) [:2.0.3-FCS]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135) [:2.0.3-FCS]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309) [:2.0.3-FCS]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343) [:]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) [:]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109) [:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) [:]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149) [:]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) [:3.0.0.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167) [:3.0.0.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
at java.lang.Thread.run(Unknown Source) [:1.6.0_22]