gpt4 book ai didi

java - ArrayList和Concurrent HashMap并发修改异常

转载 作者:行者123 更新时间:2023-12-01 16:33:30 27 4
gpt4 key购买 nike

以下是我存储数据的数据结构和更新数据的方法。

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;
}
}
}

我有下面的方法,它通过轮询每秒都被访问,以显示网页中的一些数据,通过上面的并发 HashMap 检索,该数据也会不时更新数据,并在订单被删除时更新。

/* 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]

最佳答案

迭代时不要对列表调用remove,这可能会导致并发修改异常。

Orderlist.getNewOrder().remove(drm); 

在迭代器上调用remove。

itr.remove()

关于java - ArrayList和Concurrent HashMap并发修改异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11940163/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com