線程---ThreadLocal

什么是ThreadLocal:

可以把它理解成一個(gè)容器,用于存放線程的局部變量书斜,它為每個(gè)線程提供了一個(gè)獨(dú)立的副本




使用場景:

一般來說牡借,當(dāng)某些數(shù)據(jù)是以線程為作用域并且不同線程具有不同的數(shù)據(jù)副本的時(shí)候,就可以考慮采用ThreadLocal




ThreadLocal的使用:

API如下:

  • public void set(T value):將值放入線程局部變量中
  • public T get():從線程局部變量中獲取值
  • public void remove():從線程局部變量中移除值
  • protected T initialValue():返回線程局部變量中的初始值




例子:

(1)沒有使用ThreadLocal:

public interface BusBase {
    int getPassengerNum();
}

public class Bus implements BusBase{
    private static int passengerNum = 0;
    public int getPassengerNum() {
        passengerNum = passengerNum + 1;
        return passengerNum;
    }
}

public class TestThread extends Thread {
    private BusBase bus;
    public TestThread(BusBase bus) {
        this.bus = bus;
    }
    @Override
    public void run() {
        for (int i = 0; i < 2; i++) {
            System.out.println(Thread.currentThread().getName() + " => " +  bus.getPassengerNum());
        }
    }
}

public class WithoutThreadLocalTest {
    public static void main(String[] args) {
        Bus studentBus = new Bus();
        TestThread thread1 = new TestThread(studentBus);
        TestThread thread2 = new TestThread(studentBus);
        TestThread thread3 = new TestThread(studentBus);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

控制臺(tái)打印結(jié)果如下:

Thread-2 => 3
Thread-0 => 2
Thread-0 => 5
Thread-1 => 1
Thread-1 => 6
Thread-2 => 4

可以看到雨饺,所有線程共用了一個(gè)passengerNum變量

(2)使用了ThreadLocal:

public class BusWithThreadLocal implements BusBase{
    private static ThreadLocal<Integer> passengerNum = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public int getPassengerNum() {
        passengerNum.set(passengerNum.get() + 1);
        return passengerNum.get();
    }
}

public class ThreadLocalTest {
    public static void main(String[] args) {
        BusWithThreadLocal studentBus = new BusWithThreadLocal();
        TestThread thread1 = new TestThread(studentBus);
        TestThread thread2 = new TestThread(studentBus);
        TestThread thread3 = new TestThread(studentBus);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

控制臺(tái)打印結(jié)果如下:

Thread-0 => 1
Thread-0 => 2
Thread-1 => 1
Thread-1 => 2
Thread-2 => 1
Thread-2 => 2

從結(jié)果可以看到钳垮,每個(gè)線程中的passengerNum變量的值互不干擾,一下子變得線程安全了




通過源碼(基于jdk 1.8)分析原理:

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

set方法中通過getMap()方法獲得了當(dāng)前線程的ThreadLocalMap對象额港,getMap方法具體如下:

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

可以看到饺窿,getMap方法就是返回了當(dāng)前線程t的threadLocals變量,而threadLocals變量是ThreadLocalMap的一個(gè)對象移斩,那么ThreadLocalMap又是啥短荐,源碼如下:

 /**
     * ThreadLocalMap is a customized hash map suitable only for
     * maintaining thread local values. No operations are exported
     * outside of the ThreadLocal class. The class is package private to
     * allow declaration of fields in class Thread.  To help deal with
     * very large and long-lived usages, the hash table entries use
     * WeakReferences for keys. However, since reference queues are not
     * used, stale entries are guaranteed to be removed only when
     * the table starts running out of space.
     */
    static class ThreadLocalMap {
/**
         * The entries in this hash map extend WeakReference, using
         * its main ref field as the key (which is always a
         * ThreadLocal object).  Note that null keys (i.e. entry.get()
         * == null) mean that the key is no longer referenced, so the
         * entry can be expunged from table.  Such entries are referred to
         * as "stale entries" in the code that follows.
         */
        static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;

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

ThreadLocalMap是ThreadLocal的一個(gè)靜態(tài)內(nèi)部類倚舀,從注釋可以看出它是用來保存線程本地變量的一個(gè)hashMap,它的key是ThreadLocal變量忍宋,value即為對應(yīng)的保存值痕貌。
看到這里該明白了吧,每個(gè)threadlocal通過set設(shè)置的值糠排,最后是保存在了調(diào)用時(shí)線程的一個(gè)hashMap里面舵稠,這個(gè)hashMap通過threadlocal對象作為key來標(biāo)識(shí)保存的值

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

可以看到,get方法正是從當(dāng)前線程t的threadLocals變量(ThreadLocalMap)中入宦,通過ThreadLocal對象索引哺徊,得到了之前保存下來的value

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市乾闰,隨后出現(xiàn)的幾起案子落追,更是在濱河造成了極大的恐慌,老刑警劉巖涯肩,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件轿钠,死亡現(xiàn)場離奇詭異,居然都是意外死亡病苗,警方通過查閱死者的電腦和手機(jī)疗垛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來硫朦,“玉大人贷腕,你說我怎么就攤上這事∫д梗” “怎么了泽裳?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長破婆。 經(jīng)常有香客問我诡壁,道長,這世上最難降的妖魔是什么荠割? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任妹卿,我火速辦了婚禮,結(jié)果婚禮上蔑鹦,老公的妹妹穿的比我還像新娘夺克。我一直安慰自己,他們只是感情好嚎朽,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布铺纽。 她就那樣靜靜地躺著,像睡著了一般哟忍。 火紅的嫁衣襯著肌膚如雪狡门。 梳的紋絲不亂的頭發(fā)上陷寝,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音其馏,去河邊找鬼凤跑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛叛复,可吹牛的內(nèi)容都是我干的仔引。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼褐奥,長吁一口氣:“原來是場噩夢啊……” “哼咖耘!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起撬码,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤儿倒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后呜笑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體夫否,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年蹈垢,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片袖裕。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡曹抬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出急鳄,到底是詐尸還是另有隱情谤民,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布疾宏,位于F島的核電站张足,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏坎藐。R本人自食惡果不足惜为牍,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望岩馍。 院中可真熱鬧碉咆,春花似錦、人聲如沸蛀恩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽双谆。三九已至壳咕,卻和暖如春席揽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背谓厘。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國打工幌羞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人庞呕。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓新翎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親住练。 傳聞我的和親對象是個(gè)殘疾皇子地啰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容