Java多线程之ThreadLocal剖析

对于ThreadLocal这个类我相信多数java攻城狮都不陌生,它是java多线程中的一个重要的类,在面试时经常被问到。不知道大家有没有和我一样的感觉,虽然大概知道这个类的含义但对其实现机制以及什么时候该使用这个类似乎总是理解的不太深刻。如果你和我有同样的困惑,就一起来看看这篇文章。

1.ThreadLocal是什么

看看ThreadLocal源码对类的注释怎么说

This class provides thread-local variables.  These variables differ from 
their normal counterparts in that each thread that accesses one (via its 
{@code get} or {@code set} method) has its own, independently initialized 
copy of the variable.  {@code ThreadLocal} instances are typically private 
static fields in classes that wish to associate state with a thread (e.g.,
a user ID or Transaction ID)

这段话主要告诉我们四个信息:

  • ThreadLocal可以为线程提供专属本线程的变量
  • 变量通过ThreadLocal的get、set方法访问
  • 线程间变量相互独立
  • ThreadLocal对象一般定义为一个私有静态变量

OK, 通过这几个点我们知道了ThreadLocal是用来做什么的。ThreadLocal为我们提供了一种将多线程共享变量变为线程独享变量的一种机制,通过ThreadLocal可以将共享变量变为专属本线程的独占变量,线程之间可以自由操作ThreadLocal变量,互不影响。我们知道了ThreadLocal能干什么,我们再来看看它是怎么实现的。

2.ThreadLocal实现原理

ThreadLocal中最常用的API就是set和get了,set用于给ThreadLocal对象设置值,get用于从ThreadLocal对象中取值。

private static ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<Connection>();

    public static void main(String[] args) {
        if (connectionThreadLocal.get() == null){
            connectionThreadLocal.set(ConnectionUtil.getConnection());
        }
        Thread thread = new Thread(new ConnectionTask(connectionThreadLocal));
        thread.start();
        try {
            //等子线程执行结束后,再继续执行主线程
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Connection connection = connectionThreadLocal.get();
        System.out.println(Thread.currentThread().getName() + " : " + connection);
    }

    static class ConnectionTask implements Runnable{

        private ThreadLocal<Connection> threadLocal;

        public ConnectionTask(ThreadLocal<Connection> threadLocal){
            this.threadLocal = threadLocal;
        }
        public void run() {
            Connection connection = threadLocal.get();
            if (connection == null){
                connection = ConnectionUtil.getConnection();
                threadLocal.set(connection);
            }
            System.out.println(Thread.currentThread().getName() +" : " + connection);
        }
    }

上面的代码中在主线程和子线程中分别对同一个ThreadLocal对象执行set操作,并分别执行get方法取出ThreadLocal对象中的Connnection对象。主线程的get操作在子线程全部执行完后才继续执行。大家猜测一下两个线程中执行打印后输出的是否是同一个Connection对象呢?

代码执行结果

可以看到两个线程中输出的是两个connection对象。这是为什么呢?要知道我们在两个线程中操作的可是同一个ThreadLocal对象啊,假如我们将ThreadLocal当做普通的Bean,它在主线程中执行set操作后在子线程中对该对象再执行get应该取到的是在主线程中set的值,可是我们看到的结果并不是如此。我们通过ThreadLocal的get、set源码来看看为什么会是这样的结果。

先来看get方法:

    /**
     * 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方法中首先拿到执行方法的当前线程t,然后以线程t为参数调用了getMap(t)方法返回了一个ThreadLocalMap对象。如果map对象不为null就从map返回result,如果为null就返回setInitialValue()。有一点比较费解的是map.getEntry传入的key是this,也就是当前ThreadLocal对象,我们来逐层分析,一点点揭开ThreadLocal的神秘面纱。关键点在于这个ThreadLocalMap对象,我们来看看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返回的是当前线程中一个threadLocals属性,线程的threadLocals属性是啥呢。

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

这是Thread类中的threadLocals属性,从注释上我们可以看到这个threadLocals用来维护属于这个线程的ThreadLocal对象,而ThreadLocalMap是ThreadLocal的一个内部类。既然ThreadLocalMap是一个Map,那我们去看一下它的key、value分别是什么。

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

这是ThreadLocal的Entry定义,可以看到ThreadLocalMap的key是一个ThreadLocal对象,其value是一个Object。

看到这里我们应该能够知道的是:执行ThreadLocal的get方法会返回属于当前执行线程的变量,该变量保存在线程的ThreadLocalMap属性中。Map以当前ThreadLocal对象为key,value是一个Object对象,这个value就是在set到ThreadLocal中的那个值。

再看一下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);
    }

可以看到ThreadLocal的set方法同样是先获取当前线程,然后拿到当前线程的ThreadLocalMap引用map。如果map不为null,就以当前threadLocal对象为key,T value作为值保存到线程的threadLocals属性中。否则执行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);
    }

createMap方法很简单,就是初始化线程的threadLocals属性,并将第一个通过ThreadLocal set的值保存到threadLocals中。

看到这里ThreadLocal为线程提供线程本地变量的机制也就很清楚了

  • 通过ThreadLocal的set方法设置的value实际上被保存到了当前线程的ThreadLocalMap对象中,Map以ThreadLocal对象为key,保存的值为value
  • 在调用ThreadLocal的get时,实际上是以当前的ThreadLocal对象为key从当前线程的ThreadLocalMap属性中取出对应的value

这也就解释了在本节开头中那个demo的输出:同一个ThreadLocal对象,分别在两个线程中执行set方法后,再调用get方法输出的是两个不同的对象。因为主线程和子线程中的分别拥有各自的ThreadLocalMap属性来保存<ThreadLocal this,T value>的键值对,因此两个线程的中value是互不相干的。

3.什么时候使用ThreadLocal

当访问共享变量时为了确保并发安全性,通常需要线程间同步。我们知道线程间同步是非常麻烦且耗费性能的。一种避免线程间同步的就是不进行线程间数据共享,仅在线程内访问数据就无需进行线程同步了,这种技术被称为线程封闭。而ThreadLocal就是一种非常好的实现线程封闭的技术。

当我们不希望线程间共享变量时,或者说线程完全没有必要进行变量共享时就可以使用ThreadLocal对象将变量设置为线程独享的变量。

例如,在单线程应用程序中可能会维持一个全局的数据库连接,并在程序启动的时候初始化这个连接对象,从而避免在调用每个方法时都初始化都要传递一个Connection对象。但是在多线程中Connection对象是共享的,没有线程同步的情况下这是不安全的。可能一个线程正在使用共享的Connection对象,而另一个线程就将Connection对象close了。利用ThreadLocal则可以为每个线程都初始化一个Connection对象,每个线程独立操作属于本线程的Connection,互不影响。

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

    public static Connection getConnection(){
        return connectionHolder.get();
    }

最后,需要注意的是在使用ThreadLocal时,一般将ThreadLocal设置私有静态变量,get前先set或者重写initialValue方法。如果有多个对象需要set到ThreadLocal中,就初始化多个ThreadLocal变量。

好了,ThreadLocal的介绍就到这里。

你的关注是我持续更新的动力!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。