Concurrent modification of synchronization containers

Source: Internet
Author: User

1. Synchronization containers are thread-safe. In some scenarios, locks are required to protect compound operations.
2. Common Compound operations include iteration (repeatedly accessing elements, traversing all elements in the container), jump (finding the next element of the current element in the specified order), and conditional operations.
3. In these composite operations, a java. util. concurrentmodificationexception will be thrown During Concurrent modifications (add or remove) containers. In the early design of the iterator, concurrent modification was not considered.
...
Vector v = new vector <> ();
V. Add ("1 ");
V. Add ("2 ");
V. Add ("3 ");
...

Public Collection <string> m2 (vector <string> List) {iterator <string> iterator = List. iterator (); While (iterator. hasnext () {string temp = iterator. next (); If ("3 ". equals (temp) {list. remove (temp) ;}} return list ;}

4. Read the source code and you can see that the member variables modcount and expectedmodcount (expected number of modifications, initial value modcount) are defined in the javasactlist class ), A concurrentmodificationexception is thrown when these two values are not equal in the case of multi-thread concurrency modification.

 
Final void checkforcomodification () {If (modcount! = Expectedmodcount) throw new concurrentmodificationexception ();}

Concurrent modification of synchronization containers

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.