这与Java 7u6的改进有关,并已在Java 8中删除.
相关文件:
The alternative hash function improves the performance of these map
implementations when a large number of key hash collisions are
encountered.
For Java SE 7u6, this alternative hash function is implemented as
follows:
The alternative hash function is only applied to maps with a capacity
larger than a specified threshold size. By default, the threshold is
-1. This value disables the alternative hash function. To enable the
alternative hash function, set the jdk.map.althashing.threshold
system property to a different value. The recommended value is 512.
Setting this system property to 512 causes all maps with a capacity
larger than 512 entries to use the alternative hash function. You can
set this system property to 0, which causes all maps to use the
alternative hash function.
…
和
The alternative String hash function added in 7u6 has been removed
from JDK 8, along with the jdk.map.althashing.threshold system
property. Instead, hash bins containing a large number of colliding
keys improve performance by storing their entries in a balanced tree
instead of a linked list.
现在,回答你的问题:
How and when we have to use that class?
是什么让你认为你应该使用它?这是一个私人课程without any public docs,所以你不必关心它.这是Oracle HashMap的实现细节,您无法直接使用它.您可以间接使用它的唯一方法是通过jdk.map.althashing.threshold系统属性.
为什么Oracle的工程师会使用这样的持有者?因为类加载顺序. VM很难在彼此有很多依赖关系的情况下加载所有类,它可能会被卡住.因此,所有内部类的开发人员都要确保他们没有使用可能尚未加载的类的方法/属性/可能导致加载顺序问题.
这是一个这样的帮助程序,它只在加载所有其他类并且VM完全启动后初始化一个值.只有在第一次访问类时才会初始化holder中的值,并且使用sun.misc.VM.isBooted()调用屏蔽此访问.