Java HashMap 的 size() 是否会与其实际条目不同步?尺寸?
我有一个名为 statusCountMap 的 Java HashMap。
调用 size() 结果为 30。
但如果我手动计算条目,则为 31
这是我的 TestNG 单元测试之一。下面的这些结果来自 Eclipse 的显示窗口(键入代码 -> 突出显示 -> 点击显示评估所选文本的结果)。
statusCountMap.size() (int) 30 statusCountMap.keySet().size() (int) 30 statusCountMap.values().size() (int) 30 statusCountMap (java.util.HashMap) {40534-INACTIVE=2, 40526-INACTIVE=1, 40528-INACTIVE=1, 40492-INACTIVE=3, 40492-TOTAL=4, 40513-TOTAL=6, 40532-DRAFT=4, 40524-TOTAL=7, 40526-DRAFT=2, 40528-ACTIVE=1, 40524-DRAFT=2, 40515-ACTIVE=1, 40513-DRAFT=4, 40534-DRAFT=1, 40514-TOTAL=3, 40529-DRAFT=4, 40515-TOTAL=3, 40492-ACTIVE=1, 40528-TOTAL=4, 40514-DRAFT=2, 40526-TOTAL=3, 40524-INACTIVE=2, 40515-DRAFT=2, 40514-ACTIVE=1, 40534-TOTAL=3, 40513-ACTIVE=2, 40528-DRAFT=2, 40532-TOTAL=4, 40524-ACTIVE=3, 40529-ACTIVE=1, 40529-TOTAL=5} statusCountMap.entrySet().size() (int) 30
什么给出?有人有过这样的经历吗?
我很确定 statusCountMap 此时没有被修改。
有 2 个方法(我们称之为 methodA 和 methodB)可以通过重复调用incrementCountInMap来并发修改statusCountMap。
private void incrementCountInMap(Map map, Long id, String qualifier) { String key = id + "-" + qualifier; if (map.get(key) == null) { map.put(key, 0); } synchronized (map) { map.put(key, map.get(key).intValue() + 1); } }
methodD 是我遇到问题的地方。 methodD 有一个 TestNG @dependsOnMethods = { "methodA", "methodB" } 因此当 methodD 执行时,statusCountMap 几乎已经是静态的了。 我提到这一点是因为它可能是 TestNG 中的一个错误。
我使用的是 Sun JDK 1.6.0_24。 TestNG 是 testng-5.9-jdk15.jar
嗯...重读我的帖子后,是否是因为并发执行了同步块外部 map.get(key) == null & map.put(key,0) 导致了这个问题?
I have a Java HashMap called statusCountMap.
Calling size() results in 30.
But if I count the entries manually, it's 31
This is in one of my TestNG unit tests. These results below are from Eclipse's Display window (type code -> highlight -> hit Display Result of Evaluating Selected Text).
statusCountMap.size() (int) 30 statusCountMap.keySet().size() (int) 30 statusCountMap.values().size() (int) 30 statusCountMap (java.util.HashMap) {40534-INACTIVE=2, 40526-INACTIVE=1, 40528-INACTIVE=1, 40492-INACTIVE=3, 40492-TOTAL=4, 40513-TOTAL=6, 40532-DRAFT=4, 40524-TOTAL=7, 40526-DRAFT=2, 40528-ACTIVE=1, 40524-DRAFT=2, 40515-ACTIVE=1, 40513-DRAFT=4, 40534-DRAFT=1, 40514-TOTAL=3, 40529-DRAFT=4, 40515-TOTAL=3, 40492-ACTIVE=1, 40528-TOTAL=4, 40514-DRAFT=2, 40526-TOTAL=3, 40524-INACTIVE=2, 40515-DRAFT=2, 40514-ACTIVE=1, 40534-TOTAL=3, 40513-ACTIVE=2, 40528-DRAFT=2, 40532-TOTAL=4, 40524-ACTIVE=3, 40529-ACTIVE=1, 40529-TOTAL=5} statusCountMap.entrySet().size() (int) 30
What gives ? Anyone has experienced this ?
I'm pretty sure statusCountMap is not being modified at this point.
There are 2 methods (lets call them methodA and methodB) that modify statusCountMap concurrently, by repeatedly calling incrementCountInMap.
private void incrementCountInMap(Map map, Long id, String qualifier) { String key = id + "-" + qualifier; if (map.get(key) == null) { map.put(key, 0); } synchronized (map) { map.put(key, map.get(key).intValue() + 1); } }
methodD is where I'm getting the issue. methodD has a TestNG @dependsOnMethods = { "methodA", "methodB" } so when methodD is executing, statusCountMap is pretty much static already.
I'm mentioning this because it might be a bug in TestNG.
I'm using Sun JDK 1.6.0_24. TestNG is testng-5.9-jdk15.jar
Hmmm ... after rereading my post, could it be because of concurrent execution of outside-of-synchronized-block map.get(key) == null & map.put(key,0) that's causing this issue ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信如果你在将键添加到 HashMap 后修改它,你就可以实现这一点。
然而,在您的情况下,这似乎只是在两个线程中修改同一映射而没有正确同步的情况。例如,在线程 A 中,map.put(key, 0)、线程 B 中的 map.put(key2, 0) 可能会导致大小为 1 或 2。如果对删除执行相同操作,最终可能会得到大于你应该。
I believe you can achieve this if you modify a key after it is added to a HashMap.
However in your case it appears to be just a case of modifying the same map in two threads without proper synchronization. e.g. in thread A, map.put(key, 0), thread B map.put(key2, 0) can results in a size of 1 or 2. If you do the same with remove you can end up with a size larger than you should.
总之……是的。
HashMap 不是线程安全的。因此,如果两个线程在没有适当同步的情况下更新 HashMap,则映射可能会进入不一致状态。即使其中一个线程只是读取,该线程也可能看到地图的不一致状态。
编写该方法的正确方法是:
In a word ... yes.
HashMap is not thread-safe. Therefore, if there is any point where two threads could update a HashMap without proper synchronization, the map could get into an inconsistent state. And even if one of the threads is only reading, that thread could see an inconsistent state for the map.
The correct way to write that method is:
如果您使用默认的初始容量 16 并以非线程安全的方式访问它们映射,那么您就会陷入不一致的状态。 Size 是 Map 中的一个状态成员,随着每个项目的输入而更新(size++)。这是因为地图本身是一个链表数组,无法真正返回其实际大小,因为它不指示其包含的项目数。一旦地图达到初始容量的百分比(load_factor),它就必须调整自身大小以容纳更多项目。如果恶意线程在调整地图大小时尝试添加项目,谁知道地图将处于什么状态。
If you using the default initial capacity of 16 and accessing them map in a non thread safe manner your ripe for an inconsistent state. Size is a state member in the Map getting updated as each item is entered(size++). This is because the map itself is an array of linked lists and cannot really return its actual size because its not indicative of the number of items it contains. Once the Map reaches a percentage(load_factor) of the intial capacity it has to resize itself to accomodate more items. If a rogue thread is attempting to add items as the map is resizing who knows what state the map will be in.
第一个map.put(..)不同步的问题。要么对其进行同步,要么使用
Collections.synchronizedMap(..)
。测试用例:我得到的一些结果:
The problem that the first map.put(..) isn't synchronized. Either synchronize it, or use
Collections.synchronizedMap(..)
. Test case:Some results I get: