ThreadLocal原理及用法詳解

背景

一直以來(lái)對(duì)ThreadLocal用法模棱兩可米罚,不知道怎么用今天好好研究了下給大家分享下髓窜。

  • 1征讲、講解ThreadLocal之前先回顧下什么是取模据某、x^y、弱引用诗箍。

1. 取模運(yùn)算實(shí)際上是計(jì)算兩數(shù)相除以后的余數(shù).

假設(shè)a除以b的商是c癣籽,d是相對(duì)應(yīng)的余數(shù),那么幾乎所有的計(jì)算機(jī)系統(tǒng)都滿足a = c* b + d。所以d=a-b*c筷狼。
?17 % 10 的計(jì)算結(jié)果如下:d = (17) - (17 / 10) x 10 = (17) - (1 x 10) = 7
?-17 % 10 的計(jì)算結(jié)果如下:d = (-17) - (-17 / 10) x 10 = (-17) - (-1 x 10) = -7
-17 % -10 的計(jì)算結(jié)果如下:d = 17 - (17 / -10) x (-10) = (17) - (-1 x -10) = 7
-17 % -10 的計(jì)算結(jié)果如下:d= (-17) - (-17 / -10) x (-10) = (-17) - (1 x -10) = -7
可以看出:運(yùn)算結(jié)果的符號(hào)始終和被模數(shù)的符號(hào)一致瓶籽。

2. x^y 按位取異或。

如:x是二進(jìn)制數(shù)0101 y是二進(jìn)制數(shù)1011 則結(jié)果為x^y=1110,0^1=1,0^0=0,1^1=0,1^0=1埂材!只要有一個(gè)為1就取值為1塑顺。

3. 弱引用

弱引用也是用來(lái)描述非必需對(duì)象的,當(dāng)JVM進(jìn)行垃圾回收時(shí)俏险,無(wú)論內(nèi)存是否充足严拒,都會(huì)回收被弱引用關(guān)聯(lián)的對(duì)象。在java中竖独,用java.lang.ref.WeakReference類來(lái)表示裤唠。這里所說(shuō)的被弱引用關(guān)聯(lián)的對(duì)象是指只有弱引用與之關(guān)聯(lián),如果存在強(qiáng)引用同時(shí)與之關(guān)聯(lián)预鬓,則進(jìn)行垃圾回收時(shí)也不會(huì)回收該對(duì)象巧骚。

  • 2、講解之前先問(wèn)兩個(gè)問(wèn)題

1.什么是ThreadLocal
ThreadLocal從字面意識(shí)可以理解為線程本地變量格二。也就是說(shuō)如果定義了ThreadLocal劈彪,每個(gè)線程往這個(gè)ThreadLocal中讀寫(xiě)是線程隔離,互相之間不會(huì)影響的顶猜。它提供了一種將可變數(shù)據(jù)通過(guò)每個(gè)線程有自己的獨(dú)立副本從而實(shí)現(xiàn)線程封閉的機(jī)制沧奴。
2.實(shí)現(xiàn)的思路是什么
Tread類有一個(gè)類型為T(mén)hreadLocal.ThreadLocalMap的實(shí)例變量threadLocals,我們使用線程的時(shí)候有一個(gè)自己的ThreadLocalMap长窄。ThreadLocalMap有自己的獨(dú)立實(shí)現(xiàn)滔吠,可以簡(jiǎn)單把ThreadLocal視為key,value為代碼中放入的值(實(shí)際上key并不是ThreadLocal本身挠日,通過(guò)源碼可以知道它是一個(gè)弱引用)疮绷。調(diào)用ThreadLocal的set方法時(shí)候,都會(huì)存到ThreadLocalMap里面嚣潜。調(diào)用ThreadLocal的get方法時(shí)候冬骚,在自己map里面找key,從而實(shí)現(xiàn)線程隔離懂算。

  • 3只冻、ThreadLocal最主要的實(shí)現(xiàn)在于ThreadLocalMap這個(gè)內(nèi)部類里面,我們重點(diǎn)關(guān)注ThreadLocalMap這個(gè)類的用法计技,看看兩位大師Josh Bloch and Doug Lea鬼斧神刀設(shè)計(jì)出如此好的類喜德。

  • 4、ThreadLocalMap

