ThreadLocal 源碼分析

引言

在 jdk/jre/lib/rt.jar 字節碼包中,可以看到關於ThreadLocal類及其內部類編譯後的文件;

 

 


ThreadLocalMap類分析

ThreadLocalMap是ThreadLocal中的內部類,它是實現ThreadLocal類功能的基礎,所以先來看看這個字面上是個Map的類,有哪些方法,實現了哪些功能;

Entry實例對象

Entry是ThreadLocalMap的內部類;Entry纔是體現Map含義的根本所在,ThreadLocalMap只是維護了Entry數組的增刪改查和擴容等

// Entry沿繼承鏈可以追溯到Reference,
// 該類用於保持對某個堆中實例對象不同強度的引用,用於的控制GC對被引用對象的回收時機
 * @author   Mark Reinhold
 * @since    1.2
 */
public abstract class Reference<T> {
    private T referent;         /* Treated specially by GC */
    public T get() {
       return this.referent;
    }
}


static class Entry extends WeakReference<ThreadLocal<?>> {
     /** The value associated with this ThreadLocal. */
     Object value;

     Entry(ThreadLocal<?> k, Object v) {
          super(k);
          value = v;
     }
}

// 即將Entry理解爲
class Entry {
    // 成員變量
    private ThreadLocal<T> threadLocalReferent;
    T value;
    
    // 訪問成員變量的方式,value可直接通過點運算符訪問
    public ThreadLocal<T> get() {
       return this.referent;
    }
 
}

Entry是ThreadLocalMap中的內部類,繼承自弱引用類WeakReference,從Entry的構造函數可以看出,Entry的實例對象用於將一個ThreadLocal對象的引用和一個Object的值關聯起來;

即entry保存一個Entry entry = ( ThreadLocal<T> key, <T> value ) 鍵值對:

  • entry.get()獲取key,用於獲取該entry實例對一個ThreadLocal實例對象的referent引用;
  • entry.value獲取value,用於獲取該entry實例存放的設定值value,即相當於(value)

ThreadLocalMap成員方法分析

ThreadLocalMap只是維護了Entry數組的增刪改查和擴容等

Entry[ ]數組:ThreadLocalMap中維護了一個長度可變的Entry數組:

/**
 * The table, resized as necessary.
 * table.length MUST always be a power of two.
 */
private Entry[] table;

set方法:調用ThreadLocalMap的set方法,相當於傳入構建一個新entry所需要的(key ,value)鍵值對,具體過程如下:

  • 先通過for循環,輪詢檢測傳入的threadLocal對象是否已存在Entry數組中,如果存在並且要設定的value值也和之前相等,則不做任何處理;
  • 若輪詢檢測到數組中存在該ThreadLocal對象key,但value不一樣,則覆蓋設置value;
  • 如果輪詢檢測當前table找不到的key(從源碼中可以看出,key只是個ThreadLocal實例對象,要想轉換成table數組的整型索引,需要結合對象hashCode等一些算法步驟進行索引映射),則根據傳入的<key ,value>構建新entry,並添加至Entry數組中;
  • 當然,具體的數組擴容等細節,就不細談了;
        /**
         * Set the value associated with key.
         *
         * @param key the thread local object
         * @param value the value to be set
         */
        private void set(ThreadLocal<?> key, Object value) {

            // We don't use a fast path as with get() because it is at
            // least as common to use set() to create new entries as
            // it is to replace existing ones, in which case, a fast
            // path would fail more often than not.

            Entry[] tab = table;
            int len = tab.length;
            int i = key.threadLocalHashCode & (len-1);

            for (Entry e = tab[i]; e != null; e = tab[i = nextIndex(i, len)]) {
                ThreadLocal<?> k = e.get();

                if (k == key) {
                    e.value = value;
                    return;
                }

                if (k == null) {
                    replaceStaleEntry(key, value, i);
                    return;
                }
            }

            tab[i] = new Entry(key, value);
            int sz = ++size;
            if (!cleanSomeSlots(i, sz) && sz >= threshold)
                rehash();
        }

getEntry方法:從Entry[ ]中找出指定threadLocal對象對應的值

/**
* Get the entry associated with key.  This method
* itself handles only the fast path: a direct hit of existing
* key. It otherwise relays to getEntryAfterMiss.  This is
* designed to maximize performance for direct hits, in part
* by making this method readily inlinable.
*
* @param  key the thread local object
* @return the entry associated with key, or null if no such
*/
private Entry getEntry(ThreadLocal<?> key) {
    int i = key.threadLocalHashCode & (table.length - 1);
    Entry e = table[i];
    if (e != null && e.get() == key)
         return e;
    else
         return getEntryAfterMiss(key, i, e);
}

remove方法:從Entry[ ]中刪除指定threadLocal對象對應的數組元素entry,具體步驟:

  • 通過key映射成數組索引i,並進行for數組輪詢;
  • 如果找到entry,則先調用Reference中的clear方法,剪斷該entry對threadLocal堆實例的引用關係,方便GC回收;
  • 接着,expungeStaleEntry(i)方法能對Entry[ ]數組進行清除和整理,比較複雜,暫且不細談;
/**
 * Remove the entry for key.
 */
private void remove(ThreadLocal<?> key) {
     Entry[] tab = table;
     int len = tab.length;
     int i = key.threadLocalHashCode & (len-1);
     for (Entry e = tab[i]; e != null; e = tab[i = nextIndex(i, len)]) {
         if (e.get() == key) {
              e.clear();
              expungeStaleEntry(i);
              return;
         }
     }
}

// Reference類中定義的clear方法
/**
 * Clears this reference object.  Invoking this method will not cause this
 * object to be enqueued.
 *
 * <p> This method is invoked only by Java code; when the garbage collector
 * clears references it does so directly, without invoking this method.
 */
public void clear() {
    this.referent = null;
}

ThreadLocalMap的類源碼基本爲ThreadLocal貢獻了大部分的功能,接着在理解ThreadLocalMap作用的基礎上,看看ThrealLocal有哪些方法,並且如何實現線程封閉的?

ThreadLocal成員方法分析

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章