從源碼角度分析Android的消息機(jī)制
說(shuō)到Android的消息機(jī)制,那么主要的就是指的Handler的運(yùn)行機(jī)制。其中包括MessageQueue以及Looper的工作過(guò)程。
在開(kāi)始正文之前,先拋出兩個(gè)問(wèn)題:
為什么更新UI的操作要在主線程中進(jìn)行? Android中為什么主線程不會(huì)因?yàn)長(zhǎng)ooper.loop()里的死循環(huán)卡死?UI線程的判斷是在ViewRootImpl中的checkThread方法中完成的。
對(duì)于第一個(gè)問(wèn)題,這里給一個(gè)簡(jiǎn)單的回答:
如果可以在子線程中修改UI,多線程的并發(fā)訪問(wèn)可能會(huì)導(dǎo)致UI控件的不可預(yù)期性,采用加鎖的方式,就會(huì)降低UI的訪問(wèn)效率以及會(huì)阻塞其他線程的執(zhí)行,所以最簡(jiǎn)單有效的方法就是采用單線程模型來(lái)處理UI操作。
Handler的運(yùn)行離不來(lái)底層的MessageQueue和Looper的支撐。MessageQueue翻譯過(guò)來(lái)是一個(gè)消息隊(duì)列,里面存儲(chǔ)了Handler需要的Message,MessageQueue并不是一個(gè)隊(duì)列,其實(shí)上是用單鏈表的數(shù)據(jù)結(jié)構(gòu)來(lái)存儲(chǔ)Message。
那么Handler如何拿到Message呢?這時(shí)候就需要Looper了,Looper通過(guò)Looper.loop()來(lái)開(kāi)啟一個(gè)死循環(huán),不斷從MessageQueue中取消息然后傳遞給Handler。
這里還有另一個(gè)知識(shí)點(diǎn)就是Looper的獲取,這里就要提高一個(gè)存儲(chǔ)類:ThreadLocal
ThreadLocal的工作原理ThreadLocal是線程內(nèi)部的一個(gè)數(shù)據(jù)存儲(chǔ)類,可以存儲(chǔ)某個(gè)線程中的數(shù)據(jù),對(duì)于其他線程無(wú)法獲取該線程的數(shù)據(jù)。我們通過(guò)原理來(lái)看一下,這個(gè)觀點(diǎn)是否正確。
public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); } 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(); }
可以看出它的set和get方法就是在當(dāng)前線程中所做的操作,ThreadLocalMap內(nèi)部是一個(gè)數(shù)組table。 這樣就保證了在不同線程中的數(shù)據(jù)互不干擾。
ThreadLocal除了使用在Handler中獲取Looper,還用于一些復(fù)雜的場(chǎng)景,比如:監(jiān)聽(tīng)器的傳遞。
我們簡(jiǎn)單了解了ThreadLocal,那么我們從New Handler()來(lái)一步步梳理下消息機(jī)制。
Looper的工作原理// Handler.java public Handler() { this(null, false); } // callback 消息回調(diào);async 是否同步 public Handler(Callback callback, boolean async) { ... // 1. 首先獲取looper mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( 'Can’t create handler inside thread ' + Thread.currentThread() + ' that has not called Looper.prepare()'); } // 2. 獲取MessggeQueue mQueue = mLooper.mQueue; mCallback = callback; mAsynchronous = async; }
我們平常用的是無(wú)參數(shù)的方法,它傳入的是空的回調(diào)以及false。
public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
這里就出現(xiàn)了我們之前說(shuō)的ThreadLoacal類,那么looper值是什么時(shí)候設(shè)置進(jìn)去的呢?
它的設(shè)置方法其實(shí)是在prepare方法以及prepareMainLooper方法中,我們來(lái)分別來(lái)看下:
public static void prepare() { prepare(true); } private static void prepare(boolean quitAllowed) { // 在創(chuàng)建looper之前,判斷l(xiāng)ooper是否與threadloacal綁定過(guò),這也是prepare只能設(shè)置一遍的原因。 if (sThreadLocal.get() != null) { throw new RuntimeException('Only one Looper may be created per thread'); } sThreadLocal.set(new Looper(quitAllowed)); } public static void prepareMainLooper() { // 這里其實(shí)還是調(diào)用的prepare方法 prepare(false); synchronized (Looper.class) { if (sMainLooper != null) { throw new IllegalStateException('The main Looper has already been prepared.'); } sMainLooper = myLooper(); } }
通過(guò)上面可以prepare方法只能設(shè)置一遍,那么我們?cè)谥骶€程中為什么能直接使用呢? app程序的入口是在ActivityThread中的main方法中:
public static void main(String[] args) { ... //1. 初始化Looper對(duì)象 Looper.prepareMainLooper(); // 2. 開(kāi)啟無(wú)限循環(huán) Looper.loop(); throw new RuntimeException('Main thread loop unexpectedly exited'); }
看到了吧,初始化在這里,那么我們?cè)賮?lái)看下looper的初始化方法:
private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); }
Looper的初始化做了兩件事:創(chuàng)建消息隊(duì)列MessageQueue以及獲取當(dāng)前的線程。 到這里,我們可以得到一個(gè)結(jié)論:
prepare方法在一個(gè)線程中只能調(diào)用一次。 Looper的初始化在一個(gè)線程中只能調(diào)用一次。 最后可以得知:一個(gè)線程對(duì)應(yīng)一個(gè)Looper,一個(gè)Looper對(duì)應(yīng)一個(gè)MessageQueue。Looper可以理解為一個(gè)工廠線,不斷從MessageQueue中取Message,工廠線開(kāi)啟的方式就是Looper.loop()
public static void loop() { final Looper me = myLooper(); // 1. 判斷l(xiāng)ooper是否存在 if (me == null) { throw new RuntimeException('No Looper; Looper.prepare() wasn’t called on this thread.'); } final MessageQueue queue = me.mQueue; ... //2. 開(kāi)啟一個(gè)死循環(huán) 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); dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0; } finally { if (traceTag != 0) { Trace.traceEnd(traceTag); } } ... } }
looper方法通過(guò)開(kāi)啟一個(gè)死循環(huán),不斷從MessageQueue中取Message消息,當(dāng)message為空時(shí),退出該循環(huán),否則調(diào)用msg.target.dispatchMessage(msg)方法,target就是msg綁定的Handler對(duì)象。
Handler的工作原理好了到這里又回到了Handler類中。
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
這個(gè)handleMessage就是我們需要實(shí)現(xiàn)的方法。 那么Handler是如何設(shè)置到Message中的呢?我們來(lái)看下我們熟知的sendMessage方法:
public final boolean sendMessage(Message msg) { return sendMessageDelayed(msg, 0); } public final boolean sendMessageDelayed(Message msg, long delayMillis) { if (delayMillis < 0) { delayMillis = 0; } return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); } public boolean sendMessageAtTime(Message msg, long uptimeMillis) { MessageQueue queue = mQueue; ... return enqueueMessage(queue, msg, uptimeMillis); } private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { // 關(guān)鍵代碼來(lái)了! msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
可以看到,通過(guò)一系列的方法,在enqueueMessage中將handler賦值到msg的target中。最后調(diào)用的是MessageQueue的enqueueMessage方法中:
boolean enqueueMessage(Message msg, long when) { if (msg.target == null) { throw new IllegalArgumentException('Message must have a target.'); } if (msg.isInUse()) { throw new IllegalStateException(msg + ' This message is already in use.'); } synchronized (this) { if (mQuitting) { IllegalStateException e = new IllegalStateException( msg.target + ' sending message to a Handler on a dead thread'); Log.w(TAG, e.getMessage(), e); msg.recycle(); return false; } 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 { 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; }
enqueueMessage方法主要做了兩件事:
首先判斷handler是否存在以及是否在使用中。然后根據(jù)時(shí)間順序插入MessageQueue中。
到這里基本的流程已經(jīng)梳理完了,回到起初我們的問(wèn)題:Looper.loop()是一個(gè)死循環(huán),為什么不會(huì)堵塞主線程呢?
我們來(lái)看下MessageQueue的next方法:
Message next() { final long ptr = mPtr; if (ptr == 0) { return null; } int pendingIdleHandlerCount = -1; // -1 only during first iteration int nextPollTimeoutMillis = 0; for (;;) { nativePollOnce(ptr, nextPollTimeoutMillis); ... } }
nativePollOnce方法是一個(gè) native 方法,當(dāng)調(diào)用此 native 方法時(shí),主線程會(huì)釋放 CPU 資源進(jìn)入休眠狀態(tài),直到下條消息到達(dá)或者有事務(wù)發(fā)生,通過(guò)往 pipe 管道寫(xiě)端寫(xiě)入數(shù)據(jù)來(lái)喚醒主線程工作,這里采用的 epoll 機(jī)制。關(guān)于 nativePollOnce 的詳細(xì)分析可以參考:nativePollOnce函數(shù)分析
總結(jié) app程序啟動(dòng)從ActivityThread中的main方法中開(kāi)始,通過(guò)Looper.prepare()進(jìn)行Looper以及MessageQueue的創(chuàng)建以及ThreadLocal與線程之間的綁定。 我們?cè)趧?chuàng)建Handler時(shí),通過(guò)ThreadLocal來(lái)獲取該線程中的Looper以及在Looper上綁定的MessageQueue。 通過(guò)Handler.sendMessage()方法來(lái)將msg與Handler之間進(jìn)行綁定,然后將msg通過(guò)時(shí)間順序插入MessageQueue中。 主線程創(chuàng)建后,Looper.loop()來(lái)啟動(dòng)一個(gè)(不占用資源)死循環(huán),從Looper已經(jīng)存在的MessageQueue中不斷取出Message,然后調(diào)用不為空的Message綁定的Handler的dispatchMessage(msg)方法,最后會(huì)調(diào)用我們復(fù)寫(xiě)的handlerMessage方法中。參考資料Androi開(kāi)發(fā)藝術(shù)探索
以上就是從源碼角度分析Android的消息機(jī)制的詳細(xì)內(nèi)容,更多關(guān)于Android 消息機(jī)制的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python軟件測(cè)試Jmeter性能測(cè)試JDBC Request(結(jié)合數(shù)據(jù)庫(kù))的使用詳解2. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法3. AJAX實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果4. ASP.NET MVC視圖頁(yè)使用jQuery傳遞異步數(shù)據(jù)的幾種方式詳解5. python用700行代碼實(shí)現(xiàn)http客戶端6. react axios 跨域訪問(wèn)一個(gè)或多個(gè)域名問(wèn)題7. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容8. 詳解php如何合并身份證正反面圖片為一張圖片9. XHTML 1.0:標(biāo)記新的開(kāi)端10. 如何基于Python和Flask編寫(xiě)Prometheus監(jiān)控
