自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深入理解 Netty FastThreadLocal

開(kāi)發(fā)
本文以線上詭異問(wèn)題為切入點(diǎn),通過(guò)對(duì)比JDK ThreadLocal和Netty FastThreadLocal實(shí)現(xiàn)邏輯以及優(yōu)缺點(diǎn),并深入解讀源碼,由淺入深理解Netty FastThreadLocal。

一、前言

最近在學(xué)習(xí)Netty相關(guān)的知識(shí),在看到Netty FastThreadLocal章節(jié)中,回想起一起線上詭異問(wèn)題。

問(wèn)題描述:外銷業(yè)務(wù)獲取用戶信息判斷是否支持https場(chǎng)景下,獲取的用戶信息有時(shí)候竟然是錯(cuò)亂的。

問(wèn)題分析:使用ThreadLocal保存用戶信息時(shí),未能及時(shí)進(jìn)行remove()操作,而Tomcat工作線程是基于線程池的,會(huì)出現(xiàn)線程重用情況,所以獲取的用戶信息可能是之前線程遺留下來(lái)的。

問(wèn)題修復(fù):ThreadLocal使用完之后及時(shí)remove()、ThreadLocal使用之前也進(jìn)行remove()雙重保險(xiǎn)操作。

接下來(lái),我們繼續(xù)深入了解下JDK ThreadLocal和Netty FastThreadLocal吧。

二、JDK ThreadLocal介紹

ThreadLocal是JDK提供的一個(gè)方便對(duì)象在本線程內(nèi)不同方法中傳遞、獲取的類。用它定義的變量,僅在本線程中可見(jiàn),不受其他線程的影響,與其他線程相互隔離

那具體是如何實(shí)現(xiàn)的呢?如圖1所示,每個(gè)線程都會(huì)有個(gè)ThreadLocalMap實(shí)例變量,其采用懶加載的方式進(jìn)行創(chuàng)建,當(dāng)線程第一次訪問(wèn)此變量時(shí)才會(huì)去創(chuàng)建。

ThreadLocalMap使用線性探測(cè)法存儲(chǔ)ThreadLocal對(duì)象及其維護(hù)的數(shù)據(jù),具體操作邏輯如下:

假設(shè)有一個(gè)新的ThreadLocal對(duì)象,通過(guò)hash計(jì)算它應(yīng)存儲(chǔ)的位置下標(biāo)為x。

此時(shí)發(fā)現(xiàn)下標(biāo)x對(duì)應(yīng)位置已經(jīng)存儲(chǔ)了其他的ThreadLocal對(duì)象,則它會(huì)往后尋找,步長(zhǎng)為1,下標(biāo)變更為x+1。

接下來(lái)發(fā)現(xiàn)下標(biāo)x+1對(duì)應(yīng)位置也已經(jīng)存儲(chǔ)了其他的ThreadLocal對(duì)象,同理則它會(huì)繼續(xù)往后尋找,下標(biāo)變更為x+2。

直到尋找到下標(biāo)為x+3時(shí)發(fā)現(xiàn)是空閑的,然后將該ThreadLocal對(duì)象及其維護(hù)的數(shù)據(jù)構(gòu)建一個(gè)entry對(duì)象存儲(chǔ)在x+3位置。

在ThreadLocalMap中數(shù)據(jù)很多的情況下,很容易出現(xiàn)hash沖突,解決沖突需要不斷的向下遍歷,該操作的時(shí)間復(fù)雜度為O(n),效率較低。

圖1

從下面的代碼中可以看出:

Entry 的 key 是弱引用,value 是強(qiáng)引用。在 JVM 垃圾回收時(shí),只要發(fā)現(xiàn)弱引用的對(duì)象,不管內(nèi)存是否充足,都會(huì)被回收。

但是當(dāng) ThreadLocal 不再使用被 GC 回收后,ThreadLocalMap 中可能出現(xiàn) Entry 的 key 為 NULL,那么 Entry 的 value 一直會(huì)強(qiáng)引用數(shù)據(jù)而得不到釋放,只能等待線程銷毀,從而造成內(nèi)存泄漏

