Java Thread Local 线程本地变量

更多 Java 并发编程方面的文章,请参见文集《Java 并发编程》


Thread Local 线程本地变量

在每个线程中都创建一个变量的副本,线程内共享,线程间互斥。

ThreadLocal 是一个为线程提供线程局部变量的工具类。为线程提供一个线程私有的变量副本,这样多个线程都可以随意更改自己线程局部的变量,不会影响到其他线程。
不过需要注意的是,ThreadLocal 提供的只是一个浅拷贝,如果变量是一个引用类型,那么就要考虑它内部的状态是否会被改变,想要解决这个问题可以通过重写 ThreadLocal 的 initialValue() 函数来自己实现深拷贝,建议在使用 ThreadLocal 时一开始就重写该函数。

ThreadLocal 与像 synchronized 这样的锁机制是不同的:

  • 锁更强调的是如何同步多个线程去正确地共享一个变量,ThreadLocal 则是为了解决同一个变量如何不被多个线程共享
  • 从性能开销的角度上来讲,如果锁机制是用时间换空间的话,那么 ThreadLocal 就是用空间换时间

ThreadLocal 中含有一个叫做 ThreadLocalMap 的内部类,该类为一个采用线性探测法实现的 HashMap。
它的 key 为 ThreadLocal 对象而且还使用了 WeakReference,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;
        }
    }

    /**
     * The initial capacity -- MUST be a power of two.
     */
    private static final int INITIAL_CAPACITY = 16;

    /**
     * The table, resized as necessary.
     * table.length MUST always be a power of two.
     */
    private Entry[] table;

ThreadLocal 中只含有三个成员变量,这三个变量都是与 ThreadLocalMap 的 hash 策略相关的。

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

唯一的实例变量 threadLocalHashCode 是用来进行寻址的 hashcode,它由函数 nextHashCode() 生成,该函数简单地通过一个增量 HASH_INCREMENT 来生成 hashcode。至于为什么这个增量为 0x61c88647,主要是因为 ThreadLocalMap 的初始大小为16,每次扩容都会为原来的2倍,这样它的容量永远为2的n次方,该增量选为 0x61c88647 也是为了尽可能均匀地分布,减少碰撞冲突。

获取当前线程中该变量的值 - get()

要获得当前线程私有的变量副本需要调用 get() 函数。首先,它会调用 getMap() 函数去获得当前线程的ThreadLocalMap,这个函数需要接收当前线程的实例作为参数。如果得到的 ThreadLocalMap 为 null,那么就去调用 setInitialValue() 函数来进行初始化,如果不为 null,就通过 map 来获得变量副本并返回。

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

设置当前线程中该变量的值 - set()

public void set(T value) {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null)
        map.set(this, value);
    else
        createMap(t, value);
}

移除当前线程中该变量的值 - remove()

public void remove() {
    ThreadLocalMap m = getMap(Thread.currentThread());
    if (m != null)
        m.remove(this);
}

getMap() 函数与 createMap() 函数的实现也十分简单,但是通过观察这两个函数可以发现一个秘密:ThreadLocalMap 是存放在 Thread 中的

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

Thread 类中包括:

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

仔细想想其实就能够理解这种设计的思想。有一种普遍的方法是通过一个全局的线程安全的 Map 来存储各个线程的变量副本,但是这种做法已经完全违背了 ThreadLocal 的本意,设计 ThreadLocal 的初衷就是为了避免多个线程去并发访问同一个对象,尽管它是线程安全的。
而在每个 Thread 中存放与它关联的 ThreadLocalMap 是完全符合 ThreadLocal 的思想的,当想要对线程局部变量进行操作时,只需要把 Thread 作为 key 来获得 Thread 中的 ThreadLocalMap 即可。这种设计相比采用一个全局 Map 的方法会多占用很多内存空间,但也因此不需要额外的采取锁等线程同步方法而节省了时间上的消耗。

使用示例

提供的方法:

  • T initialValue():设置初始值
  • public T get():获取当前线程中该变量的值
  • public void set(T value):设置当前线程中该变量的值
  • public void remove():移除当前线程中该变量的值

示例:
我们希望每一个线程维护一个 Unique ID。

public class ThreadLocal_Test {
    private static final AtomicInteger nextId = new AtomicInteger(0);

    public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return nextId.getAndIncrement();
        }
    };

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new MyThread().start();
        }
    }

    static class MyThread extends Thread {
        public void run() {
            System.out.println(Thread.currentThread().getName() + " Unique ID: " + threadLocal.get());
        }
    }
}

输出如下:

Thread-0 Unique ID: 0
Thread-1 Unique ID: 1
Thread-2 Unique ID: 2
Thread-3 Unique ID: 3
Thread-4 Unique ID: 4

ThreadLocal 中的内存泄漏

如果 ThreadLocal 被设置为 null 后,而且没有任何强引用指向它,根据垃圾回收的可达性分析算法,ThreadLocal 将会被回收。这样一来,ThreadLocalMap 中就会含有 key 为 null 的 Entry,而且ThreadLocalMap 是在 Thread 中的,只要线程迟迟不结束,这些无法访问到的 value 会形成内存泄漏。为了解决这个问题,ThreadLocalMap 中的 getEntry()、set() 和 remove() 函数都会清理 key 为 null 的 Entry。

在上文中我们发现了 ThreadLocalMap 的 key 是一个弱引用,那么为什么使用弱引用呢?使用强引用key与弱引用key的差别如下:

  • 强引用 key:ThreadLocal 被设置为 null,由于 ThreadLocalMap 持有 ThreadLocal 的强引用,如果不手动删除,那么 ThreadLocal 将不会回收,产生内存泄漏。
  • 弱引用 key:ThreadLocal 被设置为 null,由于 ThreadLocalMap 持有 ThreadLocal 的弱引用,即便不手动删除,ThreadLocal 仍会被回收,ThreadLocalMap 在之后调用 set()、getEntry() 和 remove() 函数时会清除所有 key 为 null 的 Entry。

但要注意的是,ThreadLocalMap仅仅含有这些被动措施来补救内存泄漏问题。如果你在之后没有调用ThreadLocalMap 的 set()、getEntry() 和 remove() 函数的话,那么仍然会存在内存泄漏问题。
在使用线程池的情况下,如果不及时进行清理,内存泄漏问题事小,甚至还会产生程序逻辑上的问题。所以,为了安全地使用 ThreadLocal,必须要像每次使用完锁就解锁一样,在每次使用完 ThreadLocal 后都要调用 remove() 来清理无用的Entry。


引用:
聊一聊 Spring 中的线程安全性

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

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • 文章来源:http://www.54tianzhisheng.cn/2017/06/04/Java-Thread/...
    beneke阅读 1,541评论 0 1
  • 本文主要讲了java中多线程的使用方法、线程同步、线程数据传递、线程状态及相应的一些线程函数用法、概述等。 首先讲...
    李欣阳阅读 2,510评论 1 15
  • 6月--毕业 在学校上学时,多么希望早点毕业,摆脱学校无用的课程和考试。毕业晚会最后一杯酒碰撞之后,我知道,我终于...
    o翻滚的牛宝宝o阅读 386评论 1 0