notes

fail-fast

Collection classes in the java.util package fail fast and cannot be modified concurrently in multiple threads.

An exception is thrown if other threads modify the collection while the iterator traverses it.

fail-safe

Containers under the java.util.concurrent package are security failures and can be used and modified concurrently in multiple threads.

A collection container with a security failure mechanism iterates by copying the contents of the original collection when obtaining iterators.

example

So, in this case, you just need to replace the ArrayList with the corresponding class under the java.util.Concurrent package.

The following replacement

private static List<String> list = new ArrayList<String>();
Copy the code

private static List<String> list = new CopyOnWriteArrayList<String>();
Copy the code

The original

1. Fail — fast

When traversing a collection object with an iterator, Concurrent Modification Exceptions are thrown if changes (additions, deletions) are made to the structure of the collection object during traversal.

How it works: Iterators directly access the contents of a collection during traversal and use a modCount variable during traversal. If the structure of the collection changes during traversal, the modCount value is changed. Whenever the iterator iterates over the next element using hashNext()/next(), it checks whether the modCount variable is the expectedmodCount value and returns the traversal if it is; Otherwise, an exception is thrown and the traversal is terminated.

Note: The exception is thrown if modCount is detected! =expectedmodCount This condition. If the set changes when the modCount value is just set to the expectedmodCount value, the exception will not be thrown. Therefore, concurrent operations cannot be programmed depending on whether or not this exception is thrown; this exception is only recommended for detecting concurrent modification bugs.

Scenario: Collection classes in the java.util package fail fast and cannot be modified concurrently (iteratively) in multiple threads.

2. Fail — Safe

The collection container using security failure mechanism is not directly accessed on the collection content in traversal, but copies the original collection content first and traverses the copied collection.

Principle: Since the copy of the original collection is iterated during iteration, the changes made to the original collection during iteration cannot be detected by the iterator, so Concurrent Modification Modification is not triggered.

Disadvantages: Copy-based has the advantage of avoiding Concurrent Modification Exceptions, but again, the iterator cannot access the modified content, i.e., iterators iterate over the copy of the collection at the beginning of the iteration, and the iterator is not aware of the changes that occurred during the iteration of the original collection.

Scenario: Containers under the java.util.concurrent package are security failures and can be used and modified concurrently in multiple threads.