它不会抛出异常 ConcurrentModificationException

It does not throw exception ConcurrentModificationException(它不会抛出异常 ConcurrentModificationException)
本文介绍了它不会抛出异常 ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有以下代码,我希望它会抛出 ConcurrentModificationException,但它运行成功.为什么会这样?

I have the below code and I would expect it to throw a ConcurrentModificationException, but it runs successfully. Why does this happen?

public void fun(){
    List <Integer>lis = new ArrayList<Integer>();
    lis.add(1);
    lis.add(2);

    for(Integer st:lis){
        lis.remove(1);
        System.out.println(lis.size());
    }
}

public static void main(String[] args) {
    test t = new test();
    t.fun();
}

推荐答案

List 上的remove(int) 方法删除指定位置的元素.在开始循环之前,您的列表如下所示:

The remove(int) method on List removes the element at the specified position. Before you start your loop, your list looks like this:

[1, 2]

然后你在列表上启动一个迭代器:

Then you start an iterator on the list:

[1, 2]
 ^

您的 for 循环然后删除 位置 1 的元素,即数字 2:

Your for loop then removes the element at position 1, which is the number 2:

[1]
 ^

迭代器在下一个隐含的 hasNext() 调用中返回 false,循环终止.

The iterator, on the next implied hasNext() call, returns false, and the loop terminates.

如果您向列表中添加更多元素,您将收到 ConcurrentModificationException.然后隐式 next() 会抛出.

You will get a ConcurrentModificationException if you add more elements to the list. Then the implicit next() will throw.

请注意,来自 JCF 的 ArrayList 的 Javadoc:

As a note, from the Javadoc for ArrayList from the JCF:

请注意,无法保证迭代器的快速失败行为,因为一般来说,在存在不同步的并发修改的情况下无法做出任何硬保证.快速失败的迭代器会尽最大努力抛出 ConcurrentModificationException.因此,编写一个依赖此异常来确保其正确性的程序是错误的:迭代器的快速失败行为应该只用于检测错误.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

这可能实际上是Oracle ArrayList 迭代器实现中的一个错误;hasNext() 检查修改:

This is probably actually a bug in the Oracle ArrayList iterator implementation; hasNext() does not check for modification:

public boolean hasNext() {
    return cursor != size;
}

这篇关于它不会抛出异常 ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)