Fast failure
in the JDK , there are many descriptions of quick failures in viewing collections:
Note that this implementation is not synchronous. If more than one thread accesses a hash map at the same time, and at least one of the threads modifies the mapping from the structure, it must remain externally synchronized. (Structural modification is any action that adds or deletes one or more mapping relationships; changing only the values associated with the key that the instance already contains are not structural modifications.) This is typically done by synchronizing the objects that naturally encapsulate the mapping. If such an object does not exist, you should use the Collections.synchronizedmap method to "wrap" the map. It is a good idea to do this at creation time to prevent unintended non-synchronous access to the mappings, as follows:
Map m =collections.synchronizedmap (new HashMap (...)); The iterator returned by all such "collection View Methods" is a quick failure: After the iterator has been created, if the mapping is modified from the structure, the iterator will throw unless modified by the Remove method of the iterator itself, any other time, in any way. Concurrentmodificationexception. Therefore, in the face of concurrent modifications, the iterator will soon fail completely without risking any uncertain behavior at any time in the future.
Note that the fast failure behavior of iterators is not guaranteed and, in general, there is no firm guarantee that there is an asynchronous concurrent modification. The fast-failing iterator does its best to throw concurrentmodificationexception. Therefore, it is wrong to write a program that relies on this exception, and the correct approach is that the fast failure behavior of the iterator should only be used to detect program errors.
fast failure: for non-concurrent collections, when they are iterated, for example iterator when iterating, iterator is another thread, if there are other threads (such as Collection ) for structural modification (modifying the contents of the collection), this iteration immediately senses and immediately throws concurrentmodificationexception The exception, not the iteration, tells you that something went wrong, causing a quick failure. If modified with iterator , this problem will not occur, such as Iterator.move (); in other words, it involves synchronization between multiple threads .
Example code:
[Java] view plain copy
- Package Corejava;
- import Java.util.HashMap;
- import java.util.Hashtable;
- import java.util.Iterator;
- import Java.util.Map;
- import java.util.Map.Entry;
- import Java.util.concurrent.ConcurrentHashMap;
Public class Concurrenthashmaptest {
- Public static void main (string[] args) {
- 12.
- hashtable<string, string> table = new hashtable<string, string> ();
- Table.put ("A", "VB");
- Table.put ("s", "er");
- Table.put ("D", "FG");
- Table.remove ("D");
- iterator<entry<string, string>> Iterator = Table.entryset (). Iterator ();
- 19.
- while ( iterator.hasnext ()) {
- System.out.println (Iterator.next (). GetValue ());
- Iterator.remove ();//Use iterator directly to modify the program normal
- *//Table.put ("C", "WC"); Delete data directly from Hashtable error
- Table.remove ("D"), directly from the Hashtable add or delete data will be error hashtable,hashmap, such as non-concurrent sets if the iterative process of adding or subtracting data,
- 25.}
- 26.
- System.out.println ("-----------");
- 28.
- hashmap<string, string> HashMap = new hashmap<string, string> ();
- Hashmap.put ("A", "VB");
- Hashmap.put ("s", "er");
- Hashmap.put ("D", "FG");
- iterator<entry<string, string>> iterators = Hashmap.entryset ()
- Iterator ();
- 35.
- The. while (Iterators.hasnext ()) {
- Panax Notoginseng System.out.println (Iterators.next (). GetValue ());
- Iterators.remove ();//Normal
- *//Hashmap.remove ("D");//directly from the Hashtable add and delete data will be error hashtable,hashmap and other non-concurrent sets, if the iterative process of adding or subtracting data, will quickly fail (a change detected, immediately throw an exception)
- 40.}
- 41.
- System.out.println ("-----------");
- 43.
- concurrenthashmap<string, string> map = new concurrenthashmap<string, string> ();
- Map.put ("A", "VB");
- Map.put ("s", "er");
- Map.put ("D", "FG");
- iterator<entry<string, string>> mapiterator = Map.entryset (). Iterator ();
- 49.
- while ( mapiterator.hasnext ()) {
- Wuyi System.out.println (Mapiterator.next (). GetValue ());
- Map.Remove ("D");//Normal concurrent collection no quick failure problem
- Map.put ("C", "WC");//Normal concurrent collection no quick failure problem
- 54.}
- System.out.println ("-----------");
- . For (map.entry<string, string> entry:map.entrySet ()) {
- System.out.println (Entry.getvalue () + "," + Entry.getkey ());
- 58.}
- System.out.println ("-----------");
- For ( map.entry<string, string> entry:table.entrySet ()) {
- System.out.println (Entry.getvalue () + "," + Entry.getkey ());
- 62.}
- 63.}
- 64.
- 65./*
- * Final entry<k,v> NextEntry () {if (Modcount! = expectedmodcount) Throw
- * New Concurrentmodificationexception (); Entry<k,v> e = next; if (E = =
- * null) throw new Nosuchelementexception ();
- 69. *
- * if (next = e.next) = = null) {entry[] t = table; while (Index < t.length
- * && (next = t[index++]) = = null); } current = e; return e; }
- 72. */
- 73.
74.}
Fast Failure Exception: This exception is thrown when the method detects concurrent modifications to an object, but does not allow this modification
Workaround:
A: Iterator removal
B: Lock the gun back. The collection is self-deleted. (Premise: A collection of thread synchronizations)
Java Collection Quick Failure exception