Exception Concurrentmodificationexception_java the Java Collection concurrency operation
Source: Internet
Author: User
Like the container map in Java:
for (person Person:plist) {
if (Person.getgender () ==gender.male) {
Plist.remove (person); The remove operation cannot be performed during traversal
}
}
The map usually obtains the set set of its key values when traversing, and then iterates through the map using the iterator iterator.
Note that during the traversal process, only the elements in the map can be handled appropriately, the map element cannot be added or reduced by the map element, that is, the size of the map cannot be changed, and an exception can occur (you cannot modify the deletion or increase the elements in the map during traversal).
The reported exception is java.util.ConcurrentModificationException exception
public class Concurrentmodificationexceptionextends RuntimeException
This exception is thrown when the method detects concurrent modifications to the object, but does not allow this modification.
For example, when a thread iterates over a Collection, it usually does not allow another linear modification of the Collection. Usually in these cases, the result of the iteration is ambiguous. If this behavior is detected, some iterator implementations (including all common collection implementations provided by the JRE) may choose to throw this exception. The iterator that performs this operation is called a fast fail iterator, because the iterator will soon fail completely without risking any uncertainty at any future time.
Note that this exception does not always indicate that the object has been modified concurrently by different threads. The object may throw this exception if a single-threaded emit a method call sequence that violates an object contract. For example, an iterator throws this exception if the thread modifies the collection directly when it uses the quick fail iterator to collection over the iteration.
Note that the rapid failure behavior of iterators is not guaranteed because, in general, it is not possible to make any hard guarantees about whether or not to have a different step of concurrent modification. Fast failure operations will do their best to throw concurrentmodificationexception. Therefore, it is wrong to write a program that relies on this exception to improve the correctness of such operations: Concurrentmodificationexception should only be used to detect bugs.
When you try to modify Collection/map content directly during an iterative operation of the Collection or Map using Fail-fast iterator, even if you are running in a single thread, Java.util.ConcurrentModificati The onexception exception will also be thrown.
Iterator is working in a separate thread and has a mutex lock. Iterator is created to create a single chain index table that points to the original object, and when the original number of objects changes, the contents of this index table will not be synchronized, so when the index pointer moves back, you can't find the object to iterate, so follow the fail-fast principle iterator Will throw the java.util.ConcurrentModificationException exception immediately.
So iterator is not allowed to be changed by the object of the iteration at work. But you can use iterator's own method remove () to delete an object, and the Iterator.remove () method maintains the consistency of the index while deleting the current iteration object.
The interesting thing is that if your Collection/map object actually has only one element, the concurrentmodificationexception exception will not be thrown. This is why it is pointed out in Javadoc that: it would is wrong to write a, "depended on", exception for its correctness:concurren Tmodificationexception should is used to detect bugs.
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