【并发编程】ThreadLocal 的原理分析和最佳实践

1. 引言

之前在学习ThreadLocal的时候,了解到其作用是数据隔离,并且是每一个线程复制了一份,每个线程的访问的数据都是不受其他线程影响的。其实,这句话前半句是对的,ThreadLocal的确是数据的隔离,但是并非数据的复制,而是在每一个线程中创建一个新的数据对象(这点类似于new创建对象),然后每一个线程使用的是不一样的。

2. 示例

首先想通过个小问题来说明ThreadLocal的用法。假设有这么一个需求:我想要启动两个线程,分别输出1~6的整数,要求两个线程输出的数据又不能相互干扰。
使用ThreadLocal实现。
程序清单1:实现两个线程分别输出整数

/**
 * @author Carson Chu
 * @date 2020/1/28 18:00
 * @description
 */
public class PrintDigit {

    /* 通过匿名内部类重写ThreadLocal的initialValue()方法,指定初始值0*/
    private static ThreadLocal<Integer> integerThreadLocal = new ThreadLocal<Integer>() {
        public Integer initialValue() {
            return 0;
        }
    };

    /* 自增1*/
    public int getNextNum() {
        integerThreadLocal.set(integerThreadLocal.get() + 1);
        //System.out.println();
        return integerThreadLocal.get();
    }

    public ThreadLocal<Integer> getThreadLocal() {
        return integerThreadLocal;
    }

    public static void main(String[] args) {
        PrintDigit printDigit = new PrintDigit();
        // 两个线程共享printDigit,各自输出
        TestClient t1 = new TestClient(printDigit);
        TestClient t2 = new TestClient(printDigit);
        t1.start();
        t2.start();
    }

    private static class TestClient extends Thread {
        private PrintDigit printDigit;

        public TestClient(PrintDigit printDigit) {
            this.printDigit = printDigit;
        }

        public void run() {
            for (int i = 0; i < 6; i++) {
                // 每个线程打出6个数字
                System.out.println(Thread.currentThread().getName() + " --> printDigit["
                        + printDigit.getNextNum() + "]");
            }
        }
    }
}

输出结果如下图,可以看出,两个线程是虽然交替执行,但是输出的时候各行其道,互不影响。
Carson
到这里,你应该对ThreadLocal的大致用法有个了解了,你应该很好奇它的底层实现。那么究竟这是怎么实现的呢?

3. 原理

ThreadLocal,连接ThreadLocalMap类和Thread类。来处理Thread的TheadLocalMap属性,包括初始化属性赋值、get对应的变量,set设置变量等。通过当前线程,获取线程上的ThreadLocalMap属性,对数据进行get、set等操作。
  ThreadLocal,有个ThreadLocalMap类型的属性,主要是用来存储数据,采用类似HashMap机制,存储了以threadLocal为key,需要隔离的数据为value的Entry键值对数组结构。
3.1 ThreadLocal、ThreadLocal、Thread之间的关系
  ThreadLocalMap是ThreadLocal内部类,由ThreadLocal创建,Thread有ThreadLocal.ThreadLocalMap类型的属性。源码如下:
3.2 Thread类:
程序清单2:Thread类源码

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

	/* 具体实现省略 */
}

3.3 ThreadLocal类:
程序清单3:ThreadLocal类源码

public class ThreadLocal<T> {
    /**
     * ThreadLocals rely on per-thread linear-probe hash maps attached
     * to each thread (Thread.threadLocals and
     * inheritableThreadLocals).  The ThreadLocal objects act as keys,
     * searched via threadLocalHashCode.  This is a custom hash code
     * (useful only within ThreadLocalMaps) that eliminates collisions
     * in the common case where consecutively constructed ThreadLocals
     * are used by the same threads, while remaining well-behaved in
     * less common cases.
     */
    private final int threadLocalHashCode = nextHashCode();

    /**
     * The next hash code to be given out. Updated atomically. Starts at
     * zero.
     */
    private static AtomicInteger nextHashCode =
        new AtomicInteger();

    /**
     * The difference between successively generated hash codes - turns
     * implicit sequential thread-local IDs into near-optimally spread
     * multiplicative hash values for power-of-two-sized tables.
     */
    private static final int HASH_INCREMENT = 0x61c88647;

    /**
     * Returns the next hash code.
     */
    private static int nextHashCode() {
        return nextHashCode.getAndAdd(HASH_INCREMENT);
    }

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

    /**
     * Creates a thread local variable. The initial value of the variable is
     * determined by invoking the {@code get} method on the {@code Supplier}.
     *
     * @param <S> the type of the thread local's value
     * @param supplier the supplier to be used to determine the initial value
     * @return a new thread local variable
     * @throws NullPointerException if the specified supplier is null
     * @since 1.8
     */
    public static <S> ThreadLocal<S> withInitial(Supplier<? extends S> supplier) {
        return new SuppliedThreadLocal<>(supplier);
    }

    /**
     * Creates a thread local variable.
     * @see #withInitial(java.util.function.Supplier)
     */
    public ThreadLocal() {
    }

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

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

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

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

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

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

3.4 ThreadLocal类核心方法:
ThreadLocal类核心方法createMap、set、get、initialValue、withInitial、setInitialValue、remove:

  • createMap方法表示由ThreadLocal对Thread的TreadLocalMap进行赋值
  • initialValue返回该线程局部变量的初始值。该方法是一个protected的方法,显然是为了让子类覆盖而设计的。这个方法是一个延迟调用方法,在线程第1次调用get()或set(Object)时才执行,并且仅执行1次。ThreadLocal中的缺省实现直接返回一个null。
  • withInitial提供一个Supplier的lamda表达式用来当做初始值,java8引入。
  • setInitialValue设置初始值。在get操作没有对应的值时,调用此方法。private方法,防止被覆盖。过程和set类似,只不过是用initialValue作为value进行设置。
  • set设置当前线程对应的线程局部变量的值。先取出当前线程对应的threadLocalMap,如果不存在则用创建一个,否则将value放入以this,即threadLocal为key的映射的map中,其实threadLocalMap内部和hashMap机制一样,存储了Entry键值对数组,后续会深挖threadLocalMap。
  • get该方法返回当前线程所对应的线程局部变量。和set类似,也是先取出当前线程对应的threadLocalMap,如果不存在则用创建一个,但是是用inittialValue作为value放入到map中,且返回initialValue,否则就直接从map取出this即threadLocal对应的value返回。
  • remove将当前线程局部变量的值删除,目的是为了减少内存的占用,该方法是JDK5.0新增的方法。需要指出的是,当线程结束后,对应该线程的局部变量将自动被垃圾回收,所以显式调用该方法清除线程的局部变量并不是必须的操作,但它可以加快内存回收的速度。需要注意的是,如果remove之后又调用了get,会重新初始化一次,即再次调用initialValue方法,除非在get之前调用set设置过值。

4. 应用

4.1 数据库连接

先看下线程不安全的实现,
程序清单4:线程不安全的数据库连接

/**
 * @author Carson Chu
 * @date 2020/1/28 20:16
 * @description
 */
@NotThreadSafe
public class DBConnectionDemo {
    /* 一个非线程安全的变量*/
    private Connection connection;

    public void getDBConnection() throws SQLException {
        /* 引用非线程安全变量 */
        Statement statement = connection.createStatement();
        /* 具体实现省略 */
    }
}

程序清单5:线程安全的数据库连接

/**
 * @author Carson Chu
 * @date 2020/1/28 20:24
 * @description
 */
@ThreadSafe
public class DBConnectionDemo1 {
    // 使用ThreadLocal保存Connection变量
    private static ThreadLocal<Connection> connectionThreadLocal = ThreadLocal.withInitial(DBConnectionDemo1::createConnection);

    // 初始化数据库连接的方法
    private static Connection createConnection() {
        Connection result = null;
        /**
          * create a real connection...
          * such as :
          * result = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
          */
        return result;
    }

    // 直接返回线程本地变量
    public static Connection getConnection() {
        return connectionThreadLocal.get();
    }

    // 具体操作
    public void getDBConnection() throws SQLException {
        // 从ThreadLocal中获取线程对应的Connection
        Statement statement = getConnection().createStatement();
        //....any other operation
    }
}

不同的线程在使用DBConnectionDemo1 时,根据之前的get具体操作,判断connThreadLocal.get()会去判断是有map,没有则根据initivalValue创建一个Connection对象并添加到本地线程变量中,initivalValue对应的值也就是上述的lamba表达式对应的创建connection的方法返回的结果,下次get则由于已经创建了,则会直接获取已经创建好的connection。
当然,这个例子只是保证了线程安全问题,但是要做到同一事务多个CRUD操作共享同一connection,必须在一个共同的外部类使用ThreadLocal保存connection。

4.2 session管理

程序清单6:后台开发时的session管理

/**
 * @author Carson Chu
 * @date 2020/1/28 20:40
 * @description
 */
public class SessionMgt {
    private static final ThreadLocal THREAD_LOCAL = new ThreadLocal();

    public static Session getSession() throws Exception {
        Session session = (Session) THREAD_LOCAL.get();

        if (session == null) {
            session = getSessionFactory().openSession();
            threadSession.set(session);
        }
       
        return session;
    }
}

5. 小结

ThreadLocalMap是ThreadLocal类的一个静态内部类,它实现了键值对的设置和获取(对比Map对象来理解),每个线程中都有一个独立的ThreadLocalMap副本,它所存储的值,只能被当前线程读取和修改。ThreadLocal类通过操作每一个线程特有的ThreadLocalMap副本,从而实现了变量访问在不同线程中的隔离。因为每个线程的变量都是自己特有的,完全不会有并发错误。还有一点就是,ThreadLocalMap存储的键值对中的键是this对象指向的ThreadLocal对象,而值就是你所设置的对象了。

5.1 优点

ThreadLocal是解决线程安全问题一个很好的思路,它通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题。在很多情况下,ThreadLocal比直接使用synchronized同步机制解决线程安全问题更方便(因为synchronized需要考虑很多的数据安全性和可变变量的数据一致性),且程序可以拥有更高的并发性。

5.2 缺点

因为每个线程都有一个变量副本,所以ThreadLocal内存消耗很大。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值