ThreadLocal 定義、使用場景、案例、原理、注意事項

(一)定義

This class provides thread-local variables.

線程本地變量,線程獨有的變量,做用域爲當前線程web

 

(二)使用場景

(1) 目標變量只與當前線程有關,每一個線程須要有該值的備份json

(2) 目標變量在線程執行過程當中屢次使用,致使須要在每一個用到的方法都要做爲參數傳遞,ThreadLocal提供了一種從當前線程取變量的途徑ide

 

(三)案例

(1)Java文檔提供的官方案例 -- 線程獨有的標識符

 ThreadId類提供了每一個線程的id,用於標識該線程,經過ThreadId.get()方法獲取該標識this

經過使用ThreadLocal,程序就能夠根據當前線程取查找對應的id標識spa

 public class ThreadId {
      // Atomic integer containing the next thread ID to be assigned
      private static final AtomicInteger nextId = new AtomicInteger(0);
 
      // Thread local variable containing each thread's ID
      private static final ThreadLocal<Integer> threadId =
          new ThreadLocal<Integer>() {
              @Override protected Integer initialValue() {
                  return nextId.getAndIncrement();
          }
      };
 
      // Returns the current thread's unique ID, assigning it if necessary
      public static int get() {
          return threadId.get();
      }
  }

 

(2)fastjson中分配字節空間 - JSON.bytesLocal

在這個案例中,這個變量僅在JSON分配字節空間時起到做用,而不像第一個案例多個地方使用的時候從線程獲取該變量線程

這裏使用ThreadLocal是由於每一個線程分配空間可能不同,所以每一個線程都要有本身的字節空間,要有本身的備份設計

    private final static ThreadLocal<byte[]> bytesLocal = new ThreadLocal<byte[]>();
    private static byte[] allocateBytes(int length) {
        byte[] chars = bytesLocal.get();

        if (chars == null) {
            if (length <= 1024 * 64) {
                chars = new byte[1024 * 64];
                bytesLocal.set(chars);
            } else {
                chars = new byte[length];
            }
        } else if (chars.length < length) {
            chars = new byte[length];
        }

        return chars;
    }

 

(3)cat日誌中的tags

使用cat打日誌時須要傳一個map參數,用於追蹤日誌,用法以下,第三個參數是一個map日誌

logger.info(GlobalContext.getLogTitle(), String.format("調用接口,request:%s", SerializationUtils.serialize(request)), GlobalContext.getLogtags());

3.1 對於每個請求,都是由一個線程取處理,每一個線程接到的請求是不一樣的,所以打日誌時傳入的map也是不同的,這就符合ThreadLocal的第一種場景 -- 變量值只與當前線程有關,關於該變量每一個線程有本身的備份code

3.2 處理一次web請求要多個地方打日誌,若是在每一個方法參數裏都傳這個map的話會顯得很繁瑣,這就符合ThreadLocal的第二種場景 -- 變量在線程執行過程當中須要屢次使用orm

有一種思路是用一個靜態公共類去存儲與提取map,但這個map在每一個請求也就是每一個線程的值都是不同的,所以咱們使用ThreadLocal來存儲,每一個線程都有本身的備份map,經過公共類去存儲與獲取

public final class GlobalContext {
    private static final ThreadLocal<Map<String, String>> localLogTags = ThreadLocal.withInitial(HashMap::new);

    private GlobalContext() {}

    public static Map<String, String> getLogTags() {
        return localLogTags.get();
    }

    public static void setLogTags(Map<String, String> logTags) {
        localLogTags.set(logTags);
    }

    public static void addLogTags(String tagName, String tagValue) {
        localLogTags.get().put(tagName, tagValue);
    }
}

 

 

(四)原理

(1)線程與本地變量的map對應關係由線程來維護,這個Map的key爲ThreadLocal,value就是本地變量Object

查看Thread源碼能夠看到這個變量

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

這樣設計不須要額外的加鎖來保證Map關係讀寫

 

(2)ThreadLocal.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();
    }

2.1 ThreadLocal.getMap(t)就獲取到當前線程ThreadLocal與變量的映射關係map

    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

查看Thread的源碼,能夠看到有這個域,它的訪問級別是package

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

而由由於ThreadLocal跟Thread在同一個包下,因此getMap()能直接獲取到映射關係map

 

2.2 把自身this做爲key值從map中查找對應的value,也就是咱們的局部變量

2.3 若是map是空, 經過ThreadLocal.setInitialValue()初始化該線程的映射關係map

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

觀察setInitialValue(),首先經過ThreadLocal.initialValue()來獲取該ThreadLocal變量的初始值

initialValue()默認是返回null的,可是咱們能夠在建立ThreadLocal變量時重寫該方法來定義一個默認返回,例如:

    ThreadLocal<String> day = new ThreadLocal<String>(){
        @Override
        protected String initialValue() {
            return "sunday";
        }
    };

如今更推薦另外一種方法在初始化時賦默認值:

ThreadLocal<String> day = ThreadLocal.withInitial(() -> "sunday");

 

 (3)ThreadLocal.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);
    }

3.1 ThreadLocal.getMap(t)就獲取到當前線程ThreadLocal與變量的映射關係map

3.2 map不空就把當前ThreadLocal做爲key,要保存的值做爲value寫入線程的映射關係map

3.3 map爲空就調用createMap方法來初始化map

 

(五)注意事項

(1)ThreadLocal變量一般被定義爲private static

{@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).

這樣作的目的是但願與當前線程的狀態相關聯,例如對於一個請求,userID在整個線程處理流程中都要用到,每一個請求處理線程的userID均可能不同,所以使用private static ThreadLocal

 

(2)使用完ThreadLocal變量後,肯定再也不須要用到該變量,調用其remove方法

相關文章
相關標籤/搜索