久久久久久久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)
主站蜘蛛池模板: 久久成人精品一区二区三区 | 91精品久久久久久久久久 | 成人国产精品一级毛片视频毛片 | 欧美在线综合 | 欧美日韩亚洲在线 | 一级黄a| 国产日韩欧美一区 | 91精品国产综合久久久久久漫画 | aⅴ色国产 欧美 | 久久aⅴ乱码一区二区三区 91综合网 | 国产一区在线免费观看 | 日本一区二区视频 | 三级视频久久 | 激情a | 国产在线视频在线观看 | 国产精品视屏 | 97精品超碰一区二区三区 | 最近免费日本视频在线 | 久久久久久国产精品久久 | 久久久久久久久久久久久久av | 日本小电影在线 | 国产视频线观看永久免费 | 亚洲一区日韩 | 日本一区二区三区免费观看 | 全免费a级毛片免费看视频免费下 | 成人免费在线观看 | 国产精品久久久久国产a级 欧美日韩国产免费 | 国产成人午夜电影网 | 国产91丝袜在线播放 | 欧洲高清转码区一二区 | www.亚洲精品 | 亚洲综合第一页 | 亚洲精品国产a久久久久久 午夜影院网站 | 日本高清不卡视频 | 2019天天操 | 国产精品国产 | 91视频亚洲| 丝袜 亚洲 另类 欧美 综合 | 久久久久一区二区三区 | 久久精品视频亚洲 | 成人影院av |