ThreadLocal 源码分析

引言

在 jdk/jre/lib/rt.jar 字节码包中,可以看到关于ThreadLocal类及其内部类编译后的文件;

通过本文的对相关类的源码分析,旨在理清Thread、ThreadLocalMap、Entry、ThreadLocal之间的关系


ThreadLocalMap类分析

ThreadLocalMap是ThreadLocal中的内部类,它是实现ThreadLocal类功能的基础,所以先来看看这个字面上是个Map的类,有哪些方法,实现了哪些功能;

Entry实例对象

Entry是ThreadLocalMap的内部类;Entry才是体现Map含义的根本所在,ThreadLocalMap只是维护了Entry数组的增删改查和扩容等

// Entry沿继承链可以追溯到Reference,
// 该类用于保持对某个堆中实例对象不同强度的引用,用于的控制GC对被引用对象的回收时机
 * @author   Mark Reinhold
 * @since    1.2
 */
public abstract class Reference<T> {
    private T referent;         /* Treated specially by GC */
    public T get() {
       return this.referent;
    }
}


static class Entry extends WeakReference<ThreadLocal<?>> {
     /** The value associated with this ThreadLocal. */
     Object value;

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

// 即将Entry理解为
class Entry {
    // 成员变量
    private ThreadLocal<T> threadLocalReferent;
    T value;
    
    // 访问成员变量的方式,value可直接通过点运算符访问
    public ThreadLocal<T> get() {
       return this.referent;
    }
 
}

Entry是ThreadLocalMap中的内部类,继承自弱引用类WeakReference,从Entry的构造函数可以看出,Entry的实例对象用于将一个ThreadLocal对象的引用和一个Object的值关联起来;

即entry保存一个Entry entry = ( ThreadLocal<T> key, <T> value ) 键值对:

  • entry.get()获取key,用于获取该entry实例对一个ThreadLocal实例对象的referent引用;
  • entry.value获取value,用于获取该entry实例存放的设定值value,即相当于(value)

ThreadLocalMap成员方法分析

ThreadLocalMap只是维护了Entry数组的增删改查和扩容等

Entry[ ]数组:ThreadLocalMap中维护了一个长度可变的Entry数组:

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

set方法:调用ThreadLocalMap的set方法,相当于传入构建一个新entry所需要的(key ,value)键值对,具体过程如下:

  • 先通过for循环,轮询检测传入的threadLocal对象是否已存在Entry数组中,如果存在并且要设定的value值也和之前相等,则不做任何处理;
  • 若轮询检测到数组中存在该ThreadLocal对象key,但value不一样,则覆盖设置value;
  • 如果轮询检测当前table找不到的key(从源码中可以看出,key只是个ThreadLocal实例对象,要想转换成table数组的整型索引,需要结合对象hashCode等一些算法步骤进行索引映射),则根据传入的<key ,value>构建新entry,并添加至Entry数组中;
  • 当然,具体的数组扩容等细节,就不细谈了;
        /**
         * Set the value associated with key.
         *
         * @param key the thread local object
         * @param value the value to be set
         */
        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();
        }

getEntry方法:从Entry[ ]中找出指定threadLocal对象对应的值

/**
* Get the entry associated with key.  This method
* itself handles only the fast path: a direct hit of existing
* key. It otherwise relays to getEntryAfterMiss.  This is
* designed to maximize performance for direct hits, in part
* by making this method readily inlinable.
*
* @param  key the thread local object
* @return the entry associated with key, or null if no such
*/
private Entry getEntry(ThreadLocal<?> key) {
    int i = key.threadLocalHashCode & (table.length - 1);
    Entry e = table[i];
    if (e != null && e.get() == key)
         return e;
    else
         return getEntryAfterMiss(key, i, e);
}

remove方法:从Entry[ ]中删除指定threadLocal对象对应的数组元素entry,具体步骤:

  • 通过key映射成数组索引i,并进行for数组轮询;
  • 如果找到entry,则先调用Reference中的clear方法,剪断该entry对threadLocal堆实例的引用关系,方便GC回收;
  • 接着,expungeStaleEntry(i)方法能对Entry[ ]数组进行清除和整理,比较复杂,暂且不细谈;
/**
 * Remove the entry for key.
 */
private void remove(ThreadLocal<?> key) {
     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)]) {
         if (e.get() == key) {
              e.clear();
              expungeStaleEntry(i);
              return;
         }
     }
}

// Reference类中定义的clear方法
/**
 * Clears this reference object.  Invoking this method will not cause this
 * object to be enqueued.
 *
 * <p> This method is invoked only by Java code; when the garbage collector
 * clears references it does so directly, without invoking this method.
 */
public void clear() {
    this.referent = null;
}

ThreadLocalMap的类源码基本为ThreadLocal贡献了大部分的功能,接着在理解ThreadLocalMap作用的基础上,看看ThrealLocal有哪些方法,并且如何实现线程封闭的?

ThreadLocal成员方法分析

Thread类成员变量threadLocals:

在Thread类中,维护了一个ThreadLocal.ThreadLocalMap类实例对象 threadLocals,

public class Thread implements Runnable {


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


} 

set方法:理解set方法最核心的是:

  • 每个Thread类实例中都有一个ThreadLocalMap的实例对象,即ThreadLocalMap虽然定义在ThreadLocal中,但是是由当前线程进行引用;
  • ThreadLocal的set方法实则是对当前线程的threadLocals进行增添键值对操作。即新增Entry为 ( ThreadLock<T>  this,   <T> value )

    public void set(T value) {
        // 获取当前线程
        Thread t = Thread.currentThread();
        // 以局部变量的方式,获取当前线程的threadLocals成员变量
        ThreadLocalMap map = getMap(t);
        if (map != null)
            // 如果当前线程的ThreadLocalMap已经被创建过,
            // 则调用ThreadLocalMap的set方法创建新Entry,并增加至Entry[]
            map.set(this, value);
        else
            // 如果当前线程的ThreadLocalMap还未被创建过,则为当前线程初始化threadLocals
            // 并继续在方法内部调用map.set方法
            createMap(t, value);
    }
    
    /* 传入当前线程引用,获取当前线程的threadLocals
     * @param  t the current thread
     * @return the map
     */
    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }


    /* 传入当前线程,以设置其threadLocals
     * @param t the current thread
     * @param firstValue value for the initial entry of the map
     */
    void createMap(Thread t, T firstValue) {
        // 传入当前ThreadLocal实例引用this,和value值,
        // 在构建ThreadLocalMap的同时,初始化Entry数组,并返回新构建的实例引用
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

get方法:理解get方法最核心的是:

  • 内部获取的是当前线程维护的ThreadLocalMap threadLocals;
  • 内部以当前ThreadLocal实例对象自身(this)的引用作为key值,来获取对应的value;
    /*
     * @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方法中getMap得到的ThreadLocalMap为null时,先创建map并新建Entry
     * 当ThreadLocalMap已存在,但找不到key时,会调用用户模板initialValue为这个key匹配一个默认的vaule,并作为新的entry添加至Entry[]中
     * @return the initial value
     */
    private T setInitialValue() {
        T value = initialValue();
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
        return value;
    }

    
    /* initialValue是留给用户创建ThreadLocal时的模板方法
     * 当get
     * @return the initial value for this thread-local
     */
    protected T initialValue() {
        return null;
    }

总结:Thread、ThreadLocalMap、Entry 、ThreadLocal四者之间的关系

总结:

  • 每个线程都维护自己的独有的ThreadLocalMap;
  • 每个TheadLocalMap维护一个Entry数组,数组中存放很多键值对;
  • 每个Entry键值对,以ThreadLocal对象的引用作为键,以泛型的value作为值;
  • ThreadLocal作为Entry键值对的键,是通过在threadLocal实例调用set方法将自身引用this作为参数来实现的;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值