static class ThreadLocalMap {
    // 弱引用,在資源緊張的時(shí)候可以回收部分不再引用的ThreadLocal變量
    static class Entry extends WeakReference<ThreadLocal<?>> {
        // 當(dāng)前ThreadLocal對(duì)象所維護(hù)的數(shù)據(jù)
        Object value;

        Entry(ThreadLocal<?> k, Object v) {
            super(k);
            value = v;
        }
    }
    // 省略其他代碼
}

綜上所述,既然JDK提供的ThreadLocal可能存在效率較低和內(nèi)存泄漏的問(wèn)題,為啥不做相應(yīng)的優(yōu)化和改造呢?

1.從ThreadLocal類注釋看,它是JDK1.2版本引入的,早期可能不太關(guān)注程序的性能。

2.大部分多線程場(chǎng)景下,線程中的ThreadLocal變量較少,因此出現(xiàn)hash沖突的概率相對(duì)較小,及時(shí)偶爾出現(xiàn)了hash沖突,對(duì)程序的性能影響也相對(duì)較小。

3.對(duì)于內(nèi)存泄漏問(wèn)題,ThreadLocal本身已經(jīng)做了一定的保護(hù)措施。作為使用者,在線程中某個(gè)ThreadLocal對(duì)象不再使用或出現(xiàn)異常時(shí),立即調(diào)用 remove() 方法刪除 Entry 對(duì)象,養(yǎng)成良好的編碼習(xí)慣。

三、Netty FastThreadLocal介紹

FastThreadLocal是Netty中對(duì)JDK提供的ThreadLocal優(yōu)化改造版本,從名稱上來(lái)看,它應(yīng)該比ThreadLocal更快了,以應(yīng)對(duì)Netty處理并發(fā)量大、數(shù)據(jù)吞吐量大的場(chǎng)景。

那具體是如何實(shí)現(xiàn)的呢?如圖2所示,每個(gè)線程都會(huì)有個(gè)InternalThreadLocalMap實(shí)例變量。

每個(gè)FastThreadLocal實(shí)例創(chuàng)建時(shí),都會(huì)采用AtomicInteger保證順序遞增生成一個(gè)不重復(fù)的下標(biāo)index,它是該FastThreadLocal對(duì)象維護(hù)的數(shù)據(jù)應(yīng)該存儲(chǔ)的位置。

讀寫數(shù)據(jù)的時(shí)候通過(guò)FastThreadLocal的下標(biāo) index 直接定位到該FastThreadLocal的位置,時(shí)間復(fù)雜度為 O(1),效率較高。

如果該下標(biāo)index遞增到特別大,InternalThreadLocalMap維護(hù)的數(shù)組也會(huì)特別大,所以FastThreadLocal是通過(guò)空間換時(shí)間來(lái)提升讀寫性能的。

圖2

四、Netty FastThreadLocal源碼分析

4.1 構(gòu)造方法

public class FastThreadLocal<V> {
    // FastThreadLocal中的index是記錄了該它維護(hù)的數(shù)據(jù)應(yīng)該存儲(chǔ)的位置
    // InternalThreadLocalMap數(shù)組中的下標(biāo), 它是在構(gòu)造函數(shù)中確定的
    private final int index;
    public InternalThreadLocal() {
        index = InternalThreadLocalMap.nextVariableIndex();
    }
    // 省略其他代碼
}
public final class InternalThreadLocalMap extends UnpaddedInternalThreadLocalMap {
    // 自增索引, ?于計(jì)算下次存儲(chǔ)到Object數(shù)組中的位置
    private static final AtomicInteger nextIndex = new AtomicInteger();
    private static final int ARRAY_LIST_CAPACITY_MAX_SIZE = Integer.MAX_VALUE - 8;
    public static int nextVariableIndex() {
        int index = nextIndex.getAndIncrement();
        if (index >= ARRAY_LIST_CAPACITY_MAX_SIZE || index < 0) {
            nextIndex.set(ARRAY_LIST_CAPACITY_MAX_SIZE);
            throw new IllegalStateException("too many thread-local indexed variables");
        }
        return index;
    }
    // 省略其他代碼
}

