LinkedList run out of the concurrentmodificationexception anomaly

Source: Internet
Author: User
Tags concurrentmodificationexception

Project, the Concurrentmodificationexception exception is thrown in the background when using LinkedList

See the source code found the problem, the analysis is as follows:

1. The outermost method of the exception (the method that throws the exception directly):

  final void Checkforcomodification ()    {      if (LinkedList.this.modCount! = this.expectedmodcount) {        throw New Concurrentmodificationexception ();}}}  
The field of the Modcount:linkedlist object, add (), and the Remove () action will perform the + + on the field.

Expectedmodcount: The iterator iterator the field of the object, and also the Add (), the remove operation will increment the field value.


2. Impersonation of the business method that throws the exception call:

<pre name= "code" class= "Java" >public class Test {public    static void Main (string[] args)  {        arraylist& lt;integer> list = new arraylist<integer> ();        List.add (2);        iterator<integer> Iterator = List.iterator ();        while (Iterator.hasnext ()) {            integer integer = Iterator.next ();            if (integer==2)                list.remove (integer);}}    }

InThe List object executes
List.add (2);
after its modcount ==1, and then generates iteratorobject, iterator of the Expectedmodcount==1, at which point the two values are consistent.

When executing <span style= "Font-family:verdana, Arial, Helvetica, Sans-serif;" >list.remove (integer); </span><span style= "Font-family:verdana, Arial, Helvetica, Sans-serif;" After >, note this time, </span><span style= "Font-family:verdana, Arial, Helvetica, Sans-serif;" >modcount ==2, while Expectedmodcount==1</span><span style= "Font-family:verdana, Arial, Helvetica, Sans-serif ; font-size:18px; line-height:28px; " ></span>

Down, as normal thinking, to execute again

while (Iterator.hasnext ()), the intuition is that I added an object to come in, this time, there should be no next element, so here will not come in.

The problem is here, because we just called list.remove (integer);
After execution, there is no effect on the internal iterator, so execute again

while (Iterator.hasnext ())
, the iterator internal judgment has not reached size, so it will continue to execute and then execute
Iterator.next ();
At this point, the next () method is executed first
Checkforcomodification ();
FoundModcount andExpectedmodcount inconsistent, throws an exception.

Speaking of which, everyone already understands.

Here's how to fix it:

public static void Main (string[] args)  {        linkedlist<integer> list = new linkedlist<integer> ();        List.add (2);        iterator<integer> Iterator = List.iterator ();        while (Iterator.hasnext ()) {            integer integer = Iterator.next ();            if (integer==2) {            iterator.remove ();}}    }

Welcome to Exchange, reproduced please specify the source: http://blog.csdn.net/smithdoudou88/article/details/48131107


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

LinkedList run out of the concurrentmodificationexception anomaly

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.