Java notes-fast failure and security failure

Source: Internet
Author: User

Reference: http://blog.csdn.net/chenssy/article/details/38151189

Fast Failure Fail-fast Security Failure Fail-Safe
All of the collection classes below the Java.util package are fast-failing All classes under the Java.util.concurrent package are safe and fail.
A fast-failing iterator throws a Concurrentmodificationexception exception A security failure iterator does not throw an concurrentmodificationexception exception.

Fast failure

When a thread iterates over a vector, it does not allow other threads to modify the contents of the vector, so that the iterations of the iterator will be inaccurate, such as when using iterator iteration collection, iterator is another thread. It iterates over the collection, and if at this point the Collection.remove (obj) method modifies the contents of the collection, a Concurrentmodificationexception exception occurs. This time the iterator fails quickly.

Like HashMap HashSet ArrayList, it's a quick failure.

Why is it called fast? My understanding: In the iteration, if another thread modifies the contents of the content, it immediately errors, rather than continuing the iteration.

Security failure

Iterator's security failure is based on a copy of the underlying collection, so it is not affected by modifications on the source collection.

Example: Copyonwriterarraylist

A thread-safe variant of ArrayList, where all mutable operations (add, set, and so on) are implemented by a new copy of the underlying array. This class produces more overhead, but in both cases it is well suited for use.
1: When you cannot or do not want to perform synchronous traversal, but you need to exclude conflicts from concurrent threads.
2: When the number of traversal operations greatly exceeds the number of variable operations.

Java notes-fast failure and security failure

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.