ThreadLocal浅析

1.ThreadLocal的大体理解

  ThreadLocal 又名 线程局部变量,是 Java 中一种较为特殊的 线程绑定机制,可以为每一个使用该变量的线程都提供一个变量值的副本,并且每一个线程都可以独立地改变自己的副本,而不会与其它线程的副本发生冲突。通过 ThreadLocal 存取的数据,总是与当前线程相关,也就是说,JVM 为每个运行的线程绑定了私有的本地实例存取空间,从而为多线程环境常出现的并发访问问题提供了一种 隔离机制 。

  ThreadLocal,很多地方叫做线程本地变量,也有些地方叫做线程本地存储,其实意思差不多。可能很多朋友都知道ThreadLocal为变量在每个线程中都创建了一个副本,那么每个线程可以访问自己内部的副本变量。这句话从字面上看起来很容易理解,但是真正理解并不是那么容易。

  先来看个例子

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

当只有一个线程访问时,完全没有问题,但多线程呢?因为创建连接并没有加锁,并且连接共享,所以可能会有创建多个Connection,所以就会出现一种情况,当一个现场在使用连接的时候另一个线程关闭连接

解决这个问题有一个很简单的处理办法,那就是方法内创建连接(private Connection conn=null;),但是,这有一个很严重的问题,频繁的开关连接,导致服务器压力剧增,显然不是一个很好的办法

2.为了解决以上问题,我们引入ThreadLocal这个类

要了解一个类,首先要看它的成员变量,这里就不对其说明了,其次了解其主要方法

public T get() { }  
public void set(T value) { }  
public void remove() { }  
protected T initialValue() { }

先看看get方法

public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null)
                return (T)e.value;
        }
        return setInitialValue();
    }

第一句是取得当前线程,然后通过getMap(t)方法获取到一个map,map的类型为ThreadLocalMap。然后接着下面获取到<key,value>键值对,注意这里获取键值对传进去的是 this,而不是当前线程t。

        如果获取成功,则返回value值。

        如果map为空,则调用setInitialValue方法返回value。

首先看一下getMap方法中做了什么:

   ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

 可能大家没有想到的是,在getMap中,是调用当期线程t,返回当前线程t中的一个成员变量threadLocals。

        那么我们继续取Thread类中取看一下成员变量threadLocals是什么:

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

实际上就是一个ThreadLocalMap,这个类型是ThreadLocal类的一个内部类,我们继续取看ThreadLocalMap的实现

     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作为键值。

        然后再继续看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;
    }

很容易了解,就是如果map不为空,就设置键值对,为空,再创建Map,看一下createMap的实现:

   void createMap(Thread t, T firstValue) {
        t.threadLocals new ThreadLocalMap(this, firstValue);
    }

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

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

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

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

        下面通过一个例子来证明通过ThreadLocal能达到在每个线程中创建变量副本的效果:

package com.cn.test;  
  
public class Test {  
      
    ThreadLocal<Long> longLocal = new ThreadLocal<Long>();  
    ThreadLocal<String> stringLocal = new ThreadLocal<String>();  
  
    public void set() {  
        longLocal.set(Thread.currentThread().getId());  
        stringLocal.set(Thread.currentThread().getName());  
    }  
  
    public long getLong() {  
        return longLocal.get();  
    }  
  
    public String getString() {  
        return stringLocal.get();  
    }  
  
    public static void main(String[] args) throws InterruptedException {  
        final Test test = new Test();  
  
        test.set();  
        System.out.println(test.getLong());  
        System.out.println(test.getString());  
  
        Thread thread1 new Thread() {  
            public void run() {  
                test.set();  
                System.out.println(test.getLong());  
                System.out.println(test.getString());  
            };  
        };  
        thread1.start();  
        thread1.join();  
  
        System.out.println(test.getLong());  
        System.out.println(test.getString());  
    }  
}
运行结果:
1  
main  
11  
Thread-0  
1  
main

  从这段代码的输出结果可以看出,在main线程中和thread1线程中,longLocal保存的副本值和stringLocal保存的副本值都不一样。最后一次在main线程再次打印副本值是为了证明在main线程中和thread1线程中的副本值确实是不同的。

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();  
}

   Session管理:

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

ThreadLocal知识总结

  1)实际的通过ThreadLocal创建的副本是存储在每个线程自己的threadLocals中的;

        2)为何threadLocals的类型ThreadLocalMap的键值为ThreadLocal对象,因为每个线程中可有多个threadLocal变量,就像上面代码中的longLocal和stringLocal;

        3)在进行get之前,必须先set,否则会报空指针异常;

        如果想在get之前不需要调用set就能正常访问的话,必须重写initialValue()方法。

原文地址:https://www.cnblogs.com/xhlwjy/p/11281282.html