1. 程式人生 > >Java執行緒和多執行緒(七)——ThreadLocal

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的方式來實現執行緒安全操作。