前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >八股文:为什么HashMap的键值可以为null,而ConcurrentHashMap不行?

八股文:为什么HashMap的键值可以为null,而ConcurrentHashMap不行?

作者头像
崔认知
发布2024-06-17 13:40:34
1120
发布2024-06-17 13:40:34
举报
文章被收录于专栏:nobodynobody

在Java中,ConcurrentHashMap这个线程安全的集合中的Key或者Value是不允许 null(空)值出现,但是非线程安全的HashMap又允许Key或者Value插入null(空)值。why?

探寻源码

ConcurrentHashMap的put方法对key和value做了非空判断,如果为空,会抛出空指针异常:

那为什么ConcurrentHashMap如此设计呢?

并发歧义问题

对于ConcurrentHashMap不允许插入null值的问题,有人问过作者Doug Lea,以下是他回复的邮件内容:

The main reason that nulls aren't allowed in ConcurrentMaps(ConcurrentHashMaps, ConcurrentSkipListMaps) is that ambiguities that may be just barely tolerable in non-concurrent maps can't be accommodated. The main one is that if map.get(key) returns null, you can't detect whether the key explicitly maps to null vs the key isn't mapped.In a non-concurrent map, you can check this via map.contains(key),but in a concurrent one, the map might have changed between calls.

Further digressing: I personally think that allowingnulls in Maps (also Sets) is an open invitation for programsto contain errors that remain undetected untilthey break at just the wrong time. (Whether to allow nulls evenin non-concurrent Maps/Sets is one of the few design issues surroundingCollections that Josh Bloch and I have long disagreed about.)

It is very difficult to check for null keys and valuesin my entire application . Would it be easier to declare somewherestatic final Object NULL = new Object();and replace all use of nulls in uses of maps with NULL?

-Doug

————————————————

Doug Lea认为这样设计最主要的原因是:不容忍在并发场景下出现歧义

在单线程环境中,不会存在一个线程操作该 HashMap 时,其他的线程将该 HashMap 修改的情况,可以通过 contains(key)来做判断是否存在这个键值对,从而做相应的处理。

而在多线程环境下,可能会存在多个线程同时修改键值对的情况,这时是无法通过contains(key)来判断键值对是否存在的,这会带来一个二义性的问题,Doug Lea说二义性是多线程中不能容忍的!

结论

ConcurrentHashMap在源码中加入不允许插入null(空)值的设计,主要目的是为了防止并发场景下的歧义问题。

也就是说,当一个线程从ConcurrentHashMap获取某个key,如果返回的结果是null的时候。

这个线程无法确认,这个null表示的是确实不存在这个key,还是说存在key,但是value为空。

这种不确定性会造成线程安全性问题,而ConcurrentHashMap本身又是一个线程安全的集合。

所以才这么设计。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-06-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 认知科技技术团队 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档