Java并發(fā)編程 - ThreadLocal

ThreadLocal是什么

/**
 * This class provides thread-local variables.  These variables differ from
 * their normal counterparts in that each thread that accesses one (via its
 * {@code get} or {@code set} method) has its own, independently initialized
 * copy of the variable.  {@code ThreadLocal} instances are typically private
 * static fields in classes that wish to associate state with a thread (e.g.,
 * a user ID or Transaction ID).
 * 
 * <p>For example, the class below generates unique identifiers local to each
 * thread.
 * A thread's id is assigned the first time it invokes {@code ThreadId.get()}
 * and remains unchanged on subsequent calls.
 */     

簡單的翻譯一下就是

這個類提供thread-local變量,這些變量與線程的局部變量不同,每個線程都保存一份改變量的副本,可以通過get或者set方法訪問。如果開發(fā)者希望將類的某個靜態(tài)變量(user ID或者transaction ID)與線程狀態(tài)關聯(lián),則可以考慮使用ThreadLocal。

ThreadLocal提供了線程的局部變量,每個線程都可以通過set()get()來對這個局部變量進行操作,但不會和其他線程的局部變量進行沖突,實現(xiàn)了線程的數(shù)據(jù)隔離。

簡要言之:往ThreadLocal中填充的變量屬于當前線程,該變量對其他線程而言是隔離的。

ThreadLocal使用方法

package com.lance.study;

public class ThreadLocalTest {

    static class ResourceClass {

        public final static ThreadLocal<String> RESOURCE_1 =
                new ThreadLocal<String>();

        public final static ThreadLocal<String> RESOURCE_2 =
                new ThreadLocal<String>();

    }

    static class A {

        public void setOne(String value) {
            ResourceClass.RESOURCE_1.set(value);
        }

        public void setTwo(String value) {
            ResourceClass.RESOURCE_2.set(value);
        }
    }

    static class B {
        public void display() {
            System.out.println(ResourceClass.RESOURCE_1.get()
                    + ":" + ResourceClass.RESOURCE_2.get());
        }
    }

    public static void main(String[] args) {
        final A a = new A();
        final B b = new B();
        for (int i = 0; i < 15; i++) {
            final String resouce1 = "線程-" + i;
            final String resouce2 = " value = (" + i + ")";
            new Thread() {
                @Override
                public void run() {
                    try {
                        a.setOne(resouce1);
                        a.setTwo(resouce2);
                        b.display();
                    } finally {
                        ResourceClass.RESOURCE_1.remove();
                        ResourceClass.RESOURCE_2.remove();
                    }
                }
            }.start();
        }
    }
}

輸出結(jié)果:

線程-0: value = (0)
線程-1: value = (1)
線程-2: value = (2)
線程-3: value = (3)
線程-4: value = (4)
線程-5: value = (5)
線程-6: value = (6)
線程-7: value = (7)
線程-8: value = (8)
線程-9: value = (9)
線程-10: value = (10)
線程-11: value = (11)
線程-12: value = (12)
線程-13: value = (13)
線程-14: value = (14)

大家可以看到輸出的線程順序并非最初定義線程的順序,理論上可以說明多線程應當是并發(fā)執(zhí)行的,但是依然可以保持每個線程里面的值是對應的,說明這些值已經(jīng)達到了線程私有的目的。

ThreadLocal原理

翻看源碼,我們發(fā)現(xiàn)ThreadLocal類的用法非常簡單,它提供了如下三個public方法。

  • T get():返回此線程局部變量中當前線程副本中的值。
  • void remove():刪除此線程局部變量中當前線程的值。
  • void set():設置此線程局部變量中當前線程副本中的值。

現(xiàn)在我們來一步步看看源碼中到底怎么做處理的,首先我們來看一下set方法。

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

