Looper與ThreadLocal

Looper

static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

Looper中創(chuàng)建了一個ThreadLocal<T> TLooper的變量

public static void prepare() {
    prepare(true);
}

private static void prepare(boolean quitAllowed) {
    if (sThreadLocal.get() != null) {
        throw new RuntimeException("Only one Looper may be created per thread");
    }
    sThreadLocal.set(new Looper(quitAllowed));
}

?prepare?方法中硼补,調(diào)用了?sThreadLocal.set()?和?sThreadLocal.get()?兩個方法霎终,會在下面展開

其中?new Looper(quitAllowed)?是調(diào)用了構(gòu)造函數(shù)

private Looper(boolean quitAllowed) {
    mQueue = new MessageQueue(quitAllowed);
    mThread = Thread.currentThread();
}

很顯然,在任意線程調(diào)用国旷,創(chuàng)建的?Looper?就會持有該線程的引用。

同時我們看到,還有另一個方法能創(chuàng)建調(diào)用了?prepare

public static void prepareMainLooper() {
    prepare(false);
    synchronized (Looper.class) {
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        sMainLooper = myLooper();
    }
}

在這個方法中陶缺,先是通過?prepare?方法,調(diào)用?sMainLooper?的?sThreadLocal.set()?方法洁灵。

  • 接著通過下面這個?myLooper()?方法獲取饱岸,這時候這個?threadLocal?又出現(xiàn)了,我們再次放一放徽千。
/**
 * Return the Looper object associated with the current thread.  Returns
 * null if the calling thread is not associated with a Looper.
 */
public static @Nullable Looper myLooper() {
    return sThreadLocal.get();
}

看到這兒可能會有疑問苫费,注釋中說的是返回與當(dāng)前線程有關(guān)聯(lián)的?Looper?,并不是返回主線程的?Looper?双抽,那為何?prepareMainLooper?方法是直接調(diào)用這個方法呢百框?

我們回到?prepareMainLooper()?方法,它的注釋是這樣說的

/**
 * Initialize the current thread as a looper, marking it as an
 * application's main looper. The main looper for your application
 * is created by the Android environment, so you should never need
 * to call this function yourself.  See also: {@link #prepare()}
 */

也就是說荠诬,這個方法只會在應(yīng)用創(chuàng)建時被程序自己調(diào)用琅翻,不應(yīng)該被我們調(diào)用,這也確保了當(dāng)前是處在主線程中柑贞。

public static Looper getMainLooper() {
    synchronized (Looper.class) {
        return sMainLooper;
    }
}

這個靜態(tài)方法方椎,方便了我們在程序中,可以快速的拿到主線程Looper钧嘶。

ThreadLocal

說到現(xiàn)在棠众,我們已經(jīng)把?Looper?中和?ThreadLocal?相關(guān)的部分都講完了,接下來有决,就直接跳轉(zhuǎn)到?ThreadLocal?類中闸拿,看看?set??get?分別是什么。

set

/**
 * Sets the current thread's copy of this thread-local variable
 * to the specified value.  Most subclasses will have no need to
 * override this method, relying solely on the {@link #initialValue}
 * method to set the values of thread-locals.
 *
 * @param value the value to be stored in the current thread's copy of
 *        this thread-local.
 */
public void set(T value) {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null)
        map.set(this, value);
    else
        createMap(t, value);
}

get

/**
 * Returns the value in the current thread's copy of this
 * thread-local variable.  If the variable has no value for the
 * current thread, it is first initialized to the value returned
 * by an invocation of the {@link #initialValue} method.
 *
 * @return the current thread's value of this thread-local
 */
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();
}

前兩行代碼都是相同的书幕,獲取當(dāng)前線程的ThreadLocalMap變量新荤。

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

查看Thread代碼,發(fā)現(xiàn)這個?threadLocals?是?ThreadLocal.ThreadLocalMap?類型的變量台汇。

?ThreadLocalMap?是定義在?ThreadLocal?中的內(nèi)部類

/**
 * Create the map associated with a ThreadLocal. Overridden in
 * InheritableThreadLocal.
 *
 * @param t the current thread
 * @param firstValue value for the initial entry of the map
 */
void createMap(Thread t, T firstValue) {
    t.threadLocals = new ThreadLocalMap(this, firstValue);
}
//構(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.
 */
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);
}

?createMap?中調(diào)用了構(gòu)造函數(shù)苛骨,參數(shù)為?this?和?firstValue?