image.png

上面是ThreadLocalMap所有的API
ThreadLocalMap提供了一種為T(mén)hreadLocal定制的高效實(shí)現(xiàn)垮媒,并且自帶一種基于弱引用的垃圾清理機(jī)制舍悯。

  1. 存儲(chǔ)結(jié)構(gòu)方面
    存儲(chǔ)結(jié)構(gòu)可以理解為一個(gè)map航棱,但是不要和java.util.Map弄混。
 static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;

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

從上面代碼可以看出Entry便是ThreadLocalMap里定義的節(jié)點(diǎn)贱呐,它繼承了WeakReference類丧诺,定義了一個(gè)類型為Object的value,用于存放塞到ThreadLocal里的值奄薇,key可以視為為T(mén)hreadLocal。

  1. 為什么要用弱引用抗愁,因?yàn)槿绻@里使用普通的key-value形式來(lái)定義存儲(chǔ)結(jié)構(gòu)馁蒂,實(shí)質(zhì)上就會(huì)造成節(jié)點(diǎn)的生命周期與線程強(qiáng)綁定,只要線程沒(méi)有銷毀蜘腌,那么節(jié)點(diǎn)在GC分析中一直處于可達(dá)狀態(tài)沫屡,沒(méi)辦法被回收,而程序本身也無(wú)法判斷是否可以清理節(jié)點(diǎn)撮珠。弱引用是Java四種引用的的第三種(其它三種強(qiáng)引用沮脖、軟引用、虛引用)芯急,比軟引用更加弱一些勺届,如果一個(gè)對(duì)象沒(méi)有強(qiáng)引用鏈可達(dá),那么一般活不過(guò)下一次GC娶耍。當(dāng)某個(gè)ThreadLocal已經(jīng)沒(méi)有強(qiáng)引用可達(dá)免姿,則隨著它被垃圾回收,在ThreadLocalMap里對(duì)應(yīng)的Entry的鍵值會(huì)失效榕酒,這為T(mén)hreadLocalMap本身的垃圾清理提供了便利胚膊。
  2. Entry里面的成員變量和方法
       /**
         * 初始容量,必須為2的冪.
         */
        private static final int INITIAL_CAPACITY = 16;

        /**
         * 根據(jù)需要調(diào)整大小想鹰。
         * 長(zhǎng)度必須總是2的冪紊婉。
         */
        private Entry[] table;

        /**
         * 表中條目的數(shù)量。
         */
        private int size = 0;

        /**
         * 要調(diào)整大小的下一個(gè)大小值辑舷。默認(rèn)為0
         */
        private int threshold; // Default to 0

        /**
         * 將調(diào)整大小閾值設(shè)置維持最壞2/3的負(fù)載因子喻犁。
         */
        private void setThreshold(int len) {
            threshold = len * 2 / 3;
        }

        /**
         *上一個(gè)索引
         */
        private static int nextIndex(int i, int len) {
            return ((i + 1 < len) ? i + 1 : 0);
        }

        /**
         * 下一個(gè)索引
         */
        private static int prevIndex(int i, int len) {
            return ((i - 1 >= 0) ? i - 1 : len - 1);
        }

由于ThreadLocalMap使用線性探測(cè)法來(lái)解決散列沖突,所以實(shí)際上Entry[]數(shù)組在程序邏輯上是作為一個(gè)環(huán)形存在的惩妇。

  1. 構(gòu)造函數(shù)
         /**
         * Construct a new map initially containing (firstKey, firstValue).
         * ThreadLocalMaps are constructed lazily, so we only create
         * one when we have at least one entry to put in it.
         * 構(gòu)造一個(gè)最初包含(firstKey, firstValue)的新映射threadlocalmap是延遲構(gòu)造的株汉,因      
         *  此當(dāng)我們至少有一個(gè)元素可以放進(jìn)去的時(shí)候才去創(chuàng)建。
         */
        ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
            table = new Entry[INITIAL_CAPACITY];
            int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
            table[i] = new Entry(firstKey, firstValue);
            size = 1;
            setThreshold(INITIAL_CAPACITY);
        }