上面這兩段代碼在Netty FastThreadLocal介紹中已經(jīng)講解過(guò),這邊就不再重復(fù)介紹了。

4.2 get 方法

public class FastThreadLocal<V> {
    // FastThreadLocal中的index是記錄了該它維護(hù)的數(shù)據(jù)應(yīng)該存儲(chǔ)的位置
    private final int index;
    public final V get() {
        // 獲取當(dāng)前線程的InternalThreadLocalMap
        InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get();
        // 根據(jù)當(dāng)前線程的index從InternalThreadLocalMap中獲取其綁定的數(shù)據(jù)
        Object v = threadLocalMap.indexedVariable(index);
        // 如果獲取當(dāng)前線程綁定的數(shù)據(jù)不為缺省值UNSET,則直接返回;否則進(jìn)行初始化
        if (v != InternalThreadLocalMap.UNSET) {
            return (V) v;
        }
        return initialize(threadLocalMap);
    }
    // 省略其他代碼
}
public final class InternalThreadLocalMap extends UnpaddedInternalThreadLocalMap {
    private static final int INDEXED_VARIABLE_TABLE_INITIAL_SIZE = 32;
    // 未賦值的Object變量(缺省值),當(dāng)?個(gè)與線程綁定的值被刪除之后,會(huì)被設(shè)置為UNSET
    public static final Object UNSET = new Object();
    // 存儲(chǔ)綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組
    private Object[] indexedVariables;
    // slowThreadLocalMap為JDK ThreadLocal存儲(chǔ)InternalThreadLocalMap
    private static final ThreadLocal<InternalThreadLocalMap> slowThreadLocalMap =
            new ThreadLocal<InternalThreadLocalMap>();
    // 從綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組中取出index位置的元素
    public Object indexedVariable(int index) {
        Object[] lookup = indexedVariables;
        return index < lookup.length? lookup[index] : UNSET;
    }
    public static InternalThreadLocalMap get() {
        Thread thread = Thread.currentThread();
        // 判斷當(dāng)前線程是否是FastThreadLocalThread類型
        if (thread instanceof FastThreadLocalThread) {
            return fastGet((FastThreadLocalThread) thread);
        } else {
            return slowGet();
        }
    }
    private static InternalThreadLocalMap fastGet(FastThreadLocalThread thread) {
        // 直接獲取當(dāng)前線程的InternalThreadLocalMap
        InternalThreadLocalMap threadLocalMap = thread.threadLocalMap();
        // 如果當(dāng)前線程的InternalThreadLocalMap還未創(chuàng)建,則創(chuàng)建并賦值
        if (threadLocalMap == null) {
            thread.setThreadLocalMap(threadLocalMap = new InternalThreadLocalMap());
        }
        return threadLocalMap;
    }
    private static InternalThreadLocalMap slowGet() {
        // 使用JDK ThreadLocal獲取InternalThreadLocalMap
        InternalThreadLocalMap ret = slowThreadLocalMap.get();
        if (ret == null) {
            ret = new InternalThreadLocalMap();
            slowThreadLocalMap.set(ret);
        }
        return ret;
    }
    private InternalThreadLocalMap() {
        indexedVariables = newIndexedVariableTable();
    }
    // 初始化一個(gè)32位長(zhǎng)度的Object數(shù)組,并將其元素全部設(shè)置為缺省值UNSET
    private static Object[] newIndexedVariableTable() {
        Object[] array = new Object[INDEXED_VARIABLE_TABLE_INITIAL_SIZE];
        Arrays.fill(array, UNSET);
        return array;
    }
    // 省略其他代碼
}

源碼中 get() 方法主要分為下面3個(gè)步驟處理:

通過(guò)InternalThreadLocalMap.get()方法獲取當(dāng)前線程的InternalThreadLocalMap。
根據(jù)當(dāng)前線程的index 從InternalThreadLocalMap中獲取其綁定的數(shù)據(jù)。
如果不是缺省值UNSET,直接返回;如果是缺省值,則執(zhí)行initialize方法進(jìn)行初始化。

下面我們繼續(xù)分析一下

InternalThreadLocalMap.get()方法的實(shí)現(xiàn)邏輯。

首先判斷當(dāng)前線程是否是FastThreadLocalThread類型,如果是FastThreadLocalThread
類型則直接使用fastGet方法獲取InternalThreadLocalMap,如果不是FastThreadLocalThread類型則使用slowGet方法獲取InternalThreadLocalMap兜底處理。
兜底處理中的slowGet方法會(huì)退化成JDK原生的ThreadLocal獲取InternalThreadLocalMap。
獲取InternalThreadLocalMap時(shí),如果為null,則會(huì)直接創(chuàng)建一個(gè)InternalThreadLocalMap返回。其創(chuàng)建過(guò)過(guò)程中初始化一個(gè)32位長(zhǎng)度的Object數(shù)組,并將其元素全部設(shè)置為缺省值UNSET。

4.3 set 方法

public class FastThreadLocal<V> {
    // FastThreadLocal初始化時(shí)variablesToRemoveIndex被賦值為0
    private static final int variablesToRemoveIndex = InternalThreadLocalMap.nextVariableIndex();
    public final void set(V value) {
        // 判斷value值是否是未賦值的Object變量(缺省值)
        if (value != InternalThreadLocalMap.UNSET) {
            // 獲取當(dāng)前線程對(duì)應(yīng)的InternalThreadLocalMap
            InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get();
            // 將InternalThreadLocalMap中數(shù)據(jù)替換為新的value
            // 并將FastThreadLocal對(duì)象保存到待清理的Set中
            setKnownNotUnset(threadLocalMap, value);
        } else {
            remove();
        }
    }
    private void setKnownNotUnset(InternalThreadLocalMap threadLocalMap, V value) {
        // 將InternalThreadLocalMap中數(shù)據(jù)替換為新的value
        if (threadLocalMap.setIndexedVariable(index, value)) {
            // 并將當(dāng)前的FastThreadLocal對(duì)象保存到待清理的Set中
            addToVariablesToRemove(threadLocalMap, this);
        }
    }
    private static void addToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {
        // 取下標(biāo)index為0的數(shù)據(jù),用于存儲(chǔ)待清理的FastThreadLocal對(duì)象Set集合中
        Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
        Set<FastThreadLocal<?>> variablesToRemove;
        if (v == InternalThreadLocalMap.UNSET || v == null) {
            // 下標(biāo)index為0的數(shù)據(jù)為空,則創(chuàng)建FastThreadLocal對(duì)象Set集合
            variablesToRemove = Collections.newSetFromMap(new IdentityHashMap<FastThreadLocal<?>, Boolean>());
            // 將InternalThreadLocalMap中下標(biāo)為0的數(shù)據(jù),設(shè)置成FastThreadLocal對(duì)象Set集合
            threadLocalMap.setIndexedVariable(variablesToRemoveIndex, variablesToRemove);
        } else {
            variablesToRemove = (Set<FastThreadLocal<?>>) v;
        }
        // 將FastThreadLocal對(duì)象保存到待清理的Set中
        variablesToRemove.add(variable);
    }
    // 省略其他代碼
}
public final class InternalThreadLocalMap extends UnpaddedInternalThreadLocalMap {
    // 未賦值的Object變量(缺省值),當(dāng)?個(gè)與線程綁定的值被刪除之后,會(huì)被設(shè)置為UNSET
    public static final Object UNSET = new Object();
    // 存儲(chǔ)綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組
    private Object[] indexedVariables;
    // 綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組能再次采用x2擴(kuò)容的最大量
    private static final int ARRAY_LIST_CAPACITY_EXPAND_THRESHOLD = 1 << 30;
    private static final int ARRAY_LIST_CAPACITY_MAX_SIZE = Integer.MAX_VALUE - 8;
    // 將InternalThreadLocalMap中數(shù)據(jù)替換為新的value
    public boolean setIndexedVariable(int index, Object value) {
        Object[] lookup = indexedVariables;
        if (index < lookup.length) {
            Object oldValue = lookup[index];
            // 直接將數(shù)組 index 位置設(shè)置為 value,時(shí)間復(fù)雜度為 O(1)
            lookup[index] = value;
            return oldValue == UNSET;
        } else { // 綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組需要擴(kuò)容,則擴(kuò)容數(shù)組并數(shù)組設(shè)置新value
            expandIndexedVariableTableAndSet(index, value);
            return true;
        }
    }
    private void expandIndexedVariableTableAndSet(int index, Object value) {
        Object[] oldArray = indexedVariables;
        final int oldCapacity = oldArray.length;
        int newCapacity;
        // 判斷可進(jìn)行x2方式進(jìn)行擴(kuò)容
        if (index < ARRAY_LIST_CAPACITY_EXPAND_THRESHOLD) {
            newCapacity = index;
            // 位操作,提升擴(kuò)容效率
            newCapacity |= newCapacity >>>  1;
            newCapacity |= newCapacity >>>  2;
            newCapacity |= newCapacity >>>  4;
            newCapacity |= newCapacity >>>  8;
            newCapacity |= newCapacity >>> 16;
            newCapacity ++;
        } else { // 不支持x2方式擴(kuò)容,則設(shè)置綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組容量為最大值
            newCapacity = ARRAY_LIST_CAPACITY_MAX_SIZE;
        }
        // 按擴(kuò)容后的大小創(chuàng)建新數(shù)組,并將老數(shù)組數(shù)據(jù)copy到新數(shù)組
        Object[] newArray = Arrays.copyOf(oldArray, newCapacity);
        // 新數(shù)組擴(kuò)容后的部分賦UNSET缺省值
        Arrays.fill(newArray, oldCapacity, newArray.length, UNSET);
        // 新數(shù)組的index位置替換成新的value
        newArray[index] = value;
        // 綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組用新數(shù)組替換
        indexedVariables = newArray;
    }
    // 省略其他代碼
}

源碼中 set() 方法主要分為下面3個(gè)步驟處理:

判斷value是否是缺省值UNSET,如果value不等于缺省值,則會(huì)通過(guò)InternalThreadLocalMap.get()方法獲取當(dāng)前線程的InternalThreadLocalMap,具體實(shí)現(xiàn)3.2小節(jié)中g(shù)et()方法已做講解。
通過(guò)FastThreadLocal中的setKnownNotUnset()方法將InternalThreadLocalMap中數(shù)據(jù)替換為新的value,并將當(dāng)前的FastThreadLocal對(duì)象保存到待清理的Set中。
如果等于缺省值UNSET或null(else的邏輯),會(huì)調(diào)用remove()方法,remove()具體見(jiàn)后面的代碼分析。

接下來(lái)我們看下

InternalThreadLocalMap.setIndexedVariable方法的實(shí)現(xiàn)邏輯。

判斷index是否超出存儲(chǔ)綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組indexedVariables的長(zhǎng)度,如果沒(méi)有超出,則獲取index位置的數(shù)據(jù),并將該數(shù)組index位置數(shù)據(jù)設(shè)置新value。

如果超出了,綁定到當(dāng)前線程的數(shù)據(jù)的數(shù)組需要擴(kuò)容,則擴(kuò)容該數(shù)組并將它index位置的數(shù)據(jù)設(shè)置新value。

擴(kuò)容數(shù)組以index 為基準(zhǔn)進(jìn)行擴(kuò)容,將數(shù)組擴(kuò)容后的容量向上取整為 2 的次冪。然后將原數(shù)組內(nèi)容拷貝到新的數(shù)組中,空余部分填充缺省值UNSET,最終把新數(shù)組賦值給 indexedVariables。

下面我們?cè)倮^續(xù)看下

FastThreadLocal.addToVariablesToRemove方法的實(shí)現(xiàn)邏輯。

1.取下標(biāo)index為0的數(shù)據(jù)(用于存儲(chǔ)待清理的FastThreadLocal對(duì)象Set集合中),如果該數(shù)據(jù)是缺省值UNSET或null,則會(huì)創(chuàng)建FastThreadLocal對(duì)象Set集合,并將該Set集合填充到下標(biāo)index為0的數(shù)組位置。

2.如果該數(shù)據(jù)不是缺省值UNSET,說(shuō)明Set集合已金被填充,直接強(qiáng)轉(zhuǎn)獲取該Set集合。

