【问题标题】:No ConcurrentModificationException for CHM. Why?CHM 没有 ConcurrentModificationException。为什么?
【发布时间】:2014-05-14 16:55:19
【问题描述】:

我最近做了一个例子,我在迭代时将元素添加到ConcurrentHashMap

代码sn-p -

Map<String, String> map = new ConcurrentHashMap<String, String>();
map.put("ABC", "abc");
map.put("XYZ", "xyz");
map.put("MNO", "mno");
map.put("DEF", "def");
map.put("PQR", "pqr");

Iterator<Map.Entry<String, String>> entrySet = map.entrySet().iterator();
while(entrySet.hasNext()) {
        Map.Entry<String, String> entry = entrySet.next();
        System.out.println(entry.getKey()+", "+entry.getValue());
        map.put("TQR", "tqr");
}

但我无法找到在 CHM 的情况下代码不抛出 ConcurrentModificationException 的确切原因。

简而言之,是什么让 CHM 不像 HashMap 那样抛出 ConcurrentModificationException。

谢谢!

【问题讨论】:

  • “简而言之,是什么让 CHM 不像 HashMap 那样抛出 ConcurrentModificationException?”其实施;它被设计为同时使用。感兴趣的可以看看the implementation

标签: java map hashmap hashcode concurrenthashmap


【解决方案1】:

ConcurrentHashMap API 声明其迭代器不会抛出 ConcurrentModificationException。这是因为它的迭代器反映了创建迭代器时哈希表的状态。这就好像它的迭代器使用哈希表快照:

    ConcurrentHashMap m = new ConcurrentHashMap();
    m.put(1, 1);
    Iterator i = m.entrySet().iterator();
    m.remove(1);        // remove entry from map      
    System.out.println(i.next()); //still shows entry 1=1

【讨论】:

  • 因此,迭代器将始终仅包含在创建迭代器期间存在的那些值。它们不会包含甚至添加/删除值,即“TQR”到原始地图”。如果是这样,该条目是否会添加到 CHM 的某种克隆形式中?
  • 你的意思是迭代器正在迭代旧的(未更新的)对象?
  • 这很好,但是当我删除第二个或其他条目时,迭代器也会删除这些条目:为什么这样,我在这里问同样的问题:stackoverflow.com/questions/61440727/…
猜你喜欢
  • 2018-01-05
  • 1970-01-01
  • 2014-09-11
  • 2020-11-08
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多