重點(diǎn)說(shuō)下這個(gè)hash函數(shù)int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
ThreadLocal類中有一個(gè)被final修飾的類型為int的threadLocalHashCode歌殃,它在該ThreadLocal被構(gòu)造的時(shí)候就會(huì)生成乔妈,相當(dāng)于一個(gè)ThreadLocal的ID,而它的值來(lái)源于

    private final int threadLocalHashCode = nextHashCode();

    /**
     * The next hash code to be given out. Updated atomically. Starts at
     * zero.
     */
    private static AtomicInteger nextHashCode =
        new AtomicInteger();

    /**
     * 連續(xù)生成的哈希碼之間的區(qū)別——循環(huán)隱式順序線程本地id以近乎最優(yōu)的方式展開(kāi)
     * 用于兩倍大小表的乘法哈希值氓皱。
     */
    private static final int HASH_INCREMENT = 0x61c88647;

    /**
     * 返回下一個(gè)hashcode
     */
    private static int nextHashCode() {
        return nextHashCode.getAndAdd(HASH_INCREMENT);
    }

通過(guò)理論和實(shí)踐算出通路召,當(dāng)我們用0x61c88647作為魔數(shù)累加為每個(gè)ThreadLocal分配各自的ID也就是threadLocalHashCode再與2的冪取模勃刨,得到的結(jié)果分布很均勻。

ThreadLocalMap使用的是線性探測(cè)法股淡,均勻分布的好處在于很快就能探測(cè)到下一個(gè)臨近的可用slot身隐,從而保證效率。這就回答了上文拋出的為什么大小要為2的冪的問(wèn)題唯灵。為了優(yōu)化效率贾铝。對(duì)于& (INITIAL_CAPACITY - 1),相信有過(guò)算法閱讀源碼較多的程序員埠帕,一看就明白垢揩,對(duì)于2的冪作為模數(shù)取模,可以用&(2^n-1) 來(lái)替代%2n敛瓷,位運(yùn)算比取模效率高很多叁巨。至于為什么,因?yàn)閷?duì)2n取模呐籽,只要不是低n位對(duì)結(jié)果的貢獻(xiàn)顯然都是0锋勺,會(huì)影響結(jié)果的只能是低n位。
  1. TreadLocal中的get方法
    ThreadLocal中的get方法會(huì)調(diào)用這個(gè)ThreadLocalMap中的getEntry方法狡蝶,
      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);
        }

        /**
         * Version of getEntry method for use when key is not found in
         * its direct hash slot.
         *
         * @param  key the thread local object
         * @param  i the table index for key's hash code
         * @param  e the entry at table[i]
         * @return the entry associated with key, or null if no such
         */
        private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
            Entry[] tab = table;
            int len = tab.length;

            while (e != null) {
                ThreadLocal<?> k = e.get();
                if (k == key)
                    return e;
                if (k == null)
                    expungeStaleEntry(i);
                else
                    i = nextIndex(i, len);
                e = tab[i];
            }
            return null;
        }
 private int expungeStaleEntry(int staleSlot) {
            Entry[] tab = table;
            int len = tab.length;

            // expunge entry at staleSlot
            tab[staleSlot].value = null;
            tab[staleSlot] = null;
            size--;

            // Rehash until we encounter null
            Entry e;
            int i;
            for (i = nextIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = nextIndex(i, len)) {
                ThreadLocal<?> k = e.get();
                if (k == null) {
                    e.value = null;
                    tab[i] = null;
                    size--;
                } else {
                    int h = k.threadLocalHashCode & (len - 1);
                    if (h != i) {
                        tab[i] = null;

                        // Unlike Knuth 6.4 Algorithm R, we must scan until
                        // null because multiple entries could have been stale.
                        while (tab[h] != null)
                            h = nextIndex(h, len);
                        tab[h] = e;
                    }
                }
            }
            return i;
        } private int expungeStaleEntry(int staleSlot) {
            Entry[] tab = table;
            int len = tab.length;

            // expunge entry at staleSlot
            tab[staleSlot].value = null;
            tab[staleSlot] = null;
            size--;

            // Rehash until we encounter null
            Entry e;
            int i;
            for (i = nextIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = nextIndex(i, len)) {
                ThreadLocal<?> k = e.get();
                if (k == null) {
                    e.value = null;
                    tab[i] = null;
                    size--;
                } else {
                    int h = k.threadLocalHashCode & (len - 1);
                    if (h != i) {
                        tab[i] = null;

                        // Unlike Knuth 6.4 Algorithm R, we must scan until
                        // null because multiple entries could have been stale.
                        while (tab[h] != null)
                            h = nextIndex(h, len);
                        tab[h] = e;
                    }
                }
            }
            return i;
        }

這上面的注釋很清楚庶橱,這里都不在多講了

簡(jiǎn)單說(shuō)下當(dāng)調(diào)用get方法時(shí)候遇到的情況

根據(jù)入?yún)hreadLocal的threadLocalHashCode對(duì)表容量取模得到index如果index對(duì)應(yīng)的slot就是要讀的threadLocal,則直接返回結(jié)果
調(diào)用getEntryAfterMiss線性探測(cè)牢酵,過(guò)程中每碰到無(wú)效slot悬包,調(diào)用expungeStaleEntry進(jìn)行段清理;如果找到了key馍乙,則返回結(jié)果entry
沒(méi)有找到key布近,返回null

  1. TreadLocal中的set方法
    /**
         * 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();
        }

      /**
         * Replace a stale entry encountered during a set operation
         * with an entry for the specified key.  The value passed in
         * the value parameter is stored in the entry, whether or not
         * an entry already exists for the specified key.
         *
         * As a side effect, this method expunges all stale entries in the
         * "run" containing the stale entry.  (A run is a sequence of entries
         * between two null slots.)
         *
         * @param  key the key
         * @param  value the value to be associated with key
         * @param  staleSlot index of the first stale entry encountered while
         *         searching for key.
         */
        private void replaceStaleEntry(ThreadLocal<?> key, Object value,
                                       int staleSlot) {
            Entry[] tab = table;
            int len = tab.length;
            Entry e;

            // Back up to check for prior stale entry in current run.
            // We clean out whole runs at a time to avoid continual
            // incremental rehashing due to garbage collector freeing
            // up refs in bunches (i.e., whenever the collector runs).
            int slotToExpunge = staleSlot;
            for (int i = prevIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = prevIndex(i, len))
                if (e.get() == null)
                    slotToExpunge = i;

            // Find either the key or trailing null slot of run, whichever
            // occurs first
            for (int i = nextIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = nextIndex(i, len)) {
                ThreadLocal<?> k = e.get();

                // If we find key, then we need to swap it
                // with the stale entry to maintain hash table order.
                // The newly stale slot, or any other stale slot
                // encountered above it, can then be sent to expungeStaleEntry
                // to remove or rehash all of the other entries in run.
                if (k == key) {
                    e.value = value;

                    tab[i] = tab[staleSlot];
                    tab[staleSlot] = e;

                    // Start expunge at preceding stale entry if it exists
                    if (slotToExpunge == staleSlot)
                        slotToExpunge = i;
                    cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
                    return;
                }

                // If we didn't find stale entry on backward scan, the
                // first stale entry seen while scanning for key is the
                // first still present in the run.
                if (k == null && slotToExpunge == staleSlot)
                    slotToExpunge = i;
            }

            // If key not found, put new entry in stale slot
            tab[staleSlot].value = null;
            tab[staleSlot] = new Entry(key, value);

            // If there are any other stale entries in run, expunge them
            if (slotToExpunge != staleSlot)
                cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
        }

        /**
         * Heuristically scan some cells looking for stale entries.
         * This is invoked when either a new element is added, or
         * another stale one has been expunged. It performs a
         * logarithmic number of scans, as a balance between no
         * scanning (fast but retains garbage) and a number of scans
         * proportional to number of elements, that would find all
         * garbage but would cause some insertions to take O(n) time.
         *
         * @param i a position known NOT to hold a stale entry. The
         * scan starts at the element after i.
         *
         * @param n scan control: {@code log2(n)} cells are scanned,
         * unless a stale entry is found, in which case
         * {@code log2(table.length)-1} additional cells are scanned.
         * When called from insertions, this parameter is the number
         * of elements, but when from replaceStaleEntry, it is the
         * table length. (Note: all this could be changed to be either
         * more or less aggressive by weighting n instead of just
         * using straight log n. But this version is simple, fast, and
         * seems to work well.)
         *
         * @return true if any stale entries have been removed.
         */
        private boolean cleanSomeSlots(int i, int n) {
            boolean removed = false;
            Entry[] tab = table;
            int len = tab.length;
            do {
                i = nextIndex(i, len);
                Entry e = tab[i];
                if (e != null && e.get() == null) {
                    n = len;
                    removed = true;
                    i = expungeStaleEntry(i);
                }
            } while ( (n >>>= 1) != 0);
            return removed;
        }

    private void rehash() {
            expungeStaleEntries();

            // Use lower threshold for doubling to avoid hysteresis
            if (size >= threshold - threshold / 4)
                resize();
        }

        /**
         * Double the capacity of the table.
         */
        private void resize() {
            Entry[] oldTab = table;
            int oldLen = oldTab.length;
            int newLen = oldLen * 2;
            Entry[] newTab = new Entry[newLen];
            int count = 0;

            for (int j = 0; j < oldLen; ++j) {
                Entry e = oldTab[j];
                if (e != null) {
                    ThreadLocal<?> k = e.get();
                    if (k == null) {
                        e.value = null; // Help the GC
                    } else {
                        int h = k.threadLocalHashCode & (newLen - 1);
                        while (newTab[h] != null)
                            h = nextIndex(h, newLen);
                        newTab[h] = e;
                        count++;
                    }
                }
            }

            setThreshold(newLen);
            size = count;
            table = newTab;
        }

        /**
         * Expunge all stale entries in the table.
         */
        private void expungeStaleEntries() {
            Entry[] tab = table;
            int len = tab.length;
            for (int j = 0; j < len; j++) {
                Entry e = tab[j];
                if (e != null && e.get() == null)
                    expungeStaleEntry(j);
            }
        }
簡(jiǎn)單總結(jié)下上面的用法

a、探測(cè)過(guò)程中slot都不無(wú)效丝格,并且順利找到key所在的slot撑瞧,直接替換即可
b、探測(cè)過(guò)程中發(fā)現(xiàn)有無(wú)效slot显蝌,調(diào)用replaceStaleEntry预伺,效果是最終一定會(huì)把key和value放在這個(gè)slot,并且會(huì)盡可能清理無(wú)效slot
在replaceStaleEntry過(guò)程中曼尊,如果找到了key酬诀,則做一個(gè)swap把它放到那個(gè)無(wú)效slot中,value置為新值
在replaceStaleEntry過(guò)程中骆撇,沒(méi)有找到key瞒御,直接在無(wú)效slot原地放entry
c、探測(cè)沒(méi)有發(fā)現(xiàn)key神郊,則在連續(xù)段末尾的后一個(gè)空位置放上entry肴裙,這也是線性探測(cè)法的一部分趾唱。放完后,做一次啟發(fā)式清理蜻懦,如果沒(méi)清理出去key甜癞,并且當(dāng)前table大小已經(jīng)超過(guò)閾值了,則做一次rehash宛乃,rehash函數(shù)會(huì)調(diào)用一次全量清理slot方法也expungeStaleEntries悠咱,如果完了之后table大小超過(guò)了threshold - threshold / 4,則進(jìn)行擴(kuò)容2倍
6烤惊、ThreadLocal中的remove方法調(diào)用TreadLocalMap中的remove

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

這個(gè)方法比較簡(jiǎn)單乔煞,找到key就清除

  1. ThreadLocal會(huì)不會(huì)遇到內(nèi)存泄漏問(wèn)題
    有關(guān)于內(nèi)存泄露是因?yàn)樵谟芯€程復(fù)用如線程池的場(chǎng)景中,一個(gè)線程的生命周期很長(zhǎng)柒室,大對(duì)象長(zhǎng)期不被回收影響系統(tǒng)運(yùn)行效率與安全。如果線程不會(huì)復(fù)用逗宜,用完即銷毀了也不會(huì)有ThreadLocal引發(fā)內(nèi)存泄露的問(wèn)題雄右。
    仔細(xì)讀過(guò)ThreadLocalMap的源碼,可以推斷纺讲,如果在使用的ThreadLocal的過(guò)程中擂仍,習(xí)慣加上remove,這樣是不會(huì)引起內(nèi)存泄漏熬甚。
    如果沒(méi)有進(jìn)行remove呢逢渔?如果對(duì)應(yīng)線程之后調(diào)用ThreadLocal的get和set方法都有很高的概率會(huì)順便清理掉無(wú)效對(duì)象,斷開(kāi)value強(qiáng)引用乡括,從而大對(duì)象被收集器回收肃廓。
    我們應(yīng)該考慮到何時(shí)調(diào)用ThreadLocal的remove方法。一個(gè)比較熟悉的場(chǎng)景就是對(duì)于一個(gè)請(qǐng)求一個(gè)線程的server如tomcat诲泌,在代碼中對(duì)web api作一個(gè)切面盲赊,存放一些如用戶名等用戶信息,在連接點(diǎn)方法結(jié)束后敷扫,再顯式調(diào)用remove哀蘑。

以上都是理論,我們做一個(gè)小實(shí)驗(yàn)

/**
 * @author shuliangzhao
 * @Title: ThreadLocalDemo
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/1 0:00
 */
public class ThreadLocalDemo {
    private static ThreadLocal<ThreadLocalDemo> t = new ThreadLocal<>();
    private ThreadLocalDemo() {}

    public static ThreadLocalDemo getInstance() {
        ThreadLocalDemo threadLocalDemo = ThreadLocalDemo.t.get();
        if (null == threadLocalDemo) {
            threadLocalDemo = new ThreadLocalDemo();
            t.set(threadLocalDemo);
        }
        return threadLocalDemo;
    }
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
      long i =  (1L << 32) - (long) ((1L << 31) * (Math.sqrt(5) - 1));
        System.out.println(i);
        int i1 = 55&(2^2-1);
        System.out.println(55%2^2);
        System.out.println(i1);
    }
}
/**
 * @author shuliangzhao
 * @Title: ThreadLocalTest
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/1 0:03
 */
public class ThreadLocalTest {
    public static void main(String[] args) {
        for(int i=0; i<2;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Double d = Math.random()*10;
                    ThreadLocalDemo.getInstance().setName("name "+d);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }
    static class A{
        public void get(){
            System.out.println(ThreadLocalDemo.getInstance().getName());
        }
    }
    static class B{
        public void get(){
            System.out.println(ThreadLocalDemo.getInstance().getName());
        }
    }

}

運(yùn)行結(jié)果


image.png

到這里我們就把ThreadLocal講完了葵第,可以多看看源碼绘迁,看看大牛們是怎么設(shè)計(jì)出如此優(yōu)美的代碼。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末卒密,一起剝皮案震驚了整個(gè)濱河市缀台,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌栅受,老刑警劉巖将硝,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件恭朗,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡依疼,警方通過(guò)查閱死者的電腦和手機(jī)痰腮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)律罢,“玉大人膀值,你說(shuō)我怎么就攤上這事∥蠹” “怎么了沧踏?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)巾钉。 經(jīng)常有香客問(wèn)我翘狱,道長(zhǎng),這世上最難降的妖魔是什么砰苍? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任潦匈,我火速辦了婚禮,結(jié)果婚禮上赚导,老公的妹妹穿的比我還像新娘茬缩。我一直安慰自己,他們只是感情好吼旧,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布凰锡。 她就那樣靜靜地躺著,像睡著了一般圈暗。 火紅的嫁衣襯著肌膚如雪掂为。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,688評(píng)論 1 305
  • 那天厂置,我揣著相機(jī)與錄音菩掏,去河邊找鬼。 笑死昵济,一個(gè)胖子當(dāng)著我的面吹牛智绸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播访忿,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼瞧栗,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了海铆?” 一聲冷哼從身側(cè)響起迹恐,我...
    開(kāi)封第一講書(shū)人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎卧斟,沒(méi)想到半個(gè)月后殴边,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體憎茂,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年锤岸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了竖幔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡是偷,死狀恐怖拳氢,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蛋铆,我是刑警寧澤馋评,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站刺啦,受9級(jí)特大地震影響留特,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜玛瘸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一磕秤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捧韵,春花似錦、人聲如沸汉操。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)磷瘤。三九已至芒篷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間采缚,已是汗流浹背针炉。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留扳抽,地道東北人篡帕。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像贸呢,于是被迫代替她去往敵國(guó)和親镰烧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

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