Fail-fast and iterator Mode

Source: Internet
Author: User
Tags ibm developerworks concurrentmodificationexception

If the list is structurally modified at any time after the iterator is created, in any way cannot through the iterator's ownRemoveOrAddMethods, The iterator will throwConcurrentmodificationexception. 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.

After an iterator is created, if the list is modified from the structureRemoveOrAddMethod. The iterator throws any modification at any time.Concurrentmodificationexception. Therefore, in the face of concurrent modifications, the iterator will soon fail completely without the risk of any uncertain behavior in the future.

Note: The Fast failure behavior of the iterator cannot be guaranteed. In general, it is impossible to make any hard guarantee when there are non-synchronous concurrent modifications. The fast failure iterator tries its best to throwConcurrentmodificationexception. Therefore, writing programs dependent on this exception is incorrect. The correct method is:The fast failure behavior of the iterator should only be used to detect program errors.

Two http://blog.csdn.net/lianyu2008/archive/2009/10/10/4651451.aspx

Iterator <integer> keys = grademap. keyset (). iterator ();

Set <integer> gradesids = grademap. keyset ();
While (Keys. hasnext ()){
Integer I = keys. Next ();
If (! Gradesids. Contains (I )){
// Keys. Remove ();
Grademap. Remove (I );

System. Out. println (grademap. Size ());
}
}

 

In the red part, if you want to delete the elements referenced by the iterator from the aggregation, you can directly Delete the iterator.

Java. util. concurrentmodificationexception occurs when calling the reomve method of hashmap.

The solution is to use the iterator method to remove and then call the Remove Method of hashmap !! The Code is as follows:

Iterator <integer> keys = grademap. keyset (). iterator ();
While (Keys. hasnext ()){
Integer I = keys. Next ();
If (! Gradesids. Contains (I )){
Keys. Remove ();
Grademap. Remove (I );
}
}

Cause of the problem

Referenced in the network:
When fail-fast iterator is used to iterate collection or map and try to directly modify the content of collection/map, even if it is run in a single thread, Java. util. concurrentmodificationexception will also be thrown.

Iterator works in an independent thread and has a mutex lock. After the iterator is created, a single-chain index table pointing to the original object will be created. When the number of original objects changes, the content of the index table will not change simultaneously, therefore, when the index pointer moves backwards, the object to be iterated cannot be found. Therefore, iterator will immediately throw Java according to the fail-fast principle. util. concurrentmodificationexception exception.

Therefore, iterator cannot be changed when it is working. However, you can use the iterator method to remove () to delete objects. The iterator. Remove () method will maintain index consistency while deleting the current iteration object.

Interestingly, if your collection/map object actually has only one element, the concurrentmodificationexception will not be thrown. This is why it wocould be wrong to write a program that depended on this exception for its correctness: concurrentmodificationexception shocould be used only to detect bugs.

Appendix: Instructions for Java. util. Concurrent package from IBM developerworks:
The collection classes in the Java. util package return the fail-fast iterator, which means that the set does not change its content when the threads iterate in the set content. If the fail-fast iterator detects a change during the iteration, it throws concurrentmodificationexception, which is an uncontrollable exception.
It is usually inconvenient for many concurrent applications to not change the set during iteration. On the contrary, it is better that it allows concurrent modifications and ensures that the iterator can provide consistent views of the set as long as reasonable operations are performed, as the iterator in the Java. util. Concurrent collection class does.
The iterator returned by the java. util. Concurrent collection is called the Weakly Consistent iterator. For these classes, if the element has been deleted since the iteration and has not been returned by the next () method, it will not be returned to the caller. If an element has been added since the iteration, it may return the caller or not. In an iteration, no matter how you change the underlying set, the element is not returned twice.

 

External iterator and internal iterator of three iterators

A white box aggregation interface provides external users with interfaces to access their internal elements, so that external iterations can implement iteration functions through the aggregation Traversal method.

Black box clustering does not provide external interfaces for Traversing their own element objects. Therefore, these element objects can only be accessed by clustered internal members.
Since the internal iterator is a clustering internal class, you can access the clustering element.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.