之前一直有听说arraylist多线程不安全,遇到一个问题表明arraylist在单线程的情况下同样会出问题。
代码如下,没碰到这个问题之前我也理所当然的认为没问题:
while (undeleteFolders.size() != 0){
for (File folder : undeleteFolders) {
if (folder.listFiles().length == 0){
if (folder.delete()){
undeleteFolders.remove(folder);//问题发生
};
}
}
}
由于foreach循环是java语法糖的一种,在生成class编译器会将foreach循环直接转换成使用使用迭代器的代码,Java虚拟机根本不知道foreach的存在。即:
while(undeleteFolders.size() != 0) {
i = undeleteFolders.iterator();
while(i.hasNext()) {
folder = (File)i.next();
if(folder.listFiles().length == 0 && folder.delete()) {
undeleteFolders.remove(folder);
}
}
arraylist继承AbstractList,在AbstractList中Itr 内部类中实现了Iterator接口。实现代码如下:
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
* 下一次访问时返回的元素索引;
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
* 最近一次访问后一个或前一个元素索引,如果改元素被删除重置为-1;
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
* 迭代器认为支持的列表应该有modCount值,如果期望(即下方等式)被违反,迭代器能检测到并发修改;
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();//只要不相等就认为有下一个,并不检查大小
}
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;//执行remove操作将两个变量置为相等
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)//不满足抛出异常
throw new ConcurrentModificationException();
}
}
从上述代码可以看出主要的检查集中在下面的代码中:
public boolean hasNext() {
return cursor != size();//只要不相等就认为有下一个,并不检查大小
}
final void checkForComodification() {
if (modCount != expectedModCount)//不满足抛出异常
throw new ConcurrentModificationException();
}
其中变量cursor,expectedModCount属于内部类Itr,而modCount,size数据arraylist,且在整个迭代器内部只有cursor,正常的情况下其他的值应该不变。
执行过程的4个值的变化过程如下: