问六: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表过大,占用过多的内存空间。

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

 

你可能感兴趣的:(Java常用类库)