Handler Looper Message原理淺析

Handler Looper MessageQueue 原理淺析

說到Andorid線程間通信最常見的就是Handler偿荷,Handler的原理是個大廠面試必問,可見其重要程度唠椭。本文在這里從源碼角度淺析一下Handler跳纳,Looper和MessageQueue

1.從Looper開始

首先我們知道,Looper從字面理解就是輪子的意思贪嫂。

Looper的源碼很少寺庄,區(qū)區(qū)不到300行且大半都是注釋。我們直接上代碼:

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

可以看到Looper的構造方法里撩荣,new了一個MessageQueue铣揉,并且保存了當前的線程信息。

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

...

//在ThreadLocal中的get方法

public T get() {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null) {
        ThreadLocalMap.Entry e = map.getEntry(this);
        if (e != null)
            return (T)e.value;
    }
    return setInitialValue();
}

prepare方法在調用時餐曹,首先去ThreadLocal中逛拱,以該線程為key,取該Looper的信息台猴。如果有的話則拋出異常朽合。證明每個線程只允許有一個Looper存在。

如果沒有從ThreadLocal中取出饱狂,則會將該new一個新的Looper存入ThreadLocal中曹步,并且該Looper與當前線程綁定。

prepare方法很簡潔休讳,接下來讓我們看看Looper循環(huán)的主方法:Looper.loop():

 public static void loop() {
    final Looper me = myLooper();
    if (me == null) {
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    final MessageQueue queue = me.mQueue;
    
    ...
    
    for (;;) {
        Message msg = queue.next(); // might block
        if (msg == null) {
            // No message indicates that the message queue is quitting.
            return;
        }

        ...

        try {
            msg.target.dispatchMessage(msg);
        } finally {
            if (traceTag != 0) {
                Trace.traceEnd(traceTag);
            }
        }

        ...

        msg.recycleUnchecked();
    }
}

可以看到首先這里有個死循環(huán)讲婚,防止程序運行結束終止退出。在循環(huán)入口會從MessageQueue中取出一條消息俊柔,就是queue.next()方法筹麸。

如果成功取到消息活合,則會調用msg.target.dispatchMessage(msg)。

這里的msg.target就是handler物赶,稍后會有該部分的源碼白指。

好了,以上就是Looper的主要源碼酵紫,代碼量十分少告嘲,很容易理解。

接下來要看看MessageQueue

2.MessageQueue

剛剛我們看到Looper的loop()方法調用了queue.next()奖地,該段代碼起到阻塞作用橄唬。所以我們從MessageQueue的next()方法開始看起。

Message next() {
    // Return here if the message loop has already quit and been disposed.
    // This can happen if the application tries to restart a looper after quit
    // which is not supported.
    final long ptr = mPtr;
    if (ptr == 0) {
        return null;
    }

    int pendingIdleHandlerCount = -1; // -1 only during first iteration
    int nextPollTimeoutMillis = 0;
    for (;;) {
    
        ...
        
        synchronized (this) {
            // Try to retrieve the next message.  Return if found.
            final long now = SystemClock.uptimeMillis();
            Message prevMsg = null;
            Message msg = mMessages;
            if (msg != null && msg.target == null) {
                // Stalled by a barrier.  Find the next asynchronous message in the queue.
                do {
                    prevMsg = msg;
                    msg = msg.next;
                } while (msg != null && !msg.isAsynchronous());
            }
            
            //重點在這里
            if (msg != null) {
                if (now < msg.when) {
                    // Next message is not ready.  Set a timeout to wake up when it is ready.
                    nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                } else {
                    // Got a message.
                    mBlocked = false;
                    if (prevMsg != null) {
                        prevMsg.next = msg.next;
                    } else {
                        mMessages = msg.next;
                    }
                    msg.next = null;
                    if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                    msg.markInUse();
                    return msg;
                }
            } else {
                // No more messages.
                nextPollTimeoutMillis = -1;
            }

           ...
        }

       ...
    }
}

這段代碼信息量比較大鹉动,不過拆分來看轧坎,我們可以確定MessageQueue.next()方法調用的恰恰是Message的next變量賦值,Message是一個單鏈表結構泽示,其中next屬性指向下一個Message的地址缸血。

由此得知,當mMessages有值的時候械筛,Looper才會調用到msg.target.dispatchMessage()捎泻。

那么什么時候對mMessages進行賦值呢?

答案就在下一個非常重要的方法里:enqueueMessage()

boolean enqueueMessage(Message msg, long when) {
    
    ...

    synchronized (this) {
    
       ...

        msg.markInUse();
        msg.when = when;
        Message p = mMessages;
        boolean needWake;
        //重點在這里
        if (p == null || when == 0 || when < p.when) {
            // New head, wake up the event queue if blocked.
            msg.next = p;
            mMessages = msg;
            needWake = mBlocked;
        } else {
            // Inserted within the middle of the queue.  Usually we don't have to wake
            // up the event queue unless there is a barrier at the head of the queue
            // and the message is the earliest asynchronous message in the queue.
            needWake = mBlocked && p.target == null && msg.isAsynchronous();
            Message prev;
            for (;;) {
                prev = p;
                p = p.next;
                if (p == null || when < p.when) {
                    break;
                }
                if (needWake && p.isAsynchronous()) {
                    needWake = false;
                }
            }
            msg.next = p; // invariant: p == prev.next
            prev.next = msg;
        }

        // We can assume mPtr != 0 because mQuitting is false.
        if (needWake) {
            nativeWake(mPtr);
        }
    }
    return true;
}

Message由enqueueMessage()方法傳入埋哟,在其內部對mMessages進行賦值笆豁。進而轉入next()方法跳出循環(huán),并通知Looper()調用msg.target.dispatchMessage()赤赊。

多說一嘴闯狱,大家可能對這里面這個msg.when參數(shù)比較好奇,這個when參數(shù)就是我們再調用Handler.sendMessageDelay()方法時抛计,傳入的delay時長哄孤。具體時間循環(huán)計算方法可見next(),這里不做過多解釋吹截。

貌似整體流程清晰了瘦陈,誰調用這個MessageQueue的enqueueMessage方法,誰就能將一條Message發(fā)送給Looper波俄,進而發(fā)送給Handler的dispatch()方法晨逝。

3.Handler

答案當然是Handler調用的MessageQueue的enqueueMessage()方法。

 public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
    if (delayMillis < 0) {
        delayMillis = 0;
    }
    return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}

