久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

它不會拋出異常 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
egit plugin for Eclipse(Eclipse 的 egit 插件)
Gitlab cannot open git-upload-pack error(Gitlab 無法打開 git-upload-pack 錯誤)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
How to add GitLab repository in Eclipse?(如何在 Eclipse 中添加 GitLab 存儲庫?)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
主站蜘蛛池模板: 久久成人免费视频 | 福利视频一区二区三区 | 成人高清在线视频 | 人妖av| 男人的天堂亚洲 | 国产精品一码二码三码在线 | 亚洲国产精品99久久久久久久久 | 欧美综合一区二区三区 | 成人午夜免费在线视频 | 在线中文字幕av | 国产91 在线播放 | 精品一区二区三区免费毛片 | 久久久久国 | 久久亚洲一区二区 | www.四虎.com | 国产精品亚洲成在人线 | 精国产品一区二区三区 | 色黄网站 | 日韩精品一二三 | 日本不卡免费新一二三区 | 天天曰夜夜操 | 久久欧美精品 | av大全在线观看 | 亚洲视频二区 | 精品美女久久久久久免费 | 精久久| 一级黄色毛片 | 中文字幕在线观看国产 | 国产精品久久久久久久 | 久久国产精99精产国高潮 | 免费久草| 国内精品久久久久久影视8 最新黄色在线观看 | 国产精品久久久久国产a级 欧美日韩国产免费 | 一级片免费视频 | 国产精品视频免费 | 中文字幕不卡在线观看 | 99热精品在线 | 亚洲成人黄色 | 亚洲一区二区不卡在线观看 | 色综合一区二区三区 | 久久在线|