国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

java - HashTable有沒有採(cǎi)用快速失敗機(jī)制?
大家講道理
大家講道理 2017-04-18 10:55:08
0
1
1142

1,http://www.yq1012.com/api/jav...
由所有類的“collection 視圖方法”返回的 collection 的 iterator 方法返回的迭代器都是快速失敗 的:在創(chuàng)建 Iterator 之後,如果從結(jié)構(gòu)上對(duì) Hashtable 進(jìn)行修改,除非通過 Iterator 自身的 remove 方法,否則在任何時(shí)間以任何方式對(duì)其進(jìn)行修改,Iterator 都將拋出ConcurrentModificationException。因此,麵對(duì)並發(fā)的修改,Iterator 很快就會(huì)完全失敗,而不冒在將來某個(gè)不確定的時(shí)間發(fā)生任意不確定行為的風(fēng)險(xiǎn)。由 Hashtable 的鍵和元素方法返回的 Enumeration 不 是快速失敗的。

有地方說因?yàn)镠ashTable做了線程同步,所以沒有采用快速失敗機(jī)製

2,但是源碼中hashtable.keySet.iterator 返回的iterator中有 做判斷
比如說iterator的remove方法 (在類: private class Enumerator<T> implements Enumeration<T>, Iterator<T>中)

 public void remove() {
            if (!iterator)
                throw new UnsupportedOperationException();
            if (lastReturned == null)
                throw new IllegalStateException("Hashtable Enumerator");
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();

            synchronized(Hashtable.this) {
                Entry[] tab = Hashtable.this.table;
                int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;

                for (Entry<K,V> e = tab[index], prev = null; e != null;
                     prev = e, e = e.next) {
                    if (e == lastReturned) {
                        modCount++;
                        expectedModCount++;
                        if (prev == null)
                            tab[index] = e.next;
                        else
                            prev.next = e.next;
                        count--;
                        lastReturned = null;
                        return;
                    }
                }
                throw new ConcurrentModificationException();
            }
        }
        
        
          public T next() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            return nextElement();
        }
        

上麵兩端代碼中都有驗(yàn)證
if (modCount != expectedModCount)

            throw new ConcurrentModificationException();
            
            所以多線程環(huán)境下并發(fā)修改hashtable 也是會(huì)引起Iterator迭代失敗,我代碼測(cè)試過
            
            
            

這個(gè)該怎麼理解,請(qǐng)問上麵提到的哪種情況是正確的

大家講道理
大家講道理

光陰似箭催人老,日月如移越少年。

全部回覆(1)
伊謝爾倫

Hashtable的iterator遍歷方式支援fast-fail,用Enumeration不支援fast-fail

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板