我們發(fā)現(xiàn),對于Looper來說苟呐,第一個參數(shù)實(shí)際上是?ThreadLocal<Looper>?痒芝,而第二個參數(shù)是這個范型對應(yīng)的具體對象。

每個線程都有?ThreadLocalMap?對象牵素,其中的對應(yīng)一個hash表?Entry[]?严衬,存儲了不同范型對象的鍵值對。

下載.png

原理總結(jié)

線程共享變量緩存如下:

Thread.ThreadLocalMap<ThreadLocal, Object>;

  1. Thread: 當(dāng)前線程笆呆,可以通過Thread.currentThread()獲取请琳。

  2. ThreadLocal:我們的static ThreadLocal變量粱挡。

  3. Object: 當(dāng)前線程共享變量。

我們調(diào)用ThreadLocal.get方法時单起,實(shí)際上是從當(dāng)前線程中獲取ThreadLocalMap<ThreadLocal, Object>抱怔,然后根據(jù)當(dāng)前ThreadLocal獲取當(dāng)前線程共享變量Object。

ThreadLocal.set嘀倒,ThreadLocal.remove實(shí)際上是同樣的道理屈留。

這種存儲結(jié)構(gòu)的好處:

  1. 線程死去的時候,線程共享變量ThreadLocalMap則銷毀测蘑。

  2. ThreadLocalMap<ThreadLocal,Object>鍵值對數(shù)量為ThreadLocal的數(shù)量灌危,一般來說ThreadLocal數(shù)量很少,相比在ThreadLocal中用Map<Thread, Object>鍵值對存儲線程共享變量(Thread數(shù)量一般來說比ThreadLocal數(shù)量多)碳胳,性能提高很多勇蝙。

內(nèi)存泄漏問題

其中?Entry?類的定義如下

/**
 * 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, Object>弱引用問題:

當(dāng)線程沒有結(jié)束,但是ThreadLocal已經(jīng)被回收挨约,則可能導(dǎo)致線程中存ThreadLocalMap<null, Object>的鍵值對味混,造成內(nèi)存泄露。

ThreadLocal被回收诫惭,ThreadLocal關(guān)聯(lián)的線程共享變量還存在)翁锡。

雖然ThreadLocal的get,set方法可以清除ThreadLocalMap中key為null的value夕土,但是get馆衔,set方法在內(nèi)存泄露后并不會必然調(diào)用,所以為了防止此類情況的出現(xiàn)怨绣,我們有兩種手段角溃。

1、使用完線程共享變量后篮撑,顯式調(diào)用ThreadLocalMap.remove方法清除線程共享變量减细;

2、JDK建議ThreadLocal定義為private static赢笨,這樣ThreadLocal的弱引用問題則不存在了未蝌。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市质欲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌糠馆,老刑警劉巖嘶伟,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異又碌,居然都是意外死亡九昧,警方通過查閱死者的電腦和手機(jī)绊袋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铸鹰,“玉大人癌别,你說我怎么就攤上這事√A” “怎么了展姐?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長剖毯。 經(jīng)常有香客問我圾笨,道長,這世上最難降的妖魔是什么逊谋? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任擂达,我火速辦了婚禮,結(jié)果婚禮上胶滋,老公的妹妹穿的比我還像新娘板鬓。我一直安慰自己,他們只是感情好究恤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布俭令。 她就那樣靜靜地躺著,像睡著了一般丁溅。 火紅的嫁衣襯著肌膚如雪唤蔗。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天窟赏,我揣著相機(jī)與錄音妓柜,去河邊找鬼。 笑死涯穷,一個胖子當(dāng)著我的面吹牛棍掐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播拷况,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼作煌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了赚瘦?” 一聲冷哼從身側(cè)響起粟誓,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎起意,沒想到半個月后鹰服,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年悲酷,在試婚紗的時候發(fā)現(xiàn)自己被綠了套菜。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡设易,死狀恐怖逗柴,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情顿肺,我是刑警寧澤戏溺,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站挟冠,受9級特大地震影響于购,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜知染,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一肋僧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧控淡,春花似錦嫌吠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至涧狮,卻和暖如春炕矮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背者冤。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工肤视, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人涉枫。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓邢滑,卻偏偏與公主長得像,于是被迫代替她去往敵國和親愿汰。 傳聞我的和親對象是個殘疾皇子困后,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評論 2 353

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