具有空键功能的线程安全映射
我需要一个多线程 Map 对象在 Web 服务器的缓存中使用,并且需要有 null
键。
HashMap
允许我拥有 null 键,但 ConcurrentHashMap
不允许。我尝试使用 Collections.synchronizedMap(new HashMap())
创建 HashMap
的同步版本,但它也不接受 null
键。
我可以使用任何替代方法,而不必实现某种方法来包装 null
键吗?
I need a multi-threaded Map object to use in my web server's caching, and I need to have null
keys.
HashMap
allows me to have null keys, but ConcurrentHashMap
doesn't. I tried to create a synchronized version of HashMap
using Collections.synchronizedMap(new HashMap())
but it doesn't accept null
keys either.
Is there any alternative that I can use, without having to implement some way to wrap the null
keys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Collections.synchronizedMap
支持您提供的Map
的所有功能。如果你给它一个HashMap
,它支持null
键(还有null
值,你说“......我需要具有“空”键值...“,可以以任何方式读取)。这按预期工作,例如:
输出:
同步映射存在同步问题。如果您可以解决对
null
键和值的需求,ConcurrentHashMap
可能是服务器缓存的更好选择。但它“...不允许null
用作键或值。”请注意,您需要从
synchronizedMap
手动同步地图。 code> 如果你想迭代。来自 JavaDoc:地图会在
get
等方面为您处理它,但不会在迭代时处理。The
Map
returned byCollections.synchronizedMap
supports all of the features of theMap
you give it. If you give it aHashMap
, it supports thenull
key (and alsonull
values, you said "...I need to have "null" key values..." which can be read either way).This works as expected, for instance:
Output:
A synchronized map has the issue that, well, it's synchronized. If you can work around the need for
null
keys and values,ConcurrentHashMap
would probably be the better option for a server cache. But it "...does not allownull
to be used as a key or value."Note that you need to manually synchronize the map from
synchronizedMap
if you want to iterate. From the JavaDoc:The map handles it for you on
get
and such, but not iteration.据我所知,既没有简单的方法来制作 ConcurrentHashMap ,也没有支持 null 键或值的等效类。
ConcurrentHashMap
与Collections.synchronizedMap(new HashMap())
有很大不同。首先,因为即使所有访问都是只读的,同步映射也会阻止任何并发访问同时发生。 ConcurrentHashMap 不会阻止并发读取访问,在某些情况下甚至可能接受并发写入。
但更重要的是,如果在使用迭代器时修改了底层映射,则同步映射返回的迭代器很容易抛出ConcurrentModificationException。另一方面,即使在使用迭代器时底层映射发生更改,
ConcurrentHashMap
迭代器也保证永远不会抛出ConcurrentModificationException
。As far as I know there is neither a simple way to make
ConcurrentHashMap
nor an equivalent class supportingnull
keys or values.ConcurrentHashMap
is quite different fromCollections.synchronizedMap(new HashMap())
.First of all because a synchronized map will prevent any concurrent accesses to happen simultaneously even if all accesses are read only.
ConcurrentHashMap
won't block concurrent read accesses and, in some cases, may even accept concurrent writes.But the more important thing is that the
Iterator
s returned by a synchronized map are prone to throwConcurrentModificationException
if the underlying map is modified while using the iterator. On the other hand, theConcurrentHashMap
iterators' are guaranteed to never throwConcurrentModificationException
even if the underlying map is changed while using the iterator.