ThreadLocal理解

StefanJi發表於2019-04-01

不同的執行緒可以通過同一個 ThreadLocal 物件獲取只屬於自己的資料。

ThreadLocal.ThreadLocalMap

ThreadLocal的內部類。是以ThreadLocal的 hash 值為陣列下標,Entry元素為值的陣列。ThreadLocalMap 內部是實現了一個類似 Map 的對映關係,內部的 Entry 繼承自WeakReference<ThreadLocal<?>>,它持有ThreadLocal的弱引用,儲存ThreadLocal.set(value)傳入的value

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 方法

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 方法

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)就是基於這個。
  • ...

例項:

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

    }
}
複製程式碼

輸出:

thread2 localValue:value2
thread1 localValue:value1
複製程式碼

雖然是同一個ThreadLocal物件,而且都呼叫的同樣的set get方法,但是get方法返回的值,一定是與當前執行緒對應的。

相關文章