The iterators returned by this class's iterator and listIterator methods are fail-fast. If the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Example:
List<String> staff = new ArrayList<String>();
staff.add("Ajay");
staff.add("Kumar");
staff.add("Singh");
staff.add("sunny");
ListIterator listItr = staff.listIterator(); //get the ListIterator
// after getting iterator if we change(add or remove) here by ArrayList.add of ArrayList.remove then it will throw a ConcurrentModificationException except ListIterator.add or ListIterotr.remove.
staff.add("Ajay Kumar"); // will throw a ConcurrentModificationException
listItr.add("Ajay Kumar"); // OK
But there may be a exceptional situation where more than one thread of Iterator is trying to modify a collection in unsynchronized block or method then it will throw ConcurrentModificationException.
ListIterator listItr = staff.listIterator(); //get the ListIterator
Iterator itr = staff.iterator(); //get the Iterator
listItr.add("Ajay Kumar"); // throw ConcurrentModificationException
Example:
List<String> staff = new ArrayList<String>();
staff.add("Ajay");
staff.add("Kumar");
staff.add("Singh");
staff.add("sunny");
ListIterator listItr = staff.listIterator(); //get the ListIterator
// after getting iterator if we change(add or remove) here by ArrayList.add of ArrayList.remove then it will throw a ConcurrentModificationException except ListIterator.add or ListIterotr.remove.
staff.add("Ajay Kumar"); // will throw a ConcurrentModificationException
listItr.add("Ajay Kumar"); // OK
But there may be a exceptional situation where more than one thread of Iterator is trying to modify a collection in unsynchronized block or method then it will throw ConcurrentModificationException.
ListIterator listItr = staff.listIterator(); //get the ListIterator
Iterator itr = staff.iterator(); //get the Iterator
listItr.add("Ajay Kumar"); // throw ConcurrentModificationException
0 comments:
Post a Comment