Java. util. ConcurrentModificationException Solution

Source: Internet
Author: User

 
The Iterator iteration code is as follows:
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;
 
}
The above code runs normally!
The source code used this time is as follows:
 
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 ("Remove:" + unitInfo. getName ());
ListAthlete. remove (athlete );
ListUnitInfo. remove (unitInfo );

}
}
}
} Catch (Exception e ){
E. printStackTrace ();
System. out. print (e );
Return INPUT;
}
Return SUCCESS;
}

Foreach is used, because we all know that using foreach brings us great convenience, so it is also a normal idea to consider using foreach. However, the following exception occurs during use.
 
Java. util. ConcurrentModificationException
Java. util. ConcurrentModificationException
At java. util. effecactlist $ Itr. checkForComodification (effecactlist. 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.defaactionactioninvocation. invokeAction (DefaultActionInvocation. java: 452)
At com. opensymphony. xwork2.DefaultActionInvocation. invokeActionOnly (DefaultActionInvocation. java: 291)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 254)
At com. opensymphony. xwork2.interceptor. DefaultWorkflowInterceptor. doIntercept (defaworkworkflowinterceptor. java: 176)
At com. opensymphony. xwork2.interceptor. MethodFilterInterceptor. intercept (MethodFilterInterceptor. java: 98)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. 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.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. opensymphony. xwork2.interceptor. ConversionErrorInterceptor. intercept (ConversionErrorInterceptor. java: 133)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. 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.defaactionactioninvocation. invoke (defaactionactioninvocation. 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.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. opensymphony. xwork2.interceptor. StaticParametersInterceptor. intercept (StaticParametersInterceptor. java: 190)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At org. apache. struts2.interceptor. MultiselectInterceptor. intercept (MultiselectInterceptor. java: 75)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At org. apache. struts2.interceptor. CheckboxInterceptor. intercept (CheckboxInterceptor. java: 94)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At org. apache. struts2.interceptor. FileUploadInterceptor. intercept (FileUploadInterceptor. java: 243)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. opensymphony. xwork2.interceptor. ModelDrivenInterceptor. intercept (ModelDrivenInterceptor. java: 100)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. opensymphony. xwork2.interceptor. ScopedModelDrivenInterceptor. intercept (ScopedModelDrivenInterceptor. java: 141)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At org. apache. struts2.interceptor. debugging. DebuggingInterceptor. intercept (DebuggingInterceptor. java: 270)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. opensymphony. xwork2.interceptor. ChainingInterceptor. intercept (ChainingInterceptor. java: 145)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. 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.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. opensymphony. xwork2.interceptor. I18nInterceptor. intercept (I18nInterceptor. java: 176)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At org. apache. struts2.interceptor. ServletConfigInterceptor. intercept (ServletConfigInterceptor. java: 164)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. opensymphony. xwork2.interceptor. AliasInterceptor. intercept (AliasInterceptor. java: 190)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. opensymphony. xwork2.interceptor. ExceptionMappingInterceptor. intercept (ExceptionMappingInterceptor. java: 187)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At com. yaxing. util. competitionInterceptor. intercept (competitionInterceptor. java: 57)
At com. opensymphony. xwork2.defaactionactioninvocation. invoke (defaactionactioninvocation. java: 248)
At org.apache.struts2.impl.StrutsActionProxy.exe cute (StrutsActionProxy. java: 52)
At org. apache. struts2.dispatcher. Dispatcher. serviceAction (Dispatcher. java: 498)
At org.apache.struts2.dispatcher.ng.ExecuteOperations.exe cuteAction (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)
 
You can view the JDK documentation for this exception.
 
Public class ConcurrentModificationExceptionextends RuntimeException: this exception is thrown when the method detects concurrent object modifications but does not allow such modifications.
 
For example, when a thread iterates over a Collection, it usually does not allow another thread to linearly modify the Collection. In these cases, the iteration results are usually uncertain. If this behavior is detected, some iterator implementations (including all common collection implementations provided by JRE) may choose to throw this exception. The iterator that executes this operation is called the fast failure iterator, because the iterator quickly fails completely without the risk of any uncertain behavior at a certain time in the future.
 
Note that this exception does not always indicate that the object has been modified concurrently by different threads. If a single thread sends a method call sequence that violates the object protocol, the object may throw this exception. For example, if the thread uses the quick failure iterator to directly modify the collection when it iterates on the collection, the iterator will throw this exception.
 
Note: The Fast failure behavior of the iterator cannot be guaranteed, because in general, it is impossible to make any hard guarantee for non-synchronous concurrent modifications. The quick failed operation will throw ConcurrentModificationException as much as possible. Therefore, writing a program dependent on this exception to improve the correctness of such operations is an incorrect practice. The correct practice is: ConcurrentModificationException should only be used to detect bugs.
 
In Java, foreach actually uses Iterator for processing. In foreach, an element is deleted from the list, which causes the Iterator to throw ConcurrentModificationException.
 
Modify as follows:
 
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 ("Remove:" + unitInfo. getName ());
It2.remove ();
}
}
}
} Catch (Exception e ){
E. printStackTrace ();
System. out. print (e );
Return INPUT;
}
Return SUCCESS;
}


Author: w1564000045

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.