并发学习

ThreadLocal类解析

2017-06-05  本文已影响0人  二月_春风

前言

最近在看并发编程艺术这本书,对看书的一些笔记及个人工作中的总结。

00300187364_a0154fd4.jpg

今天看NBA比赛,虽然输了,但是老詹一直是我偶像。

概念:线程局部变量,是一种多线程间并发访问变量的解决方案。与其synchronized等加锁的方式不同,ThreadLocal完全不提供锁,而使用以空间换时间的手段,为每个线程提供变量的独立副本,以保障线程安全。

先看一个demo:

public class ThreadLocalTest {

    public static ThreadLocal<String> th = new ThreadLocal<>();

    public void setTh(String value){
        th.set(value);
    }
    public void getTh(){
        System.out.println(Thread.currentThread().getName() + ":" + this.th.get());
    }

    public static void main(String[] args) throws InterruptedException {

        final ThreadLocalTest ct = new ThreadLocalTest();
        Thread t1 = new Thread(() -> {
            ct.setTh("张三");
            ct.getTh();
        }, "t1");

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(2000);
                ct.setTh("李四");
                System.out.println("222");
                ct.getTh();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}

结果:

t1:张三
222
t2:李四

发现t1线程和t2线程设置的value值互不影响。

实际运用场景
之前在工作中为了统计一个方法每个线程的响应总时间使用了ThreadLocal来做的,就是在进入方法的时候记录一个时间,方法结束的时候记录一个时间,将时间差se到ThreadLocal中。这边就不写具体的demo了。

ThreadLocal的底层实现
从线程Thread的角度来看,每个线程内部都会持有一个对ThreadLocalMap实例的引用,ThreadLocalMap实例相当于线程的局部变量空间,存储着线程各自的数据。
我们在上面的demo中了解了ThreadLocal的二个重要方法,set和get方法,看其源码实现:

    /**
     * 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);//0
        if (map != null)
            map.set(this, value); //1
        else
            createMap(t, value); //2
}

getMap方法:

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

threadLocals是 ThreadLocal类维护的一个ThreadLocalMap一个实例,我们知道每个线程都持有该ThreadLocalMap的实例引用,

ThreadLocal.ThreadLocalMap threadLocals = null;
/**
  * 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); //当前线程的threadLocals引用
}
/**
  * Construct a new map initially containing (firstKey, firstValue).
  * ThreadLocalMaps are constructed lazily, so we only create
  * one when we have at least one entry to put in it.
  */
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
            table = new Entry[INITIAL_CAPACITY];
            int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
            table[i] = new Entry(firstKey, firstValue);
            size = 1;
            setThreshold(INITIAL_CAPACITY);
}

将ThreadLocal实例和value值封装成Entry对象。

调试:


图片.png 图片.png

this就是当前对象的ThreadLocal对象,也就是ThreadLocal@609对象。

图片.png

以上的结论是自己调试的结果,可能有很多的错误,希望指正。

根据调试我大概说一下自己的理解,每个线程底层都维护了一个ThreadLocalMap对象,key是根据int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);生成一个i值,每个线程生成的i都一致(比如我们demo中生成的 i是5),value就是我们设置的值,比如说这个demo的张三和李四,然后ThreadLocalMap都保存ThreadLocal的引用。

get方法的时候直接根据当前的ThreadLocal引用对象,获取当前线程的ThreadLocalMap,然后获得具体的value值。自己画个简单的图。

图片.png
上一篇 下一篇

猜你喜欢

热点阅读