为什么ConcurrentHashMap不允许插入null值?

需要面试文档的扫描下方二维码

在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 (空) 值的解答,听懂的小伙伴,请关注点个赞,下次不迷路。

往期视频已经整理成文档形式,需要的小伙伴可扫描下方二维码,海量干货免费领!只弹干货不参水!

在这里插入图片描述

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tom弹架构

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值