首先我們看到set方法先獲取了當前線程,然后又引入了一個ThreadLocalMap的東東,這個東西是什么呢?那我們先去看看ThreadLocalMap這個是個什么。

 /**
     * ThreadLocalMap is a customized hash map suitable only for
     * maintaining thread local values. No operations are exported
     * outside of the ThreadLocal class. The class is package private to
     * allow declaration of fields in class Thread.  To help deal with
     * very large and long-lived usages, the hash table entries use
     * WeakReferences for keys. However, since reference queues are not
     * used, stale entries are guaranteed to be removed only when
     * the table starts running out of space.
     */
    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;
        
        ...
            
    }

這個Entry的key就是ThreadLocal本身,value就是設置的值。

如果該Map不存在,則初始化一個。

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

通過上面我們可以發(fā)現(xiàn)的是ThreadLocalMap是ThreadLocal的一個內(nèi)部類。用Entry類來進行存儲

我們的值都是存儲到這個Map上的,key是當前ThreadLocal對象!

如果該Map可以獲取到,則直接獲取。

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

可以看出,Thread維護了ThreadLocalMap變量。

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

從上面又可以看出,ThreadLocalMap是在ThreadLocal中使用內(nèi)部類來編寫的,但對象的引用是在Thread中!

于是我們可以總結(jié)出:Thread為每個線程維護了ThreadLocalMap這么一個Map,而ThreadLocalMap的key是ThreadLocal對象本身,value則是要存儲的對象。

然后我們來看一下get方法。

    /**
     * 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;
    }
    protected T initialValue() {
        return null;
    }

簡而言之就是獲取Map中的值,如果沒有設置過值,則值默認為null。

最后我們來看一下remove方法。

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

ThreadLocal內(nèi)存泄漏

ThreadLocal內(nèi)存泄漏的根源是:由于ThreadLocalMap的生命周期跟Thread一樣長,如果沒有手動刪除對應key就會導致內(nèi)存泄漏,而不是因為弱引用

這個問題確實存在,沒辦法通過ThreadLocal解決,而是需要程序員在完成ThreadLocal的使用后要養(yǎng)成手動調(diào)用remove的習慣,從而避免內(nèi)存泄漏。

ThreadLocal使用場景

  1. 最典型的是管理數(shù)據(jù)庫的Connection:當時在學JDBC的時候,為了方便操作寫了一個簡單數(shù)據(jù)庫連接池,需要數(shù)據(jù)庫連接池的理由也很簡單,頻繁創(chuàng)建和關閉Connection是一件非常耗費資源的操作,因此需要創(chuàng)建數(shù)據(jù)庫連接池。
  2. Web系統(tǒng)Session的存儲:Web容器采用線程隔離的多線程模型,也就是每一個請求都會對應一條線程,線程之間相互隔離,沒有共享數(shù)據(jù)。這樣能夠簡化編程模型,程序員可以用單線程的思維開發(fā)這種多線程應用。當請求到來時,可以將當前Session信息存儲在ThreadLocal中,在請求處理過程中可以隨時使用Session信息,每個請求之間的Session信息互不影響。當請求處理完成后通過remove方法將當前Session信息清除即可。

ThreadLocal原理總結(jié)

  1. 每個Thread維護著一個ThreadLocalMap的引用。
  2. ThreadLocalMap是ThreadLocal的內(nèi)部類,用Entry來進行存儲。
  3. 調(diào)用ThreadLocal的set()方法時,實際上就是往ThreadLocalMap設置值,key是ThreadLocal對象,值是傳遞進來的對象。
  4. 調(diào)用ThreadLocal的get()方法時,實際上就是往ThreadLocalMap獲取值,key是ThreadLocal對象。
  5. ThreadLocal本身并不存儲值,它只是作為一個key來讓線程從ThreadLocalMap獲取value。
  6. 為了避免內(nèi)存泄漏問題,需要手動調(diào)用remove方法。

正因為這個原理,所以ThreadLocal能夠?qū)崿F(xiàn)“數(shù)據(jù)隔離”,獲取當前線程的局部變量值,不受其他線程影響~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容