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

Java 中的 Iterator 如何知道何時拋出 ConcurrentModif

How does an Iterator in Java know when to throw ConcurrentModification Exception(Java 中的 Iterator 如何知道何時拋出 ConcurrentModification 異常)
本文介紹了Java 中的 Iterator 如何知道何時拋出 ConcurrentModification 異常的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下代碼引發 ConcurrentModificationException,因為我在同一個列表上使用了兩個不同的迭代器,其中一個正在修改列表.所以,第二個迭代器在讀取列表時會拋出異常,因為其他迭代器已經修改了列表.

I have the following code which throws ConcurrentModificationException because I am using two different iterators on the same list and one of them is modifying the list. So, the second iterator throws the exception when reading the list because some other iterator has modified the list.

    List<Integer> list = new ArrayList<>();

    populate(list);//A method that adds integers to list

    ListIterator<Integer> iterator1 = list.listIterator();
    ListIterator<Integer> iterator2 = list.listIterator();

    while (iterator1.hasNext()) {
        if(iterator1.next() < 5)
            iterator1.remove();
    }

    while (iterator2.hasNext()){
        if(iterator2.next() < 5) {
         //Call handler   
        }
    }

我的問題是 iterator2 如何知道 internally 如果 list 尚未到達某個元素,則它已被其他迭代器修改iterator1 還刪除了哪個?它如何確定其他一些 iterator 已經改變了 list?一種方法是跟蹤大小,但這不是原因,因為其他一些迭代器可以替換任何元素.

My question is how does iterator2 know internally that the list has has been modified by some other iterator if it has not reached an element which is yet removed by iterator1? How does it figure out that some other iterator has mutated the list? One way could be keep track of size but that can't be the reason since some other iterator can just replace any element.

推薦答案

回答此類問題的一個好方法是查看源代碼,例如 ArrayList 的源代碼.搜索 ConcurrentModificationException.

A good way to answer questions like this is to look at the source code, for example the source code for ArrayList. Search for ConcurrentModificationException.

你應該能夠說事情是這樣工作的:

You should be able to tell that things work rather like this:

  • 集合對象有一個修改計數,該計數從零開始,并在發生添加或刪除或類似操作時增加.
  • 創建迭代器對象時,我們將集合的當前修改計數存儲在迭代器中.
  • 每次使用迭代器時,它都會檢查集合的 mod 計數與迭代器在創建時獲得的 mod 計數.如果這些值不同,則會引發異常.

在您的情況下,列表中 iterator1 執行的刪除操作會更改列表的結構操作計數 (modCount).當 iterator2 被要求刪除時,它會看到它最初收到的 expectedModCount 為 0,與列表的當前 mod 計數不同.

In your case, remove operations performed by iterator1 on the list change the structural operation count (modCount) of the list. When iterator2 is asked to remove, it sees its expectedModCount, which it received initially as 0, differing from the current mod count of the list.

需要注意的是,it.remove 是一個特例.當迭代器自己刪除時,它的 expectedModCount 會相應地調整,以與底層列表保持同步.

It should be noted that it.remove is a special case. When an iterator does a remove itself, its expectedModCount adjusts accordingly, to keep in sync with the underlying list.

這篇關于Java 中的 Iterator 如何知道何時拋出 ConcurrentModification 異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 国产xxx在线观看 | 91综合网| 丁香综合| 欧美日韩a| a在线免费观看视频 | 一区二区日韩精品 | 欧美理论片在线 | 九九热精品视频 | 免费中文字幕 | 伊人性伊人情综合网 | 免费黄色录像视频 | 中文字幕一区二区三区精彩视频 | 综合国产 | 久久精品视频在线观看 | 精品一区二区三区在线观看国产 | 日韩手机在线看片 | 欧美不卡一区二区三区 | 一区二区国产精品 | 国产主播第一页 | 一区二区三区免费 | 蜜桃视频麻豆 | 羞羞视频免费观看入口 | 成人亚洲| 亚洲高清视频一区二区 | 国产在线播放一区二区三区 | 一区二区三区视频在线观看 | 国产精品成人一区二区三区 | 91成人在线 | 欧洲成人 | 青娱乐自拍 | 欧美高清视频一区 | 在线免费观看成人 | 久久久久综合 | 国产精品免费播放 | 精品不卡 | 六月色婷 | a级片www| 亚洲欧洲一区二区 | 91一区| 中文字幕97 | 在线视频中文字幕 |