3.最后將FastThreadLocal對(duì)象保存到待清理的Set集合中。

4.4 remove、removeAll方法

public class FastThreadLocal<V> {
    // FastThreadLocal初始化時(shí)variablesToRemoveIndex被賦值為0
    private static final int variablesToRemoveIndex = InternalThreadLocalMap.nextVariableIndex();
    public final void remove() {
        // 獲取當(dāng)前線程的InternalThreadLocalMap
        // 刪除當(dāng)前的FastThreadLocal對(duì)象及其維護(hù)的數(shù)據(jù)
        remove(InternalThreadLocalMap.getIfSet());
    }
    public final void remove(InternalThreadLocalMap threadLocalMap) {
        if (threadLocalMap == null) {
            return;
        }
        // 根據(jù)當(dāng)前線程的index,并將該數(shù)組下標(biāo)index位置對(duì)應(yīng)的值設(shè)置為缺省值UNSET
        Object v = threadLocalMap.removeIndexedVariable(index);
        // 存儲(chǔ)待清理的FastThreadLocal對(duì)象Set集合中刪除當(dāng)前FastThreadLocal對(duì)象
        removeFromVariablesToRemove(threadLocalMap, this);
        if (v != InternalThreadLocalMap.UNSET) {
            try {
                // 空方法,用戶可以繼承實(shí)現(xiàn)
                onRemoval((V) v);
            } catch (Exception e) {
                PlatformDependent.throwException(e);
            }
        }
    }
    public static void removeAll() {
        InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.getIfSet();
        if (threadLocalMap == null) {
            return;
        }
        try {
            // 取下標(biāo)index為0的數(shù)據(jù),用于存儲(chǔ)待清理的FastThreadLocal對(duì)象Set集合中
            Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
            if (v != null && v != InternalThreadLocalMap.UNSET) {
                @SuppressWarnings("unchecked")
                Set<FastThreadLocal<?>> variablesToRemove = (Set<FastThreadLocal<?>>) v;
                // 遍歷所有的FastThreadLocal對(duì)象并刪除它們以及它們維護(hù)的數(shù)據(jù)
                FastThreadLocal<?>[] variablesToRemoveArray =
                        variablesToRemove.toArray(new FastThreadLocal[0]);
                for (FastThreadLocal<?> tlv: variablesToRemoveArray) {
                    tlv.remove(threadLocalMap);
                }
            }
        } finally {
            // 刪除InternalThreadLocalMap中threadLocalMap和slowThreadLocalMap數(shù)據(jù)
            InternalThreadLocalMap.remove();
        }
    }
    private static void removeFromVariablesToRemove(
            InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {
        // 取下標(biāo)index為0的數(shù)據(jù),用于存儲(chǔ)待清理的FastThreadLocal對(duì)象Set集合中
        Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
 
        if (v == InternalThreadLocalMap.UNSET || v == null) {
            return;
        }
        @SuppressWarnings("unchecked")
        // 存儲(chǔ)待清理的FastThreadLocal對(duì)象Set集合中刪除該FastThreadLocal對(duì)象
        Set<FastThreadLocal<?>> variablesToRemove = (Set<FastThreadLocal<?>>) v;
        variablesToRemove.remove(variable);
    }
 
    // 省略其他代碼
}
public final class InternalThreadLocalMap extends UnpaddedInternalThreadLocalMap {
    // 根據(jù)當(dāng)前線程獲取InternalThreadLocalMap
       public static InternalThreadLocalMap getIfSet() {
        Thread thread = Thread.currentThread();
        if (thread instanceof FastThreadLocalThread) {
            return ((FastThreadLocalThread) thread).threadLocalMap();
        }
        return slowThreadLocalMap.get();
    }
    // 數(shù)組下標(biāo)index位置對(duì)應(yīng)的值設(shè)置為缺省值UNSET
    public Object removeIndexedVariable(int index) {
        Object[] lookup = indexedVariables;
        if (index < lookup.length) {
            Object v = lookup[index];
            lookup[index] = UNSET;
            return v;
        } else {
            return UNSET;
        }
    }
    // 刪除threadLocalMap和slowThreadLocalMap數(shù)據(jù)
    public static void remove() {
        Thread thread = Thread.currentThread();
        if (thread instanceof FastThreadLocalThread) {
            ((FastThreadLocalThread) thread).setThreadLocalMap(null);
        } else {
            slowThreadLocalMap.remove();
        }
    }
    // 省略其他代碼
}

源碼中 remove() 方法主要分為下面2個(gè)步驟處理:

通過(guò)InternalThreadLocalMap.getIfSet()獲取當(dāng)前線程的InternalThreadLocalMap。具體和3.2小節(jié)get()方法里面獲取當(dāng)前線程的InternalThreadLocalMap相似,這里就不再重復(fù)介紹了。
刪除當(dāng)前的FastThreadLocal對(duì)象及其維護(hù)的數(shù)據(jù)。

源碼中 removeAll() 方法主要分為下面3個(gè)步驟處理:

通過(guò)InternalThreadLocalMap.getIfSet()獲取當(dāng)前線程的InternalThreadLocalMap。
取下標(biāo)index為0的數(shù)據(jù)(用于存儲(chǔ)待清理的FastThreadLocal對(duì)象Set集合),然后遍歷所有的FastThreadLocal對(duì)象并刪除它們以及它們維護(hù)的數(shù)據(jù)。
最后會(huì)將InternalThreadLocalMap本身從線程中移除。

五、總結(jié)

那么使用ThreadLocal時(shí)最佳實(shí)踐又如何呢?

