神奇的0x61c88647——浅析ThreadLocal

0x61c88647的特点

这个数是Integer有符号整数的0.618倍,既黄金比例,斐波拉契数列。使用这个比例,可以使key在数组上被更均匀的分散。

首先来看一个例子:

private static final int HASH_INCREMENT = 0x61c88647;

public static void main(String[] args) {
magicHash(16);
magicHash(32);
}

private static void magicHash(int size){
int hashCode=0;
for(int i=0;i<size;i++){
hashCode=i*HASH_INCREMENT+HASH_INCREMENT;
System.out.print((hashCode&(size-1))+"" "");
}
System.out.println("""");
}

输出结果:

image-20200924185509082

使用这个来累加获取到的数组下标,他的数据分布是均匀的,也就是说,当我从任意位置开始检索某一个值的时候,概率上是一样的。(这一点在使用ThreadLocalMap的线性探测法时,可以很快就能探测到下一个临近的可用slot,从而保证效率)

ThreadLocal源码解析

ThreadLocal实际上一种线程隔离机制,也是为了保证在多线程环境下对于共享变量的访问的安全性。

public class ThreadLocalDemo {

static ThreadLocal<Integer> local=new ThreadLocal<Integer>(){
protected Integer initialValue(){
return 0; //初始化一个值
}
};

public static void main(String[] args) {
Thread[] thread=new Thread[5];
for (int i=0;i<5;i++){
thread[i]=new Thread(()->{
int num=local.get(); //获得的值都是0
local.set(num+=5); //设置到local中
System.out.println(Thread.currentThread().getName()+""-""+num);
});
}
for (int i = 0; i < 5; i++) {
thread[i].start();
}
}

}

结果如下:

image-20200923234845046

说明ThreadLocal的对象,在每一个线程中都是单独存在,互不干扰。

那么ThreadLocal是如何做到线程隔离机制的,我们直接来看源码:

image-20200924162616212
  • protected T initialValue() 设置并返回当前线程变量的一个初始值
  • set(T value) 将信息 value 放到当前线程的 thread-local 变量中
  • T get() 获取set(T value)设置的值, 如果没有则返回初始值
  • remove() 移除线程中的这个 thread-local 变量

set()方法

/**
* Sets the current thread's copy of this thread-local variable
* to the specified value. Most subclasses will have no need to
* override this method, relying solely on the {@link #initialValue}
* method to set the values of thread-locals.
*
* @param value the value to be stored in the current thread's copy of
* this thread-local.
*/
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}

看注释可以知道,当前线程set的其实是这个ThreadLocal变量的副本,所以每个线程访问的是自己内部的副本变量以达到线程隔离的效果。

set()方法首先会获取到当前线程,然后从这个线程获取ThreadLocalMap,这个集合是存放在Thread类中的,而不是ThreadLocal类中:

/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;

注释:(存放)与此线程相关的ThreadLocal值,这个map由ThreadLocal类维护。

所以这个ThreadLocalMap就是存放每条线程的ThreadLocal的副本。

ThreadLocalMap

/**
* The entries in this hash map extend WeakReference, using
* its main ref field as the key (which is always a
* ThreadLocal object). Note that null keys (i.e. entry.get()
* == null) mean that the key is no longer referenced, so the
* entry can be expunged from table. Such entries are referred to
* as ""stale entries"" in the code that follows.
*/
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;

Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}

注释:这个Entry继承了弱引用,使用ThreadLocal对象作为key,当key==null时,意味着不再引用该键,因此可以从Entry中删除该项。被删除的数据在后文被称作“stale entries”即不干净的Entry(后面有一个replaceStaleEntry()方法替换掉“stale entries”)。

所以ThreadLocalMap其实是通过Entry来存放数据的,并且它和ThreadLocal是WeakReference弱引用关系(清理key==null的数据)。

弱引用:当一个对象仅仅被weak reference(弱引用)指向, 而没有任何其他strong reference(强引用)指向的时候, 如果这时GC运行, 那么这个对象就会被回收,不论当前的内存空间是否足够,这个对象都会被回收。

ThreadLocalMap为空时

继续看如何创建ThreadLocalMap,当从当前线程获取到的map为空时,会创建一个ThreadLocalMap,并将这个ThreadLocal实例和设置的值存入这个map:

/**
* Create the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
*
* @param t the current thread
* @param firstValue value for the initial entry of the map
*/
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}

进入new ThreadLocalMap(this, firstValue)构造方法:

/**
* Construct a new map initially containing (firstKey, firstValue).
* ThreadLocalMaps are constructed lazily, so we only create
* one when we have at least one entry to put in it.
*/
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}

注释:创建一个初始值包含threadLocal和firstValue值的map,ThreadLocalMaps是延时构造的,所以在至少有一个entry的时候才会创建。

看代码:table = new Entry[INITIAL_CAPACITY];首先来看这个table

/**
* The table, resized as necessary.
* table.length MUST always be a power of two.
*/
private Entry[] table;

是ThreadLocalMap的一个属性,一个Entry[] 数组,可扩容,长度必须是2的幂。而new Entry[INITIAL_CAPACITY]则是对其的初始化,INITIAL_CAPACITY是其初始化长度16。

int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);:这里是为了产生table[]数组的下标,首先来看threadLocalHashCode 属性:

private final int threadLocalHashCode = nextHashCode();

继续:

/**
* Returns the next hash code.
*/
private static int nextHashCode() {
return nextHashCode.getAndAdd(HASH_INCREMENT);
}

先来看nextHashCode:

/**
* The next hash code to be given out. Updated atomically. Starts at
* zero.
*/
private static AtomicInteger nextHashCode =
new AtomicInteger();

注释说明了这个属性是下一个要给出的hashCode(更新这个属性是原子性的,且从零开始)。

再来看nextHashCode.getAndAdd(HASH_INCREMENT);:表示下一个要给出的hashCode的按HASH_INCREMENT的值累加上去的,此处我们找到了这个神奇的0x61c88647

/**
* The difference between successively generated hash codes - turns
* implicit sequential thread-local IDs into near-optimally spread
* multiplicative hash values for power-of-two-sized tables.
*/
private static final int HASH_INCREMENT = 0x61c88647;

综上所述:创建ThreadLocalMap时,会将ThreadLocal和value存放入一个Entry(就是前文弱引用的那个),并将这个Entry放入一个Entry[]数组中,下标的值按累加0x61c88647的值与上(INITIAL_CAPACITY - 1)获取到。而后续,当线程每次set一个值,都会将ThreadLocal和value存放入一个Entry,并按类似的方法计算下标值。

ThreadLocalMap不为空时

private void set(ThreadLocal<?> key, Object value) {

// We don't use a fast path as with get() because it is at
// least as common to use set() to create new entries as
// it is to replace existing ones, in which case, a fast
// path would fail more often than not.

Entry[] tab = table;
int len = tab.length;
int i = key.threadLocalHashCode & (len-1);

for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
ThreadLocal<?> k = e.get();

if (k == key) {
e.value = value;
return;
}

if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}

tab[i] = new Entry(key, value);
int sz = ++size;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}

其中:int i = key.threadLocalHashCode & (len-1):根据哈希码和数组长度求元素存放的数组下标,这里类似前文的int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);当我们用 0x61c88647 作为魔数累加为每个 ThreadLocal 分配各自的 threadLocalHashCode 再与 2 的幂取模,得到的结果分布很均匀。这为了后面的for循环代码(即线性探测)提供了效率。

for (Entry e = tab[i]; e != null; e = tab[i = nextIndex(i, len)]):从i开始往后一直遍历到数组最后一个Entry(线性探索) 。

if (k == key) {
e.value = value;
return;
}
if (k == null) {
replaceStaleEntry(key, value, i);
return;
}

如果检索到的k和key相等,则直接覆盖value;如果k为null,则用新key、value覆盖,同时清理历史k=null的陈旧数据""stale entries""(弱引用) 。

replaceStaleEntry

replaceStaleEntry(key, value, i);是对数据的清理和替换过程。

private void replaceStaleEntry(ThreadLocal<?> key, Object value,
int staleSlot) {
Entry[] tab = table;
int len = tab.length;
Entry e;

// Back up to check for prior stale entry in current run.
// We clean out whole runs at a time to avoid continual
// incremental rehashing due to garbage collector freeing
// up refs in bunches (i.e., whenever the collector runs).
//向前扫描,查找最前一个无效的slot
int slotToExpunge = staleSlot;
for (int i = prevIndex(staleSlot, len);
(e = tab[i]) != null;
i = prevIndex(i, len))
if (e.get() == null)
//可以定位到最前面一个无效的slot
slotToExpunge = i;

// Find either the key or trailing null slot of run, whichever
// occurs first
//从i开始往后一直遍历到数组最后一个Entry(线性探索)
for (int i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal<?> k = e.get();

// If we find key, then we need to swap it
// with the stale entry to maintain hash table order.
// The newly stale slot, or any other stale slot
// encountered above it, can then be sent to expungeStaleEntry
// to remove or rehash all of the other entries in run.
//找到匹配的key以后
if (k == key) {
e.value = value;//更新对应slot的value值
//与无效的sloat进行交换
tab[i] = tab[staleSlot];
tab[staleSlot] = e;

// Start expunge at preceding stale entry if it exists
//如果最早的一个无效的slot和当前的staleSlot相等,则从i作为清理的起点
if (slotToExpunge == staleSlot)
slotToExpunge = i;
//从slotToExpunge开始做一次连续的清理
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
return;
}

// If we didn't find stale entry on backward scan, the
// first stale entry seen while scanning for key is the
// first still present in the run.
//如果当前的slot已经无效,并且向前扫描过程中没有无效slot,则更新slotToExpunge为当前位置
if (k == null && slotToExpunge == staleSlot)
slotToExpunge = i;
}

// If key not found, put new entry in stale slot
//如果key对应的value在entry中不存在,则直接放一个新的entry
tab[staleSlot].value = null;
tab[staleSlot] = new Entry(key, value);

// If there are any other stale entries in run, expunge them
//如果有任何一个无效的slot,则做一次清理
if (slotToExpunge != staleSlot)
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
}
文章已创建 17

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部