List Iteration Times concurrentmodificationexception exceptions

Source: Internet
Author: User
Tags concurrentmodificationexception

View source code we can see that there is a modcount field in the list that will report an concurrentmodificationexception exception if the field is modified during the iteration.

Here's a look at the relevant source code

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/00/wKiom1XxbGSQG4e_AAViuYUvjKw407.jpg "title=" 2015-09-10_193952.png "alt=" Wkiom1xxbgsqg4e_aaviuyuvjkw407.jpg "/>


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/72/FD/wKioL1XxbqSC9bcRAAEs0pAU6Wk356.jpg "title=" 2015-09-10_194041.png "alt=" Wkiol1xxbqsc9bcraaes0pau6wk356.jpg "/>

In the initial state, the value of Modcount is 0, the value of Expectedmodcount at the beginning of the iteration is also 0, if an element is deleted during the iteration, the value of Modcount becomes 1 after modcount++, then the next iteration (called Next method) The Checkforcomodification method will be adjusted to check whether the Modcount and expectedmodcount are equal, obviously at this time the two are not equal, so throw an exception.


Here is the test code

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/01/wKiom1XxbW7yZyF6AAONRmuOP9w194.jpg "title=" 2015-09-10_193416.png "alt=" Wkiom1xxbw7yzyf6aaonrmuop9w194.jpg "/>


Careful readers will find that the above code, when equal to a or C when the deletion will throw an exception, and equal to B when the deletion will not throw an exception, in fact, this is a coincidence. Because, after the first iteration of the deletion of a, cursor=1,size=2, and then into the next iteration, so in next detects that Modcount and expectedmodcount are not equal, throw an exception. When the hypothesis equals B is deleted, then after the second iteration delete B, cursor=2,size=2,cursor and size are equal, so the Hasnext () method returns false, so no iterations, so there is no chance to error.



This article is from the "ten-li Rice flower" blog, please be sure to keep this source http://5880861.blog.51cto.com/5870861/1693563

List Iteration Times concurrentmodificationexception exceptions

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.