簡體   English   中英

為什么Hashmap快速失敗不會在調整大小功能中發生? (用於多線程競爭條件問題)

[英]Why Hashmap fail-fast not happen in the resize function? (for multiple thread race condition issue)

所有,我試圖理解Hashmap調整大小函數的多線程競爭條件問題。

正如我從這里讀到的。 競態條件問題將導致條目列表的無限循環鏈接。

我已經知道Hashmap具有故障快速機制,可以立即停止多個線程對其進行訪問。 以下代碼顯示了這一點。

if (modCount != expectedModCount)
                throw new ConcurrentModificationException();

我的問題是為什么快速失敗不能用於調整大小功能? 我調試的代碼如下(jdk1.7)

void transfer(Entry[] newTable, boolean rehash) {
        int newCapacity = newTable.length;
        for (Entry<K,V> e : table) {
            while(null != e) {
                Entry<K,V> next = e.next;
                if (rehash) {
                    e.hash = null == e.key ? 0 : hash(e.key);
                }
                int i = indexFor(e.hash, newCapacity);
                e.next = newTable[i];
                newTable[i] = e;
                e = next;
            }
        }
    }

因為for不使用Iterator嗎?

更新

還是因為resize函數不使用Put*Remove*Clear*方法會導致modCount值更改? 請幫忙確認一下。(原諒我英語不好。)

謝謝。

HashMap的Javadoc對此進行了解釋: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: 

從而

快速失敗以立即停止多線程

不能保證。 並且任何不同步的並發修改都可能導致未定義狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM