天天看點

FastThreadLocal 是什麼鬼?吊打 ThreadLocal 的存在!!

一、FastThreadLocal 簡介

FastThreadLocal 并不是 JDK 自帶的,而是在 Netty 中造的一個輪子,Netty 為什麼要重複造輪子呢?

來看下它源碼中的注釋定義:

/**
 * A special variant of {@link ThreadLocal} that yields higher access performance when accessed from a
 * {@link FastThreadLocalThread}.
 * <p>
 * Internally, a {@link FastThreadLocal} uses a constant index in an array, instead of using hash code and hash table,
 * to look for a variable.  Although seemingly very subtle, it yields slight performance advantage over using a hash
 * table, and it is useful when accessed frequently.
 * </p><p>
 * To take advantage of this thread-local variable, your thread must be a {@link FastThreadLocalThread} or its subtype.
 * By default, all threads created by {@link DefaultThreadFactory} are {@link FastThreadLocalThread} due to this reason.
 * </p><p>
 * Note that the fast path is only possible on threads that extend {@link FastThreadLocalThread}, because it requires
 * a special field to store the necessary state.  An access by any other kind of thread falls back to a regular
 * {@link ThreadLocal}.
 * </p>
 *
 * @param <V> the type of the thread-local variable
 * @see ThreadLocal
 */
public class FastThreadLocal<V> {
    ...
}      

FastThreadLocal 是一個特殊的 ThreadLocal 變體,當從線程類 FastThreadLocalThread 中通路 FastThreadLocalm時可以獲得更高的通路性能。如果你還不知道什麼是 ThreadLocal,可以關注公衆号Java技術棧閱讀我之前分享的文章。

二、FastThreadLocal 為什麼快?

在 FastThreadLocal 内部,使用了索引常量代替了 Hash Code 和哈希表,源代碼如下:

private final int index;

public FastThreadLocal() {
    index = InternalThreadLocalMap.nextVariableIndex();
}      
public static int nextVariableIndex() {
    int index = nextIndex.getAndIncrement();
    if (index < 0) {
        nextIndex.decrementAndGet();
        throw new IllegalStateException("too many thread-local indexed variables");
    }
    return index;
}      

FastThreadLocal 内部維護了一個索引常量 index,該常量在每次建立 FastThreadLocal 中都會自動+1,進而保證了下标的不重複性。

這要做雖然會産生大量的 index,但避免了在 ThreadLocal 中計算索引下标位置以及處理 hash 沖突帶來的損耗,是以在操作數組時使用固定下标要比使用計算哈希下标有一定的性能優勢,特别是在頻繁使用時會非常顯著,用空間換時間,這就是高性能 Netty 的巧妙之處。

要利用 FastThreadLocal 帶來的性能優勢,就必須結合使用 FastThreadLocalThread 線程類或其子類,因為 FastThreadLocalThread 線程類會存儲必要的狀态,如果使用了非 FastThreadLocalThread 線程類則會回到正常 ThreadLocal。

Netty 提供了繼承類和實作接口的線程類:

FastThreadLocalRunnable

FastThreadLocalThread

FastThreadLocal 是什麼鬼?吊打 ThreadLocal 的存在!!

Netty 也提供了 DefaultThreadFactory 工廠類,所有由 DefaultThreadFactory 工廠類建立的線程預設就是 FastThreadLocalThread 類型,來看下它的建立過程:

FastThreadLocal 是什麼鬼?吊打 ThreadLocal 的存在!!

先建立 FastThreadLocalRunnable,再建立 FastThreadLocalThread,基友搭配,幹活不累,一定要配合使用才“快”。

三、FastThreadLocal 實戰

要使用 FastThreadLocal 就需要導入 Netty 的依賴了:

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.52.Final</version>
</dependency>      

寫一個測試小示例:

import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.FastThreadLocal;

public class FastThreadLocalTest {

    public static final int MAX = 100000;

    public static void main(String[] args) {
        new Thread(() -> threadLocal()).start();
        new Thread(() -> fastThreadLocal()).start();
    }

    private static void fastThreadLocal() {
        long start = System.currentTimeMillis();
        DefaultThreadFactory defaultThreadFactory = new DefaultThreadFactory(FastThreadLocalTest.class);

        FastThreadLocal<String>[] fastThreadLocal = new FastThreadLocal[MAX];

        for (int i = 0; i < MAX; i++) {
            fastThreadLocal[i] = new FastThreadLocal<>();
        }

        Thread thread = defaultThreadFactory.newThread(() -> {
            for (int i = 0; i < MAX; i++) {
                fastThreadLocal[i].set("java: " + i);
            }

            System.out.println("fastThreadLocal set: " + (System.currentTimeMillis() - start));

            for (int i = 0; i < MAX; i++) {
                for (int j = 0; j < MAX; j++) {
                    fastThreadLocal[i].get();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("fastThreadLocal total: " + (System.currentTimeMillis() - start));
    }

    private static void threadLocal() {
        long start = System.currentTimeMillis();
        ThreadLocal<String>[] threadLocals = new ThreadLocal[MAX];

        for (int i = 0; i < MAX; i++) {
            threadLocals[i] = new ThreadLocal<>();
        }

        Thread thread = new Thread(() -> {
            for (int i = 0; i < MAX; i++) {
                threadLocals[i].set("java: " + i);
            }

            System.out.println("threadLocal set: " + (System.currentTimeMillis() - start));

            for (int i = 0; i < MAX; i++) {
                for (int j = 0; j < MAX; j++) {
                    threadLocals[i].get();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("threadLocal total: " + (System.currentTimeMillis() - start));
    }

}      

結果輸出:

FastThreadLocal 是什麼鬼?吊打 ThreadLocal 的存在!!

可以看出,在大量讀寫面前,寫操作的效率差不多,但讀操作 FastThreadLocal 比 ThreadLocal 快的不是一個數量級,簡直是秒殺 ThreadLocal 的存在。

當我把 MAX 值調整到 1000 時,結果輸出:

FastThreadLocal 是什麼鬼?吊打 ThreadLocal 的存在!!

讀寫操作不多時,ThreadLocal 明顯更勝一籌!

上面的示例是單線程測試多個 *ThreadLocal,即數組形式,另外,我也測試了多線程單個 *ThreadLocal,這時候 FastThreadLocal 效率就明顯要落後于 ThreadLocal。。

最後需要說明的是,在使用完 FastThreadLocal 之後不用 remove 了,因為在 FastThreadLocalRunnable 中已經加了移除邏輯,線上程運作完時會移除全部綁定在目前線程上的所有變量。

FastThreadLocal 是什麼鬼?吊打 ThreadLocal 的存在!!

是以,使用 FastThreadLocal 導緻記憶體溢出的機率會不會要低于 ThreadLocal?

不一定,因為 FastThreadLocal 會産生大量的 index 常量,所謂的空間換時間,是以感覺 FastThreadLocal 記憶體溢出的機率更大,但好在每次使用完都會自動 remove。

四、總結

Netty 中的 FastThreadLocal 在大量頻繁讀寫操作時效率要高于 ThreadLocal,但要注意結合 Netty 自帶的線程類使用,這可能就是 Netty 為什麼高性能的奧妙之一吧!

如果沒有大量頻繁讀寫操作的場景,JDK 自帶的 ThreadLocal 足矣,并且性能還要優于 FastThreadLocal。

好了,今天的分享就到這裡了,覺得有用,轉發分享一下哦。