ConcurrentModificationException received during the qualification course of [Java Programmer, Silver SE 8](http://www.oracle.com/jp/education/certification/jse8-2489021-ja.html) conducted in the same effort. About questions related to
.java.util.ConcurrentModificationException
occurs when an unthread-safe java.util.ArrayList
instance is changed from another thread, but it also occurs in single-threaded processing. ..ConcurrentModificationException
that occurs even in a single thread.DenaiConcurrentModificationException.java
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
for (String str : list) {
if ("C".equals(str)) {
list.remove(str);
} else {
System.out.println(str);
}
}
}
ConcurrentModificationException
is thrown.DeruConcurrentModificationException.java
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
for (String str : list) {
if ("C".equals(str)) {
list.remove(str);
} else {
System.out.println(str);
}
}
}
ConcurrentModificationException
is thrown or not. As a rough test preparation, if you remove the penultimate or last element of the list, it will be thrown "not done", otherwise it will be "doed".This exception is thrown when a method that detects a parallel change in an object does not allow such a change.
For example, it is generally not allowed for one thread to iterate over a collection while another thread modifies the collection.
In such an environment, the results of iterative processing are usually not guaranteed.
Some iterators(Iterator)Implementation of(JREが提供するすべての一般的な目的のコレクションImplementation ofの、イテレータImplementation ofを含む)Is
You can choose to throw this exception if that behavior is detected.
The iterator that throws this exception is called a failfast iterator.
Iterators throw exceptions immediately and neatly to avoid the risk of unpredictable behavior at unpredictable points in the future.
This exception does not necessarily indicate that the object has not been updated in parallel by another thread.
If a single thread issues a set of methods that violate the object's conventions, the object throws this exception.
For example, if a thread modifies a collection directly while iterating over a collection with a failfast iterator.
The iterator throws this exception.
ConcurrentModificationException
is always thrown when a thread modifies a collection directly while iterating with *.java
/**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator<E> {}
With *, ConcurrentModificationException is thrown if there is a difference between expectedModCount and modCount every time the cursor moves. You can guess the role of the property from the name.
java
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
remove
is incrementing modCount, which causes ConcurrentModificationException
.java
/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
System.arraycopy (elementData, index + 1, elementData, index, numMoved);
, you can see that when the C element is deleted, the following element is moved up as shown below.
list.remove ("C ")
. Since 3 does not exist in the first stage, the processing ends as it is, the processing continues in the second stage, checkForComodification
is executed, and ConcurrentModificationException is thrown.Recommended Posts