What are fail-fast and fail-safe? And what are they related to? The above two points are the content of this article, no more nonsense words, please slow down the body.
We’re all dealing with collection classes like HashMap and ArrayList, which fail quickly in the java.util package; Classes in the java.util.concurrent package fail safely, such as ConcurrentHashMap.
1. Fail fast
When iterating over A collection object, if thread A is iterating over the collection and thread B is modifying the collection (adding, deleting, modifying), or thread A is modifying the collection while iterating, Throw ConcurrentModificationException will lead to A thread.
Let’s take a look at the code:
HashMap hashMap = new HashMap();
hashMap.put("Not only Java - 1", 1);
hashMap.put("Not only Java - 2", 2);
hashMap.put("Not only Java - 3", 3);
Set set = hashMap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
hashMap.put("The next loop throws an exception.", 4);
System.out.println("Now the hashMap has a length of + hashMap.size());
}
Copy the code
The effect after execution is shown as follows:
Why are exceptions thrown when iterators are used to iterate through the collection?
The reason is that the iterator accesses the contents of the collection directly during traversal and uses a modCount variable during traversal. If the contents of the collection change during traversal, the value of modCount 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.
2. fail-safe
Once you understand what a quick failure is, a security failure is easy to understand.
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.
As the iteration is to traverse the copies of the original collection, so in the process of traversing the changes to the original collection and cannot be detected by the iterator, so don’t throw ConcurrentModificationException anomalies
Let’s go into the code and see if that’s true
ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap(); Concurrenthashmap. put(" java-1 ", 1); concurrenthashmap. put(" java-1 ", 1); Concurrenthashmap. put(" java-2 ", 2); concurrenthashmap. put(" java-2 ", 2); Concurrenthashmap. put(" java-3 ", 3); concurrenthashmap. put(" java-3 ", 3); Set set = concurrentHashMap.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); Concurrenthashmap. put(" Next loop normally executes ", 4); } system.out.println (" end of program ");Copy the code
The running effect is as follows, it does not throw exceptions, the normal execution of the program.
As a final note, fast and safe failures are for iterators. Container classes under the java.util.concurrent package are recommended for concurrent environments, unless no modification is performed.
Reference article: blog.csdn.net/qq_31780525…
PS: This article was originally published in the wechat public account “not only Java”, the background reply “Java”, send you 13 Classic Java e-books. The public account focuses on sharing Java dry goods, reading notes, growth thinking