简体   繁体   中英

fail-fast iterator

I get this definition : As name suggest fail-fast Iterators fail as soon as they realized that structure of Collection has been changed since iteration has begun .

what it mean by since iteration has begun ? is that mean after Iterator it=set.iterator() this line of code?

public static void customize(BufferedReader br) throws IOException{  
    Set<String> set=new HashSet<String>(); // Actual type parameter added  
    **Iterator it=set.iterator();**

First of all, they are fail- fast , not fail- safe .

The contract is that structural modifications (ie insertions/deletions) of certain types of collections invalidate existing iterators into the collection. Fail-fast iterators attempt to detect that they are not supposed to be valid and throw a ConcurrentModificationException . This is done as a service to you, the programmer, to help discover this type of bugs quicker.

In your example:

Iterator it = set.iterator();
it.next();
set.add("unique-entry"); // invalidates the iterator
it.next();

If you're lucky, the second it.next() will detect the invalid usage and throw an exception. Note that this is done on a best-effort basis and is not guaranteed.

The iterator being fail-fast means the following piece of code is expected to fail:

Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
set.add("");
it.next();   // the set has changed now, and the iterator will throw an exception

because the following series of events occur: The iterator is created, then its underlying collection changes, then the iterator is accessed.

is that mean after Iterator it=set.iterator() this line of code?

Yes. If you look at the code for HashSet.iterator() you'll see it's just this:

return map.keySet().iterator();

... which delegate's to HashMap.KeySet.iterator() . There are a few more links in the chain, but eventually you get to HashMap.HashIterator , which contains this in the constructor:

private abstract class HashIterator<E> implements Iterator<E> {
    int expectedModCount;   // For fast-fail

    ...

    HashIterator() {
        expectedModCount = modCount;
        ...
    }
}

... where modCount is a field in the enclosing instance of HashMap which keeps track of the number of modifications.

是的,在使用.iterator()之后不要更改集合如果你打算迭代它,你可以使用.remove()如果你想删除最新的元素

Before Fail Fast Iterator is starting to work it's getting count of collection and after any iteration it is checking if count is changed or not, and in case of changed count JVM will throw ConcurrentModificationException. Fail fast iterators are any iterator of collection which is inside java.util package(eg ArrayList, LinkedList etc) and Fail Safe iterators are iterators which are inside of java.concurrent package(eg CopyOnWriteArrayList, CopyOnWriteSet etc.). Fail Fast iterators will throw exception in case of concurrent modification but Fail Safe iterator is basically working with copy of collection which is not throwing exception in case of concurrent modification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM