ThreadLocal源码解析(一)

来源:互联网 发布:淘宝店铺首页全屏轮播 编辑:程序博客网 时间:2024/06/12 01:11

1.ThreadLocal中涉及到的几个重要类
• Thread:表示一个线程
• ThreadLocal:本文要解析的这个类
• ThreadLocalMap:ThreadLocal类中的一个内部类,这个类相当于一个HashMap类,但是跟HashMap没有任何关系,内部实现跟HashMap类似
• ThreadLocalMap.Entry:把它看成是保存键值对的对象,其本质上是一个WeakReference对象。

2.ThreadLocal数据存取
1.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         ThreadLocalMap map = getMap(t);        if (map != null)            //map不为空,则设置            map.set(this, value);        else//否则创建并设置            createMap(t, value);    }

getMap(t)方法:

/**     * Get the map associated with a ThreadLocal. Overridden in     * InheritableThreadLocal.     *     * @param  t the current thread     * @return the map     */    ThreadLocalMap getMap(Thread t) {        return t.threadLocals;    }

有一种不可思议的感觉,这个ThreadLocalMap怎么是从Thread对象中
获取的呢?我们来看看ThreadLocalMap在Thread对象中是怎么存在的:

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

在Thread对象中ThreadLocalMap对象就是一个属性存在,所以说每一个线程引用的ThreadLocal副本值都保存在当前线程中,存储结构为ThreadLocalMap类型,ThreadLocalMap的键为ThreadLocal,值为副本。
一个ThreadLocal对象只能存储一个数据,如果存储多个数据,new多个ThreadLocal对象,虽然对象很多,但是都以键值对的形式保存在Thread的ThreadLocalMap属性中。
总结下流程:
先拿到保存键值对的ThreadLocalMap对象实例map,如果map为空(第一次调用的时候map值为null),则去创建一个ThreadLocalMap对象并赋值给map,并把键值对保存到map中。

2.get()方法

public T get() {        //获取当前线程        Thread t = Thread.currentThread();        //根据当前线程获取ThreadLocalMap对象        ThreadLocalMap map = getMap(t);        //不为空,直接获取值        if (map != null) {            ThreadLocalMap.Entry e = map.getEntry(this);            if (e != null)                return (T)e.value;        }       // 为空,则调用setInitialValue()        return setInitialValue();    }

拿到当前线程Thread对象实例中保存的ThreadLocalMap对象map,然后从map中读取键为this(即ThreadLocal类实例)对应的值。
如果map不是null,直接从map里面读取就好,如果map==null,那么我们需要对当前线程Thread对象实例中保存的ThreadLocalMap对象new一下。即通过setInitialValue()

private T setInitialValue() {        //通过initialValue()获取value值,实质initialValue()设置的值为null。        T value = initialValue();        //获取当前线程        Thread t = Thread.currentThread();        //根据当前线程获取ThreadLocalMap对象        ThreadLocalMap map = getMap(t);        //map为空,设置值        if (map != null)            map.set(this, value);        else            //map为空,创建并设置            createMap(t, value);        //返回value值,value值为null        return value;    }

代码很简单,通过createMap来创建ThreadLocalMap对象,前面set函数里面创建ThreadLocalMap也是通过createMap来的,我们看看createMap具体实现:
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
3.类之间的关系


从这个图片可以看到,数据存在Thread类中,通过绿线获取值,通过橘线设置值,而操作这个类都是通过threadLocal对象,可见threadLocal相当于一个工具类。用来操作Thread类中的ThreadLocalMap。

后续文章将讲述ThreadLocal的内部类ThreadLocalMap是怎么实现HashMap类似的功能的,已经threadLocal存在的局限性,请关注。

0 0
原创粉丝点击