10 Ağustos 2020 Pazartesi

ConcurrentModificationException Sınıfı

Giriş
Java veri yapıları iki çeşit iterator sağlarlar.
1. Fail Fast Iterator
2. Fail Sage Iterator

Fail Fast Iterator Nedir?
Açıklaması şöyle. Yani bazı veri yapıları fail fast iterator sağlarlar. Bu iterator eğer veri yapısında değişiklik olduğunu anlarsa ConcurrentModificationException fırlatır.
Java Collections supports two types of Iterator, fail safe and fail fast. The main distinction between a fail-fast and fail-safe Iterator is whether or not the underlying collection can be modified while its begin iterated. If you have used Collection like ArrayList then you know that when you iterate over them, no other thread should modify the collection. If Iterator detects any structural change after iteration has begun e.g adding or removing a new element then it throws ConcurrentModificationException,  this is known as fail-fast behavior and these iterators are called fail-fast iterator because they fail as soon as they detect any modification .
Fail Safe Iterator Nedir?
Açıklaması şöyle. Yani ismi ConcurrentX şeklinde olan sınıflar Fail Safe iterator sağlarlar.
The other type of iterator was introduced in Java 1.5 when concurrent collection classes e.g. ConcurrentHashMap, CopyOnWriteArrayList and CopyOnWriteArraySet was introduced.
Bu iterator'ler bir view kullanırlar. Açıklaması şöyle.
These iterator uses a view of original collection for doing iteration and that's why they doesn't throw ConcurrentModificationException even when original collection was modified after iteration has begun.  This means you could iterate and work with stale value, but this is the cost you need to pay for fail-safe iterator and this feature is clearly documented

Hiç yorum yok:

Yorum Gönder