This page looks best with JavaScript enabled

ThreadLocal理解

 ·  ☕ 2 min read

不同的线程可以通过同一个 ThreadLocal 对象获取只属于自己的数据。

ThreadLocal.ThreadLocalMap

ThreadLocal的内部类。是以ThreadLocal的 hash 值为数组下标,Entry元素为值的数组。ThreadLocalMap 内部是实现了一个类似 Map 的映射关系,内部的 Entry 继承自WeakReference<ThreadLocal<?>>,它持有ThreadLocal的弱引用,保存ThreadLocal.set(value)传入的value

1
2
3
4
5
6
7
8
9
static class Entry extends WeakReference<ThreadLocal<?>> {
    /** The value associated with this ThreadLocal. */
    Object value;

    Entry(ThreadLocal<?> k, Object v) {
        super(k);
        value = v;
    }
}

ThreadLocal

set & get

get 方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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();
}

set 方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public void set(T value) {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null)
        map.set(this, value);
    else
        createMap(t, value);
}

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

使用地方有:

  • Android的消息循环机制(Looper Handler MessageQueue)就是基于这个。

实例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Main {
    static final ThreadLocal<String> mThreadLocal = new ThreadLocal<>();
    public static void main(String[] args) {
        new Thread("thread1") {
            @Override
            public void run() {
                mThreadLocal.set("value1");
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(currentThread().getName() + " localValue:" + mThreadLocal.get());
            }
        }.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        new Thread("thread2") {
            @Override
            public void run() {
                mThreadLocal.set("value2");
                System.out.println(currentThread().getName() + " localValue:" + mThreadLocal.get());
            }
        }.start();

    }
}

输出:

1
2
thread2 localValue:value2
thread1 localValue:value1

虽然是同一个ThreadLocal对象,而且都调用的同样的set get方法,但是get方法返回的值,一定是与当前线程对应的。

Support the author with
alipay QR Code
wechat QR Code

Yang
WRITTEN BY
Yang
Developer