问六:ConcurrentHashMap负载因子为什么是 0.75f ?

引入:

HashMap、ConcurrentHashMap、Hashtable的DEFAULT_LOAD_FACTOR(Hashtable没有这个参数,但是无参构造器直接传了0.75f,猜测是后面使用hash之后感觉这个挺好的,后面添加的)都是0.75f,当然,也都可以指定

 

API doc:

     * The main disadvantage of per-bin locks is that other  update

     * operations on other nodes in a bin list protected by  the same

     * lock can stall, for example when user equals() or  mapping

     * functions take a long time.  However, statistically,  under

     * random hash codes, this is not a common problem.   Ideally, the

     * frequency of nodes in bins follows a Poisson  distribution

     * (http://en.wikipedia.org/wiki/Poisson_distribution)  with a

     * parameter of about 0.5 on average, given the resizing  threshold

     * of 0.75, although with a large variance because of  resizing

     * granularity. Ignoring variance, the expected  occurrences of

     * list size k are (exp(-0.5) * pow(0.5, k) /  factorial(k)). The

     * first values are:

     *

     * 0:    0.60653066

     * 1:    0.30326533

     * 2:    0.07581633

     * 3:    0.01263606

     * 4:    0.00157952

     * 5:    0.00015795

     * 6:    0.00001316

     * 7:    0.00000094

     * 8:    0.00000006

     * more: less than 1 in ten million

     *

     * Lock contention probability for two threads accessing  distinct

     * elements is roughly 1 / (8 * #elements) under random  hashes.

 

这样的默认值其实就是时间和空间成本上的一种折中:

在ConcurrentHashMap中的注释中提到:一个节点在Bucket中出现的概率是符合泊松分布的,使用0.75为负载因子,可以降低节点在某一个特定桶中出现的概率,降低了hash 冲突,也就降低了查询时间(而查询是最频繁的操作(HashMap的get()与put()方法都要用到查询)),同时也不会因为负载因子过小而导致hash表过大,占用过多的内存空间。

不过这个参数也可以手动调整。

 

全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
点赞 评论 收藏
分享
06-27 18:53
门头沟学院 Java
这样才知道自己不适合搞代码,考公去咯
只爱喝白开水:我也发现不适合搞代码,打算转非技术方向了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务