Java

#java.util.ConcurrentModificationException 이슈

taehyun_kim 2017. 10. 30. 10:14

상황


 단순히 enhanced for loop ( 15줄과 같은 반복문 ) 를 쓰면서 제거하는 예제인데, 멀티쓰레드에서나 보던 ConcurrentModificationException이 발생하였다. 도대체 왜???



remove() 함수를 따라가다 보면 checkForComodification() 이라는 함수를 확인할 수 있다.  

expectedModCount는 리스트의 데이터 변경 여부를 체크하기 위한 변수이다.



remove()에서 호출하는 fastRemove()에서 modCount를 증가시킨 상태에서 Iterator의 next()가 호출 되면 위의 예외를 던지게 된다. 

( fail-fast???)



ConcurrentModificationException의 정의


This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.