久久久久久久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)
主站蜘蛛池模板: 国产精品三级在线 | 在线日韩一区 | 美日韩在线视频 | 欧美一区二区三区免费 | 一级免费片 | 精品久久一区二区 | 国产美女免费视频 | 国产成人在线观看免费网站 | 成年人免费在线观看 | 在线视频黄| 青青草福利视频 | 日本色综合| 中文字幕99 | 日韩综合一区 | txvlog.com| 欧美在线播放 | 日韩成人精品一区二区 | 亚洲精品在线观看视频 | 国产一区中文字幕 | 国产精品久久久久久久久 | 一级片黄色片 | 91久久精品日日躁夜夜躁欧美 | 黄色成年人视频 | 亚洲激情视频在线观看 | 日韩欧美专区 | 色爽| 午夜精品国产精品大乳美女 | 国产一级生活片 | 伊人久久影院 | 草逼com| 国产91av在线 | 日韩一级在线观看 | 涩久久 | 黄视频网站在线观看 | 免费av小说 | 操bbbbb | 香蕉视频一区二区 | 精品久久久久久一区二区里番 | 97国产在线 | 日韩免费网站 | 双性呜呜宫交受不住了h |