rehash本质上就是重新计算hash值而已
为了追溯,以下内容从上到下,一步步查看依赖关系。
//扩容函数中的transfer函数中遇到rehash,如果rehash为true就要重新计算hash值,否则不用重新计算
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
rehash取决于initHashSeedAsNeeded(newCapacity),看名字意思是初始化HashSeed,在需要的情况下
transfer(newTable, initHashSeedAsNeeded(newCapacity));
/*查看initHashSeedAsNeeded函数,返回值是switching,switching取决于 currentAltHashing ^ useAltHashing;这是个异或运算,currentAltHashinghashSeed = hashSeed != 0;hashSeed默认为0,因此currentAltHashinghashSeed默认为false;boolean useAltHashing = sun.misc.VM.isBooted() &&(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);前部分为true,后部分取决于Holder.ALTERNATIVE_HASHING_THRESHOLD。
*/
final boolean initHashSeedAsNeeded(int capacity) {
boolean currentAltHashing = hashSeed != 0;
boolean useAltHashing = sun.misc.VM.isBooted() &&
(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
boolean switching = currentAltHashing ^ useAltHashing;
if (switching) {
hashSeed = useAltHashing
? sun.misc.Hashing.randomHashSeed(this)
: 0;
}
return switching;
}
```cpp
/*
ALTERNATIVE_HASHING_THRESHOLD = threshold;
threshold = (null != altThreshold)? Integer.parseInt(altThreshold): ALTERNATIVE_HASHING_THRESHOLD_DEFAULT;
因此threshold又取决于altThreshold和ALTERNATIVE_HASHING_THRESHOLD_DEFAULT,
前者altThreshold = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction(
"jdk.map.althashing.threshold"));可以理解为你可以配置虚拟机参数如-D jdk.map.althashing.threshold=3,不配置就为null
后者ALTERNATIVE_HASHING_THRESHOLD_DEFAULT = Integer.MAX_VALUE;整数最大值
因此不配置altThreshold的时候,threshold为Integer.MAX_VALUE;整数最大值,这个时候ALTERNATIVE_HASHING_THRESHOLD=Integer.MAX_VALUE;整数最大值,再回到上面(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);一定为false, boolean switching = currentAltHashing ^ useAltHashing;中的useAltHashing为false,currentAltHashing默认为false,switching就为false,就不会初始化hashseed,也不会重新计算hash。
如果配置altThreshold的时候,ALTERNATIVE_HASHING_THRESHOLD=threshold=配置的值,(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD)如果成立,就为true,boolean switching = currentAltHashing ^ useAltHashing;中的useAltHashing为true,currentAltHashing默认为false,switching就为true,就会初始化hashseed,也会重新计算hash。
本质是什么呢,其实就是看你是否想要初始化hashseed,默认为零的,如果你要初始化hashseed,就要设置虚拟机参数,然后rehash才会有可能发生。
rehash的好处是什么呢,在扩容转移数据的时候,rehash后,重新计算的索引值就会变化概率更大,链表就有可能拆散分到数组上去,这样就加强了查询的效率。
*/
private static class Holder {
/**
* Table capacity above which to switch to use alternative hashing.
*/
static final int ALTERNATIVE_HASHING_THRESHOLD;
static {
String altThreshold = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction(
"jdk.map.althashing.threshold"));
int threshold;
try {
threshold = (null != altThreshold)
? Integer.parseInt(altThreshold)
: ALTERNATIVE_HASHING_THRESHOLD_DEFAULT;
// disable alternative hashing if -1
if (threshold == -1) {
threshold = Integer.MAX_VALUE;
}
if (threshold < 0) {
throw new IllegalArgumentException("value must be positive integer.");
}
} catch(IllegalArgumentException failed) {
throw new Error("Illegal value for 'jdk.map.althashing.threshold'", failed);
}
ALTERNATIVE_HASHING_THRESHOLD = threshold;
}
}