IT/JAVA

java.util.ConcurrentModificationException 예외

So1_b 2022. 7. 26. 19:55

멱집합 알고리즘을 풀다가 발생한 예외

 

알게된 점 )

 

for-each문은 배열이나 Iterable인터페이스를 구현하는 객체만 대상이 될 수 있는데

그 이유는 내부에서 Iterator를 사용하는 형태로 변경되어 인식되기 때문이다.

 

ArrayList의 경우 ArrayList.Itr 내부클래스를 통해 Iterator객체가 생성됨.

ArrayList의 상위 클래스에 데이터의 변경사항을 카운트하는 modeCount전역변수가 존재하는데

Iterator객체가 생성될 때 modeCount값으로 expectedModecount변수를 초기화시킨다.

 

ArrayList$Itr.class의 일부분

그리고 next()가 호출될 때마다 modeCount변수와 expectedModecount변수를 비교하고 

다를경우 java.util.ConcurrentModificationException예외 발생

 

즉 modCount를 변경시키는 메서드를 사용후 next()를 호출하게 되면 예외가 발생된다

 

참고 사이트

https://jistol.github.io/java/2019/11/24/concurrentmodificationexception-with-ehcache/