我是靠谱客的博主 时尚洋葱,最近开发中收集的这篇文章主要介绍java.util.ConcurrentModificationException的解决办法,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
java.util.ConcurrentModificationException的解决办法
项目需求:
比较两个list集合里面的元素,根据某些规则,删除里面某个集合的元素,本想以为比较简单,但是还是碰到了一些问题。本次使用的是foreach来实现的。之前使用的是Iterator迭代来实现,不报此异常。
使用Iterator来迭代的代码如下:
- public String findByEvent() throws Exception {
- try {
- eventName = new String(eventName.getBytes("iso-8859-1"), "UTF-8");
- listGroupInfo = this.groupInfoService.findByEvent(eventName);
- Iterator<GroupInfo> iter = listGroupInfo.iterator();
- if (listGroupInfo.size() != 0) {
- GroupInfo groupInfo = iter.next();
- for (int i = 0; i < listGroupInfo.size() - 1; i++) {
- for (int j = listGroupInfo.size() - 1; j > i; j--) {
- if (listGroupInfo.get(j).getGroupNumber().equals(
- listGroupInfo.get(i).getGroupNumber())) {
- listGroupInfo.remove(j);
- }
- }
- }
- }
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- return INPUT;
- }
- return SUCCESS;
- }
上面的代码运行正常!
本次使用的源代码如下:
- public String findUnit() throws Exception {
- try {
- Map session = ActionContext.getContext().getSession();
- String ids = session.get("userId").toString();
- id = Long.parseLong(ids);
- listAthlete = this.athleteService.findByUserId(id);
- listUnitInfo = this.unitInfoService.findByUserId(id);
- for(UnitInfo unitInfo :listUnitInfo){
- for(Athlete athlete:listAthlete){
- if(athlete.getUnitGroup().getUnitInfo().getName().equals(unitInfo.getName())){
- System.out.println("移除:"+unitInfo.getName());
- listAthlete.remove(athlete);
- listUnitInfo.remove(unitInfo);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- System.out.print(e);
- return INPUT;
- }
- return SUCCESS;
- }
使用的是foreach,因为大家都知道使用foreach为我们带来了极大的方便,所以考虑使用foreach也是正常的思路。但是,使用的时候,却产生了如下的异常。
java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException
- at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
- at java.util.AbstractList$Itr.next(AbstractList.java:343)
- at com.yaxing.action.AthleteAction.findUnit(AthleteAction.java:398)
- 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 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
- at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
- at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
- at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
- at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
- at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
- at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
- at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
- at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at com.yaxing.util.competitionInterceptor.intercept(competitionInterceptor.java:57)
- at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
- at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
- at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
- at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
- at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
- at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
- at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
- at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
- at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
- at java.lang.Thread.run(Thread.java:619)
我们可以查看此异常JDK文档的说法
- public class ConcurrentModificationExceptionextends RuntimeException当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
- 例如,某个线程在 Collection 上进行迭代时,通常不允许另一个线性修改该 Collection。通常在这些情况下,迭代的结果是不确定的。如果检测到这种行为,一些迭代器实现(包括 JRE 提供的所有通用 collection 实现)可能选择抛出此异常。执行该操作的迭代器称为快速失败 迭代器,因为迭代器很快就完全失败,而不会冒着在将来某个时间任意发生不确定行为的风险。
- 注意,此异常不会始终指出对象已经由不同 线程并发修改。如果单线程发出违反对象协定的方法调用序列,则该对象可能抛出此异常。例如,如果线程使用快速失败迭代器在 collection 上迭代时直接修改该 collection,则迭代器将抛出此异常。
- 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证。快速失败操作会尽最大努力抛出 ConcurrentModificationException。因此,为提高此类操作的正确性而编写一个依赖于此异常的程序是错误的做法,正确做法是:ConcurrentModificationException 应该仅用于检测 bug。
Java中的foreach实际上使用的是还是Iterator进行处理的。我们在foreach时,从list中删除了一个元素,这导致了Iterator抛出了ConcurrentModificationException。
修改如下:
- public String findUnit() throws Exception {
- try {
- Map session = ActionContext.getContext().getSession();
- String ids = session.get("userId").toString();
- id = Long.parseLong(ids);
- listAthlete = this.athleteService.findByUserId(id);
- listUnitInfo = this.unitInfoService.findByUserId(id);
- for (Iterator<Athlete> it = listAthlete.iterator();it.hasNext();){
- Athlete athlete=it.next();
- for (Iterator<UnitInfo> it2 = listUnitInfo.iterator();it2.hasNext();){
- UnitInfo unitInfo =it2.next();
- if(athlete.getUnitGroup().getUnitInfo().getName().equals(unitInfo.getName())){
- System.out.println("移除:"+unitInfo.getName());
- it2.remove();
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- System.out.print(e);
- return INPUT;
- }
- return SUCCESS;
- }
测试,发现,没报错了。
转载于:https://blog.51cto.com/enetq/933057
最后
以上就是时尚洋葱为你收集整理的java.util.ConcurrentModificationException的解决办法的全部内容,希望文章能够帮你解决java.util.ConcurrentModificationException的解决办法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复