最近做了一个日志调用链路跟踪的项目,涉及到操作标识在线程和子线程,线程池以及远程调用之间的传递问题。最终采用了开源的TransmittableThreadLocal
插件(https://github.com/alibaba/transmittable-thread-local)完美解决。在分析源码以及中途修复bug的过程中,被ThreadLocal
搞得晕头转向。好在静下心来细细啃了一下午,终于能理解各种ThreadLocal
相关问题了。这里准备用博客记录下来。
要想了解ThreadLocal
的底层原理首先就要了解弱引用。本篇不会详细介绍是强引用,啥是弱引用、软引用以及虚幻引用,有兴趣的同学可以自己百度。这里直接给出弱引用的简单代码说明:
Object obj = new Object();
WeakReference<Object> wf = new WeakReference<Object>(obj); // 对堆内存中对象建立一个弱引用
obj = null; // 去掉堆中对象的强引用
System.gc();
System.out.println(wf.get()); // 输出null
可以看到弱引用的作用就在于当堆内存中对象不存在强引用的时候,在下一次gc的时候可能会回收掉堆内存占用。
了解了弱引用之后,其实就能够很好地理解ThreadLocal
(题外话:其实这个ThreadLocal我摸索了好久才弄得比较透彻
)。直接上代码:
首先我们需要注意Thread
类中的两个属性:
public class Thread implements Runnable {
// ....
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
// ThreadLocal实际值的存储所在
ThreadLocal.ThreadLocalMap threadLocals = null;
/*
* InheritableThreadLocal values pertaining to this thread. This map is
* maintained by the InheritableThreadLocal class.
*/
// 后面要将的InheritableThreadLocal的实际值存储所在
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
}
这两个属性特别关键:
他们是ThreadLocal
的神奇魔法之关键~
接下来我们来看看ThreadLocal
的关键方法:
/**
* 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);
}
/**
* 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) {
// set时候的关键,实际上是创建一个当前ThreadLocal的弱引用为key的Map
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
/**
* Returns the value in the current thread's copy of this
* thread-local variable. If the variable has no value for the
* current thread, it is first initialized to the value returned
* by an invocation of the {@link #initialValue} method.
*
* @return the current thread's value of this thread-local
*/
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
/**
* Get the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
* 最为关键的方法: 可以看出getMap实际上就是得到传入线程的threadLocals属性的值
* @param t the current thread
* @return the map
*/
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
/**
* Removes the current thread's value for this thread-local
* variable. If this thread-local variable is subsequently
* {@linkplain #get read} by the current thread, its value will be
* reinitialized by invoking its {@link #initialValue} method,
* unless its value is {@linkplain #set set} by the current thread
* in the interim. This may result in multiple invocations of the
* {@code initialValue} method in the current thread.
*
* @since 1.5
*/
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}
不难看出,所有方法都是围绕着一个ThreadLocalMap
来操作的,那么这个ThreadLocalMap
究竟是啥,我们进一步来分析:
/**
* ThreadLocalMap is a customized hash map suitable only for
* maintaining thread local values. No operations are exported
* outside of the ThreadLocal class. The class is package private to
* allow declaration of fields in class Thread. To help deal with
* very large and long-lived usages, the hash table entries use
* WeakReferences for keys. However, since reference queues are not
* used, stale entries are guaranteed to be removed only when
* the table starts running out of space.
*/
static class 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.
*/
// 实际上他的存储也是利用Entry结构来进行的,只不过这个Entry的key值是弱音用对象,实际上可以将ThreadLocalMap看做WeakHashMap
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
}
至此,我们已经知道了ThreadLocal
是如何实现的了,具体来说是下面几个关键点: