ThreadLocal类详解:原理、源码、用法

 

以下是本文目录:

  1、从数据库连接探究 ThreadLocal

  2、剖析 ThreadLocal 源码

  3、 ThreadLocal 应用场景

  4、 通过面试题理解 ThreadLocal


 

1、从数据库连接探究 ThreadLocal

先看一个数据库连接的例子:

 

class ConnectionManager{
    private static Connection conn = null;
    
    public static Connection openConnection(){
        if(conn == null){
            conn = DriverManager.getConnection();
        }
        return conn;
    }
    
    public static void closeConnection(){
        if(conn != null) conn.close();
    }
}

 

在多线程环境下,上述代码有如下两个线程安全问题:1. 两个方法没有同步,可能会多次创建 conn。2. conn 是共享变量,可能一个线程用 conn 进行数据库操作的同时,另一个线程,关闭 conn。

关于解决上述问题,首先想到的可能是进行同步处理:对两个方法进行同步,并且在调用 conn 的地方进行同步。这样会大大影响程序执行效率,因为一个线程在使用connect进行数据库操作的时候,其他线程只有等待。

那么大家来仔细分析一下这个问题,这地方到底需不需要将connect变量进行共享?事实上,是不需要的。假如每个线程中都有一个connect变量,各个线程之间对connect变量的访问实际上是没有依赖关系的,即一个线程不需要关心其他线程是否对这个connect进行了修改的。

到这里,可能会有朋友想到,既然不需要在线程之间共享这个变量,可以直接这样处理,在每个需要使用数据库连接的方法中具体使用时才创建数据库链接,然后在方法调用完毕再释放这个连接。比如下面这样:

class ConnectionManager {
     
    private  Connection connect = null;
     
    public Connection openConnection() {
        if(connect == null){
            connect = DriverManager.getConnection();
        }
        return connect;
    }
     
    public void closeConnection() {
        if(connect!=null)
            connect.close();
    }
}
 
 
class Dao{
    public void insert() {
        ConnectionManager connectionManager = new ConnectionManager();
        Connection connection = connectionManager.openConnection();
         
        //使用connection进行操作
         
        connectionManager.closeConnection();
    }
}

这样处理确实也没有任何问题,由于每次都是在方法内部创建的连接,那么线程之间自然不存在线程安全问题,这里使用了栈封闭。但是这样会有一个致命的影响:导致服务器压力非常大,并且严重影响程序执行性能。由于在方法中需要频繁地开启和关闭数据库连接,这样不仅严重影响程序执行效率,还可能导致服务器压力巨大。

那么这种情况下使用ThreadLocal是再适合不过的了,因为ThreadLocal在每个线程中对该变量会创建一个副本,即每个线程内部都会有一个该变量,且在线程内部任何地方都可以使用,线程之间互不影响,这样一来就不存在线程安全问题,也不会严重影响程序执行性能。

但是要注意,虽然ThreadLocal能够解决上面说的问题,但是由于在每个线程中都创建了副本,所以要考虑它对资源的消耗,比如内存的占用会比不使用ThreadLocal要大。

 


 

2、剖析 ThreadLocal 源码

剖析源码前,先关注几个方法:

public T get(){}    //获取当前线程中保存的变量副本
public void set(T value){}    // 设置变量副本
protected T initialValue(){}    // 在 ThreadLocal 中返回值为空,此处用来被重写
public void remove(){}    // 移除当前线程中的变量副本

在不重写 initialVale() 方法的情况下,进行 get() 之前,必须先 set(),否则返回空值。这里先看一下 set() 方法的实现:

 1  /**
 2      * Sets the current thread's copy of this thread-local variable
 3      * to the specified value.  Most subclasses will have no need to
 4      * override this method, relying solely on the {@link #initialValue}
 5      * method to set the values of thread-locals.
 6      *
 7      * @param value the value to be stored in the current thread's copy of
 8      *        this thread-local.
 9      */
10     public void set(T value) {
11         Thread t = Thread.currentThread();
12         ThreadLocalMap map = getMap(t);
13         if (map != null)
14             map.set(this, value);
15         else
16             createMap(t, value);
17     }

第一句是取得当前线程的对象,然后通过 getMap(t) 方法获取到一个 map,该 map 为 ThreadLocalMap(ThreadLocal内部类) 类型。然后判断 map 是否为空,如果不为空则将 value值 set 进 map , 注意这里传进去的是 this 而不是当前线程对象 t;如果 map 为空,则创建一个 map, 注意这里用的是 t .下面仔细分析

从第11行,首先调用  Thread.currentThread() 获取当前正在执行线程对象的引用。下面是 Thread.currentThread() 的实现:

/**
     * Returns a reference to the currently executing thread object.
     *
     * @return  the currently executing thread.
     */
    public static native Thread currentThread();

这里 native 代表本地代码,用其它语言写的编译成对应平台机器码的代码。该调用返回一个当前执行线程对象的引用。"t" 是当前执行线程的对象。

然后通过 getMap(t) 获取 map,让我们看一下 getMap() 中做了什么:

 /**
     * 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;
    }

在 getMap() 方法中返回当前线程 t 中的一个成员变量,"threadLocals"。那么我们继续取Thread类中取看一下成员变量threadLocals是什么:

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

实际上就是一个ThreadLocalMap,这个类型是ThreadLocal类的一个内部类。

当 map 不为空时就将 当前线程对象的引用,value值 作为键值对set进map,保证了每一个线程对象对应一个 value。

当 map 为空时调用 createMap(), 让我们看一下它是如何实现的:

  /**
     * 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);
    }

将 当前线程t和传入的值firstValue 作为参数创建 ThreadLocalMap 的对象,赋值给 threadLocals 变量。

至此,可能大部分朋友已经明白了ThreadLocal是如何为每个线程创建变量的副本的:

首先,在每个线程Thread内部有一个ThreadLocal.ThreadLocalMap类型的成员变量threadLocals,这个threadLocals就是用来存储实际的变量副本的,键值为当前ThreadLocal变量,value为变量副本(即T类型的变量)。

初始时,在Thread里面,threadLocals为空,当通过ThreadLocal变量调用set()方法,就会对Thread类中的threadLocals进行初始化,并且以当前ThreadLocal变量为键值,以ThreadLocal要保存的副本变量为value,存到threadLocals。

然后在当前线程里面,如果要使用副本变量,就可以通过get()方法在threadLocals里面查找。

下面介绍 get() 方法:

 1     /**
 2      * Returns the value in the current thread's copy of this
 3      * thread-local variable.  If the variable has no value for the
 4      * current thread, it is first initialized to the value returned
 5      * by an invocation of the {@link #initialValue} method.
 6      *
 7      * @return the current thread's value of this thread-local
 8      */
 9     public T get() {
10         Thread t = Thread.currentThread();
11         ThreadLocalMap map = getMap(t);
12         if (map != null) {
13             ThreadLocalMap.Entry e = map.getEntry(this);
14             if (e != null) {
15                 @SuppressWarnings("unchecked")
16                 T result = (T)e.value;
17                 return result;
18             }
19         }
20         return setInitialValue();
21     }

前两行和set()方法一样,获取到一个 map 对象, 判断该对象是否为空。当该对象为空时调用 setInitialValue() 方法,该方法返回一个 value 值。下面是 setInitialValue() 方法的具体实现:

 /**
     * Variant of set() to establish initialValue. Used instead
     * of set() in case user has overridden the set() method.
     *
     * @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;
    }

考虑到set() 方法由可能被重写,这里用了 set() 方法的变体。方法中首先调用 initialValue() 方法初始化一个 value。下面看一下 initialValue() 方法的实现:

 /**
     * Returns the current thread's "initial value" for this
     * thread-local variable.  This method will be invoked the first
     * time a thread accesses the variable with the {@link #get}
     * method, unless the thread previously invoked the {@link #set}
     * method, in which case the {@code initialValue} method will not
     * be invoked for the thread.  Normally, this method is invoked at
     * most once per thread, but it may be invoked again in case of
     * subsequent invocations of {@link #remove} followed by {@link #get}.
     *
     * <p>This implementation simply returns {@code null}; if the
     * programmer desires thread-local variables to have an initial
     * value other than {@code null}, {@code ThreadLocal} must be
     * subclassed, and this method overridden.  Typically, an
     * anonymous inner class will be used.
     *
     * @return the initial value for this thread-local
     */
    protected T initialValue() {
        return null;
    }

这里 initialValue() 方法为 protected 权限,当子类继承该类时,可以重写该方法。在该类中,该方法返回值为 null。回到  setInitialValue() 方法.在setInitialValue() 方法中,接下来仍是获取一个 map 并判断是否为空,传入的 value 值均为 initialVale() 方法中获取的值,然后 返回 value。

当 get() 方法中的 map 值不为空时返回一个 Entry 下面是 Entry 的具体实现:

  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.
         */
        static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;

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

可以看到ThreadLocalMap的Entry继承了WeakReference,并且使用ThreadLocal作为键值。

下面是 remove() 方法的源码:

  /**
     * 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);
     }

可以看到就是在获取当前线程的对象作为键值,然后 remove。

 


 

3.ThreadLocal的应用场景

最常见的ThreadLocal使用场景为 用来解决 数据库连接、Session管理等。

如:

private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>() {
public Connection initialValue() {
    return DriverManager.getConnection(DB_URL);
}
};
 
public static Connection getConnection() {
return connectionHolder.get();
}

private static final ThreadLocal threadSession = new ThreadLocal();
 
public static Session getSession() throws InfrastructureException {
    Session s = (Session) threadSession.get();
    try {
        if (s == null) {
            s = getSessionFactory().openSession();
            threadSession.set(s);
        }
    } catch (HibernateException ex) {
        throw new InfrastructureException(ex);
    }
    return s;
}

 


 

4. 相关面试题

1、每个线程的变量副本是存储在哪里的?

最关键的问题是:ThreadLocal是怎么实现了多个线程之间每个线程一个变量副本的?它是如何实现共享变量的。

ThreadLocal提供了set和get访问器用来访问与当前线程相关联的线程局部变量。

可以从ThreadLocal的get函数中看出来,其中getmap函数是用t作为参数,这里t就是当前执行的线程。

从而得知,get函数就是从当前线程的threadlocalmap中取出当前线程对应的变量的副本【注意,变量是保存在线程中的,而不是保存在ThreadLocal变量中】。当前线程中,有一个变量引用名字是threadLocals,这个引用是在ThreadLocal类中createmap函数内初始化的。每个线程都有一个这样的threadLocals引用的ThreadLocalMap,以ThreadLocal和ThreadLocal对象声明的变量类型作为参数。这样,我们所使用的ThreadLocal变量的实际数据,通过get函数取值的时候,就是通过取出Thread中threadLocals引用的map,然后从这个map中根据当前threadLocal作为参数,取出数据。现在,变量的副本从哪里取出来的(本文章提出的第一个问题)已经确认解决了。

【ThreadLocal整体上给我的感觉就是,一个包装类。声明了这个类的对象之后,每个线程的数据其实还是在自己线程内部通过threadLocals引用到的自己的数据。只是通过ThreadLocal访问这个数据而已】

2、变量副本是怎么从共享的那个变量赋值出来的?源码中的threadlocal的初始值是什么时机设置的?

这里“复制”两个字用的很不专业。准确的说,应该是,变量副本【每个线程中保存的那个map中的变量】是怎么声明和初始化的?

看下面set函数的源码:


当线程中的threadlocalmap是null的时候,会调用createmap创建一个map。同时根据函数参数设置上初始值。也就是说,当前线程的threadlocalmap是在第一次调用set的时候创建map并且设置上相应的值的。

对于这篇文章中的例子,每个线程打印的东西都是相互独立的,是因为SequenceNumber的getNextNum()函数中先set了一个值,再get。写到这里,终于清楚了ThreadLocal的运作方法了。

解释如下:

1)在代码中声明的ThreadLocal对象,实际上只有一个。

2)在每个线程中,都维护了一个threadlocals对象,在没有ThreadLocal变量的时候是null的。一旦在ThreadLocal的createMap函数中初始化之后,这个threadlocals就初始化了。以后每次那个ThreadLocal对象想要访问变量的时候,比如set函数和get函数,都是先通过getMap(t)函数,先将线程的map取出,然后再从这个在线程(Thread)中维护的map中取出数据【以当前threadlocal作为参数】。

到此,第二个问题也解决了。

3、不同的线程局部变量,比如说声明了n个(n>=2)这样的线程局部变量threadlocal,那么在Thread中的threadlocals中是怎么存储的呢?threadlocalmap中是怎么操作的?

在ThreadLocal的set函数中,可以看到,其中的map.set(this, value);把当前的threadlocal传入到map中作为键,也就是说,在不同的线程的threadlocals变量中,都会有一个以你所声明的那个线程局部变量threadlocal作为键的key-value。假设说声明了N个这样的线程局部变量变量,那么在线程的ThreadLocalMap中就会有n个分别以你的线程局部变量作为key的键值对。

 

参考:深入剖析ThreadLocal  ThreadLocal工作原理

posted @ 2016-03-20 11:24  木易·月  阅读(339)  评论(0编辑  收藏  举报