Why vector methods Iterator and ListIterator do not work fast

According to http://download.oracle.com/javase/1.4.2/docs/api/java/util/Vector.html

Iterators returned by the Vector methods of the iterator and listIterator fail-fast: if the vector is structurally modified at any time after creating the Iterator, remove or add methods other than using the Iterator, the Iterator will throw a ConcurrentModificationException. Thus, in the face of parallel modification, the Iterator does not work quickly and cleanly, and not at the risk of arbitrary, non-deterministic behavior at an undetermined time in the future. Enumerations returned by the vector element method are not able to quickly. Please note that the fast-acting behavior of the iterator cannot be as usual, it is impossible to speak, it is difficult to guarantee the presence of unsynchronized parallel modification. Unmanaged iterators throw ConcurrentModificationException on the maximum basis. Hence,it would be wrong to write a program that depended on this exception for its correctness: erratic behavior iterators should only be used to detect errors

Could you give me an example to test the above set of instructions? I am still unclear with the quick behavior of the Iterator and ListIterator vector method. Confused: - ((

+3
source share
3 answers

if the vector is structurally modified at any time after creating the Iterator in any way, except through its own methods of removing or adding Iterator, the Iterator will throw ConcurrentModificationException.

Here is an example:

import java.util.*;

public class Test {

    public static void main(String[] args) {
        List<String> strings = new Vector<String>();

        strings.add("lorem");
        strings.add("ipsum");
        strings.add("dolor");
        strings.add("sit");

        int i = 0;

        Iterator<String> iter = strings.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());

            // Modify the list in the middle of iteration.
            if (i++ == 1)
                strings.remove(0);
        }
    }
}

Conclusion:

lorem
ipsum
Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
    at java.util.AbstractList$Itr.next(AbstractList.java:343)
    at Test.main(Test.java:18)

The program performs the following actions:

  • Creates a vector and gets an iterator
  • Calls twice next().
  • Changes a vector (removing the first element)
  • Call next()again (after changing the vector)
  • This causes a reset ConcurrentModificationException.

Java , ConcurrentModificationExceptions. , ( ) , , CopyOnWriteArrayList

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class Test {

    public static void main(String[] args) {
        List<String> strings = new CopyOnWriteArrayList<String>();

        strings.add("lorem");
        strings.add("ipsum");
        strings.add("dolor");
        strings.add("sit");

        int i = 0;

        Iterator<String> iter = strings.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());

            // Modify the list in the middle of iteration.
            if (i++ == 1)
                strings.remove(0);
        }
    }
}

:

lorem
ipsum
dolor
sit
+8

List<String> strings = new ArrayList<String>();
strings.add("a");
strings.add("b");
for(String s: strings)
  strings.remove(s);

, .

, Iterator , , , ( ) , . , , . ( )

concurrency - . Java 2004 , .

BTW: Vector, .

+2

, , 1-10, . Iterators remove(). , , , , . , ( ), , . , ; , - . , elements().

Fail-fast () , , , . , , . , .

, iterator() listIterator(), . Vector ( ) , . , Vector. , next() remove(), Iterator Vector. , ConcurrentModificationException.

+1
source

Source: https://habr.com/ru/post/1781052/