這段代碼僅僅是舉個例子懦铺,所有的Handler的post方法以及sendMessage方法捉貌,最終都會調用到sendMessageAtTime()。

接下來看看sendMessageAtTime():

 public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
    MessageQueue queue = mQueue;
    if (queue == null) {
        RuntimeException e = new RuntimeException(
                this + " sendMessageAtTime() called with no mQueue");
        Log.w("Looper", e.getMessage(), e);
        return false;
    }
    return enqueueMessage(queue, msg, uptimeMillis);
}

sendeMessageAtTime()最終會調用Handler的enqueueMessage()。

結果可想而知昏翰,Hanlder的enqueueMessage()必然會調用到MessageQueue的enqueueMessage()方法:

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
    msg.target = this;
    if (mAsynchronous) {
        msg.setAsynchronous(true);
    }
    return queue.enqueueMessage(msg, uptimeMillis);
}

果然苍匆,最后我們看看Handler的dispathMessage()方法:

 public void dispatchMessage(Message msg) {
    if (msg.callback != null) {
        handleCallback(msg);
    } else {
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        handleMessage(msg);
    }
}

整個一條閉環(huán)流程清晰可見,最終msg通過了dispatchMessage()的分發(fā)棚菊,來到了我們常見的hanldeMessage()里面。

最后為了方便大家理解叔汁,我用一張圖來演示一下整個調用流程:

image.png

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末统求,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子据块,更是在濱河造成了極大的恐慌码邻,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件另假,死亡現(xiàn)場離奇詭異像屋,居然都是意外死亡,警方通過查閱死者的電腦和手機边篮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門己莺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人戈轿,你說我怎么就攤上這事凌受。” “怎么了思杯?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵胜蛉,是天一觀的道長。 經(jīng)常有香客問我色乾,道長誊册,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任暖璧,我火速辦了婚禮案怯,結果婚禮上,老公的妹妹穿的比我還像新娘漆撞。我一直安慰自己殴泰,他們只是感情好,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布浮驳。 她就那樣靜靜地躺著悍汛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪至会。 梳的紋絲不亂的頭發(fā)上离咐,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機與錄音,去河邊找鬼宵蛀。 笑死昆著,一個胖子當著我的面吹牛,可吹牛的內容都是我干的术陶。 我是一名探鬼主播凑懂,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼梧宫!你這毒婦竟也來了接谨?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤塘匣,失蹤者是張志新(化名)和其女友劉穎脓豪,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體忌卤,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡扫夜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了驰徊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片笤闯。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖辣垒,靈堂內的尸體忽然破棺而出望侈,到底是詐尸還是另有隱情,我是刑警寧澤勋桶,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布脱衙,位于F島的核電站,受9級特大地震影響例驹,放射性物質發(fā)生泄漏捐韩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一鹃锈、第九天 我趴在偏房一處隱蔽的房頂上張望荤胁。 院中可真熱鬧,春花似錦屎债、人聲如沸仅政。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽圆丹。三九已至,卻和暖如春躯喇,著一層夾襖步出監(jiān)牢的瞬間辫封,已是汗流浹背硝枉。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留倦微,地道東北人妻味。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像欣福,于是被迫代替她去往敵國和親责球。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內容