About the concurrentmodificationexception of list

Source: Internet
Author: User
Tags concurrentmodificationexception

The operation of the ArrayList can be accessed by means of an index image, or accessed through iterator, as long as no modification to the ArrayList structure will cause concurrentmodificationexception, modifying ArrayList with an index alone does not cause the problem, causing the problem to be mixed with indexes and iterator. You can use the source code of the JDK to illustrate the problem.

First look at the code inside the Abstractlist iterator:

Java code
  1. /**
  2. * There is a expectedmodcount variable inside the iterator,
  3. * Each time the variable initializes *iterator equals ArrayList Modcount,modcount records the number of structural modifications to the ArrayList,
  4. * The Expectedmodcount and Modcount are synchronized when the structure of the ArrayList is modified by iterator.
  5. * But if the structure of ArrayList is modified by means of an index while accessing through iterator,
  6. * Due to the way the index is only modified modcount will not be modified synchronously expectedmodcount will cause
  7. *modcount and Expectedmodcount are not equal, they throw concurrentmodificationexception,
  8. * This is the fail-fast of iterator, fast failure. So as long as a way to operate ArrayList will not be a problem,
  9. * Of course ArrayList is not thread-safe and is not discussed here for threading issues.
  10. *
  11. */
  12. int expectedmodcount = Modcount;
  13. Public E Next () {
  14. Checkforcomodification (); //Determine if Modecount and expectedmodcount are equal and throw exceptions if they are not equal
  15. try {
  16. E next = get (cursor);
  17. Lastret = cursor++;
  18. return next;
  19. } catch (Indexoutofboundsexception e) {
  20. Checkforcomodification ();
  21. throw new Nosuchelementexception ();
  22. }
  23. }
  24. Public void Remove () {
  25. if (Lastret = =-1)
  26. throw new IllegalStateException ();
  27. Checkforcomodification (); //To determine whether Modecount and Expectedmodcount are equal
  28. try {
  29. Abstractlist.  This.remove (Lastret);
  30. if (Lastret < cursor)
  31. cursor--;
  32. Lastret =-1;
  33. Expectedmodcount = Modcount; //Here to modify the structure of the ArrayList, so the Expectedmodcount in Modcount synchronization, mainly AbstractList.this.remove (Lastret);  The Remove method will be modcount++, resulting in modcount and expectedmodcount are not equal.
  34. } catch (Indexoutofboundsexception e) {
  35. throw new Concurrentmodificationexception ();
  36. }
  37. }
  38. Determine if Modcount and expectedmodcount are equal, and if not equal, throw concurrentmodificationexception anomalies
  39. Final void Checkforcomodification () {
  40. if (modcount! = expectedmodcount)
  41. throw new Concurrentmodificationexception ();
  42. }

Therefore, the conclusion is: the operation of the ArrayList is a traversal, either indexed, or iterator do not mix.

Here is an explanation of what others have seen online: wrote

Iterator is working in a separate thread, and has a mutex lock. Iterator is created after the creation of a single-chain index table pointing to the original object, when the original object number changes, the contents of the index table will not be synchronized changes, so when the index pointer moves back to find the object to iterate, so according to Fail-fast principle Iterator The java.util.ConcurrentModificationException exception is thrown immediately.
So Iterator is not allowed to be changed by iterative objects while working. But you can delete the object using Iterator's own method remove (), and the Iterator.remove () method maintains the consistency of the index while deleting the current iteration object

About the concurrentmodificationexception of list

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.