Java線程和多線程(七)——ThreadLocal

Java中的ThreadLocal是用來創建線程本地變量用的。我們都知道,訪問某個對象的所有線程都是能夠共享對象的狀態的,所以這個對象狀態就不是線程安全的。開發者可以通過使用同步來保證線程安全,但是如果不希望使用同步的話,我們也可以使用ThreadLocal變量。

Java ThreadLocal

其實每個線程都有自己的ThreadLocal變量,並且這個變量可以通過get()set()方法來獲取默認值,或者修改其值。

ThreadLocal實例可以配置爲靜態私有變量來關聯線程的狀態。

Java ThreadLocal舉例

下面的例子展示了在Java程序中如何使用ThreadLocal,也同時證實了,線程中都保留一份ThreadLocal的拷貝的。

package com.sapphire.threads;

import java.text.SimpleDateFormat;
import java.util.Random;

public class ThreadLocalExample implements Runnable{

    // SimpleDateFormat is not thread-safe, so give one to each thread
    // SimpleDateFormat is not thread-safe, so give one to each thread
    private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
        @Override
        protected SimpleDateFormat initialValue()
        {
            return new SimpleDateFormat("yyyyMMdd HHmm");
        }
    };

    public static void main(String[] args) throws InterruptedException {
        ThreadLocalExample obj = new ThreadLocalExample();
        for(int i=0 ; i<10; i++){
            Thread t = new Thread(obj, ""+i);
            Thread.sleep(new Random().nextInt(1000));
            t.start();
        }
    }

    @Override
    public void run() {
        System.out.println("Thread Name= "+Thread.currentThread().getName()+" default Formatter = "+formatter.get().toPattern());
        try {
            Thread.sleep(new Random().nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        formatter.set(new SimpleDateFormat());

        System.out.println("Thread Name= "+Thread.currentThread().getName()+" formatter = "+formatter.get().toPattern());
    }
}

上面程序的輸出結果類似下面的結果:

Thread Name= 0 default Formatter = yyyyMMdd HHmm
Thread Name= 1 default Formatter = yyyyMMdd HHmm
Thread Name= 0 formatter = M/d/yy h:mm a
Thread Name= 2 default Formatter = yyyyMMdd HHmm
Thread Name= 1 formatter = M/d/yy h:mm a
Thread Name= 3 default Formatter = yyyyMMdd HHmm
Thread Name= 4 default Formatter = yyyyMMdd HHmm
Thread Name= 4 formatter = M/d/yy h:mm a
Thread Name= 5 default Formatter = yyyyMMdd HHmm
Thread Name= 2 formatter = M/d/yy h:mm a
Thread Name= 3 formatter = M/d/yy h:mm a
Thread Name= 6 default Formatter = yyyyMMdd HHmm
Thread Name= 5 formatter = M/d/yy h:mm a
Thread Name= 6 formatter = M/d/yy h:mm a
Thread Name= 7 default Formatter = yyyyMMdd HHmm
Thread Name= 8 default Formatter = yyyyMMdd HHmm
Thread Name= 8 formatter = M/d/yy h:mm a
Thread Name= 7 formatter = M/d/yy h:mm a
Thread Name= 9 default Formatter = yyyyMMdd HHmm
Thread Name= 9 formatter = M/d/yy h:mm a

從代碼中我們可以看到,10個線程都共享同一個對象,引用的是同一個ThreadLocal<SimpleDateFormat> formatter,看上面的代碼,當線程0執行了formatter.set(new SimpleDateFormat())的時候,顯然,讀取的線程2的formatter仍然是默認的formatter,說明修改公共的formatter其實並沒有生效,從每個線程單獨來看,也沒有破壞線程的安全性。

ThreadLocal原理

到了這裏,很多人會奇怪,ThreadLocal的實現方式,下面我們來看下ThreadLocal的實現方案,首先看下這個其set和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();
    }

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

get和set方法中都有一個核心的概念,就是ThreadLocalMap其實,這個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;
    }

上面的代碼是ThreadLocal中的getMap(Thread t)方法,這個方法來返回綁定到線程上的線程本地變量。線程的內部其實都會維護ThreadLocalMap的。通過前面的set和get方法,那麼我們就知道ThreadLocal的實現方案了。ThreadLocalMap本質上,是一個HashMap,從線程到類型T的一個映射。這也就解釋了,爲什麼我們將ThreadLocal定義爲static final仍然不會影響線程的安全,因爲我們之前代碼中訪問到的formatter其實都已經扔到了ThreadLocalMap裏面,這樣,每次調用get,其實會通過Thread.currentThread()找到對應的ThreadLocalMap,進而找到對應的formmater副本,調用set方法改變的都是ThreadLocalMap裏面的值,自然就不會影響到我們在ThreadLocalExample之中的formatter變量,自然也就不存在線程安全問題。

同時,這也解釋了我們爲什麼ThreadLocal的變量定義爲了static final的,因爲就算定義爲非static的,仍然是沒有任何意義的,只會增加額外的內存而已,因爲我們本質上修改的不是ThreadLocalExample中的實例,而是ThreadLocalMap中的副本,所以定義爲static final正合適。

ThreadLocal本質上其實是將一些變量副本寫入Thread當中的,所以內存佔用會更大,開發者可以根據自己的需求考慮是通過同步或者ThreadLocal的方式來實現線程安全操作。

發佈了44 篇原創文章 · 獲贊 24 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章