网站首页 > 文章精选 正文
在Java语言中,给ConcurrentHashMap和Hashtable这些线程安全的集合中的Key或者Value插入 null(空) 值的会报空指针异常,但是单线程操作的HashMap又允许 Key 或者 Value 插入 null(空) 值。这到底是为什么呢?
1、探寻源码
为了找到原因,我们先来看这样一段源码片段,打开ConcurrentHashMap的putVal()方法,源码中第一句就非常明确地做了判断,如果 Key 或者 Value 为 null(空) 值,就直接抛出空指针异常。
我们在源码中似乎已经找到了原因,你可以这样回答面试官,说JDK源码就是这么规定的。然而,这个原因是不能说服面试官的,虽然,源码是这样设计的,我们要思考的是,这样设计背后更深层次的原因。
那到底为什么ConcurrentHashMap不允许插入 null (空)值,HashMap又允许插入呢?
2、歧义问题
因为给ConcurrentHashMap中插入 null (空)值会存在歧义。我们可以假设ConcurrentHashMap允许插入 null(空) 值,那么,我们取值的时候会出现两种结果:
1、值没有在集合中,所以返回的结果就是 null (空);
2、值就是 null(空),所以返回的结果就是它原本的 null(空) 值。
这就产生了歧义问题。
那HashMap允许插入 null(空) 值,难道它就不担心出现歧义吗?这是因为HashMap的设计是给单线程使用的,所以如果取到 null(空) 值,我们可以通过HashMap的 containsKey(key)方 法来区分这个 null(空) 值到底是插入值是 null(空),还是本就没有才返回的 null(空) 值。
而 ConcurrentHashMap 就不一样了,因为 ConcurrentHashMap 是在多线程场景下使用的,它的情况更加复杂。
举个例子,现在有线程T1调用了 ConcurrentHashMap 的 containsKey(key) 方法,我们期望返回的结果是false,也就是说,T1并没有往ConcurrentHashMap中 put null(空)值。
但是,恰恰出了个意外,在线程T1还没有得到返回结果之前,线程T2又调用了ConcurrentHashMap 的 put() 方法,插入了一个Key,并且存入的Value是 null(空) 值。那么,线程T1 最终得到的返回结果就变成 true 了。
显然,这个结果和我们之前期望的 false 完全不一致。
也就是说,在多线程的复杂情况下,我们多线程的复杂情况下,到底是插入的 null(空) 值,还是本就没有才返回的 null(空) 值。也就是说,产生的歧义不能被 证 伪,
3、作者回复
对于 ConcurrentHashMap 不允许插入 null 值的问题,有人问过 ConcurrentHashMap 的作者 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 allowing
nulls in Maps (also Sets) is an open invitation for programs
to contain errors that remain undetected until
they break at just the wrong time. (Whether to allow nulls even
in non-concurrent Maps/Sets is one of the few design issues surrounding
Collections that Josh Bloch and I have long disagreed about.)
It is very difficult to check for null keys and values
in my entire application .
Would it be easier to declare somewhere
static final Object NULL = new Object();
and replace all use of nulls in uses of maps with NULL?
-Doug
以上信件的主要意思是,Doug Lea 认为这样设计最主要的原因是:不容忍在并发场景下出现歧义!
4、总结
ConcurrentHashMap在源码中加入不允许插入 null (空) 值的设计,主要目的是为了防止并发场景下的歧义问题。
以上就是我对关于ConcurrentHashMap为什么不允许插入 null (空) 值的解答,听懂的小伙伴,请关注点个赞,下次不迷路。
我是被编程耽误的文艺Tom,如果还有其他疑问或者需要高清无码图的小伙伴,可以关注我的主页加V。
S信【Tom】或【666】即可免费领取需要更多干货内容,还有海量面试资料,只弹干货不惨水!
本文为“Tom弹架构”原创,转载请注明出处。技术在于分享,我分享我快乐!
如果本文对您有帮助,欢迎关注和点赞;如果您有任何建议也可留言评论或私信,您的支持是我坚持创作的动力。
关注微信公众号『 Tom弹架构 』可获取更多技术干货!往期视频已经整理成文档形式,需要的小伙伴点个关注,搜索下方名片!我是被编程耽误的文艺Tom,
猜你喜欢
- 2025-03-18 系统性能优化与Java代码编写性能考虑
- 2025-03-18 面试必问之:Java 中 == 和 equals 的区别你知道吗
- 2025-03-18 为什么重写 equals时必须重写 hashCode 方法?
- 2025-03-18 一网打尽-HashMap面试题(面试hashmap底层实现原理)
- 2025-03-18 HashMap面试知识点合集,这一篇就够了
- 2025-03-18 Java并发系列 | ConcurrentHashMap源码分析
- 2025-03-18 ConcurrentHashMap的实现原理(JDK1.7和JDK1.8)
- 2025-03-18 HashMap底层实现原理以及线程安全实现
- 2025-03-18 不怕面试再问HashMap,一次彻底地梳理(原理+手写实现)
- 2025-03-18 java面试题——HashMap相关面试题
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 稳压管的稳压区是工作在什么区 (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)