오류 방지 반복기는 throw
ConcurrentModificationException
이 아닌 반복자입니다.오류 방지 반복자 및 약하게 일관성있는 반복자
fail safe iterators
과 weakly consistent iterators
의 차이점은 무엇입니까?
둘 다 같습니까?
오류 방지 반복기는 throw
ConcurrentModificationException
이 아닌 반복자입니다.오류 방지 반복자 및 약하게 일관성있는 반복자
fail safe iterators
과 weakly consistent iterators
의 차이점은 무엇입니까?
둘 다 같습니까?
Fail-safe
과 Weakly consistent
반복자는 모두 ConcurrentModificationException
을 반환하지 않습니다. CAS (비교 및 스왑)에 의존
weakly consistent iterators:
컬렉션 일부를 반영하지만, 약 일관성 반복자를 가지고 반드시 모든 사람들이 생성 된 이후 자신의 기가되는 콜렉션에 적용된 변경. 예를 들어, 반복자가 도달하기 전에 컬렉션의 요소가 수정 또는 제거 된 경우 이러한 변경 사항이 반영되지만 삽입에 대한 보장은 없습니다.
Fail safe iterator
반복기 메커니즘은 내부 Collection 데이터 구조의 복사본을 만들어 요소를 반복하는 데 사용합니다. 이렇게하면 기본 데이터 구조가 변경 될 경우 동시 수정 예외가 throw되지 않습니다. 물론 전체 어레이를 복사하는 오버 헤드가 도입되었습니다.
public CopyOnWriteArrayList(Collection<? extends E> c) {
Object[] elements = c.toArray();
if (elements.getClass() != Object[].class)
elements = Arrays.copyOf(elements, elements.length, Object[].class);
setArray(elements);
}
참조 :
Java Generics and Collections: 11.5. Collections and Thread Safety
Iterators – Fail fast Vs Fail safe
CopyOnWriteArrayList
우리가 쉽게 생성자의 소스를 보면 볼 수 있습니다 실패 안전 반복자와 하나의 구현
http://stackoverflow.com/q/14084104/469220 – Vlad
둘 모두 동일하지 않음 –
http://stackoverflow.com/questions/14084104/concurrenthashmap-returns-a-weakly-consistent-iterator-why-should-we -use-it-any –