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