Fail-fast (fast failure) mechanism in Java __java

Source: Internet
Author: User
Tags concurrentmodificationexception
Introduced in the previous ArrayList of the expansion of the problem for the modcount operation is not described in detail, the operation of the variable in Add,remove and other operations will change. So what's the effect of this variable? An introduction to the fail-fast mechanism, a fast failure mechanism, is an error detection mechanism in the Java Collection (Collection). When the set changes in structure during the iteration of the collection, it is possible to fail-fast, that is, to throw a concurrentmodificationexception exception. The fail-fast mechanism does not guarantee that exceptions will be thrown under different steps, it is just doing its best to throw, so this mechanism is typically used only to detect bugs. The fail-fast scenario may appear in our common Java collection as a fail-fast mechanism, such as Arraylist,hashmap. Rapid failure can occur in multithreaded and single-threaded environments. 1, single-threaded environment fail-fast:arraylist occurrence Fail-fast Example: [Java]  View Plain  copy public static void main (string[] args)  {          List<String> list = new ArrayList<> ();          for  (int i = 0 ; i < 10  ; i++ )  {               List.add (i +  "");         }          iterator<string> iterator = list.iterator ();          int i = 0 ;         while ( Iterator.hasnext ())  {              if   (i == 3)  {                  &nBsp; list.remove (3);              }               system.out.println (Iterator.next ());               i ++;          }  } This code defines a ArrayList collection and uses an iterator to iterate through the process, deliberately remove an element in a step iteration, There will be fail-fast.
HashMap Occurrence Fail-fast: [Java]  View Plain  copy public static void main (string[] args)  {          Map<String, String> map = new HashMap<> ();          for  (int i = 0 ; i <  10 ; i ++ )  {               map.put (i+ "",  i+ "");         }          iterator<entry<string, string>> it = map.entryset (). Iterator ();         int i = 0;          while  (It.hasnext ())  {               if  (i == 3)  {            &Nbsp;       map.remove (3+ "");               }               entry<string, string> entry = it.next ();               system.out.println ("key= "  + entry.getkey ()  +   " and value= "  + entry.getvalue ());               i++;      }  }   The code defines a HashMap object and holds 10 key-value pairs, which, during the iterative traversal, remove an element by using the Remove method of the map, causing the concurrentmodificationexception exception to be thrown:
2. Multi-threaded environment: [Java]View plain copy public class Failfasttest {public static list<string> List = new arraylist<> ();
Related Article

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.