学习笔记—arraylist单线程下ConcurrentModificationException

之前一直有听说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个值的变化过程如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值