 每次使用完ThreadLocal實(shí)例,在線程運(yùn)行結(jié)束之前的finally代碼塊中主動(dòng)調(diào)用它的remove()方法,清除Entry中的數(shù)據(jù),避免操作不當(dāng)導(dǎo)致的內(nèi)存泄漏。

使?Netty的FastThreadLocal一定比JDK原生的ThreadLocal更快嗎?

不?定。當(dāng)線程是FastThreadLocalThread,則添加、獲取FastThreadLocal所維護(hù)數(shù)據(jù)的時(shí)間復(fù)雜度是 O(1),?使?ThreadLocal可能存在哈希沖突,相對(duì)來(lái)說(shuō)使?FastThreadLocal更?效。但如果是普通線程則可能更慢。

使?FastThreadLocal有哪些優(yōu)點(diǎn)?

正如文章開(kāi)頭介紹JDK原生ThreadLocal存在的缺點(diǎn),F(xiàn)astThreadLocal全部?jī)?yōu)化了,它更?效、而且如果使?的是FastThreadLocal,它會(huì)在任務(wù)執(zhí)?完成后主動(dòng)調(diào)?removeAll?法清除數(shù)據(jù),避免潛在的內(nèi)存泄露。

責(zé)任編輯:龐桂玉 來(lái)源: vivo互聯(lián)網(wǎng)技術(shù)
相關(guān)推薦

2010-06-01 15:25:27

JavaCLASSPATH

2016-12-08 15:36:59

HashMap數(shù)據(jù)結(jié)構(gòu)hash函數(shù)

2020-07-21 08:26:08

SpringSecurity過(guò)濾器

2013-09-22 14:57:19

AtWood

2009-09-25 09:14:35

Hibernate日志

2021-02-17 11:25:33

前端JavaScriptthis

2020-09-23 10:00:26

Redis數(shù)據(jù)庫(kù)命令

2017-01-10 08:48:21

2017-08-15 13:05:58

Serverless架構(gòu)開(kāi)發(fā)運(yùn)維

2024-02-21 21:14:20

編程語(yǔ)言開(kāi)發(fā)Golang

2019-06-25 10:32:19

UDP編程通信

2020-10-15 18:31:36

理解Netty編解碼

2022-11-04 09:43:05

Java線程

2015-11-04 09:57:18

JavaScript原型

2021-05-13 21:27:24

ThreadLocal多線程多線程并發(fā)安全

2013-06-14 09:27:51

Express.jsJavaScript

2021-04-20 23:25:16

執(zhí)行函數(shù)變量

2023-02-10 08:11:43

Linux系統(tǒng)調(diào)用

2017-01-13 22:42:15

iosswift

2023-11-05 12:05:35

JVM內(nèi)存
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)