從源碼角度分析Android的消息機(jī)制
前言
說到Android的消息機(jī)制,那么主要的就是指的Handler的運行機(jī)制。其中包括MessageQueue以及Looper的工作過程。
在開始正文之前,先拋出兩個問題:
- 為什么更新UI的操作要在主線程中進(jìn)行?
- Android中為什么主線程不會因為Looper.loop()里的死循環(huán)卡死?
UI線程的判斷是在ViewRootImpl中的checkThread方法中完成的。
對于第一個問題,這里給一個簡單的回答:
如果可以在子線程中修改UI,多線程的并發(fā)訪問可能會導(dǎo)致UI控件的不可預(yù)期性,采用加鎖的方式,就會降低UI的訪問效率以及會阻塞其他線程的執(zhí)行,所以最簡單有效的方法就是采用單線程模型來處理UI操作。
Handler的運行離不來底層的MessageQueue和Looper的支撐。MessageQueue翻譯過來是一個消息隊列,里面存儲了Handler需要的Message,MessageQueue并不是一個隊列,其實上是用單鏈表的數(shù)據(jù)結(jié)構(gòu)來存儲Message。
那么Handler如何拿到Message呢?這時候就需要Looper了,Looper通過Looper.loop()來開啟一個死循環(huán),不斷從MessageQueue中取消息然后傳遞給Handler。
這里還有另一個知識點就是Looper的獲取,這里就要提高一個存儲類:ThreadLocal
ThreadLocal的工作原理
ThreadLocal是線程內(nèi)部的一個數(shù)據(jù)存儲類,可以存儲某個線程中的數(shù)據(jù),對于其他線程無法獲取該線程的數(shù)據(jù)。我們通過原理來看一下,這個觀點是否正確。
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)部是一個數(shù)組table。 這樣就保證了在不同線程中的數(shù)據(jù)互不干擾。
ThreadLocal除了使用在Handler中獲取Looper,還用于一些復(fù)雜的場景,比如:監(jiān)聽器的傳遞。
我們簡單了解了ThreadLocal,那么我們從New Handler()來一步步梳理下消息機(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; }
我們平常用的是無參數(shù)的方法,它傳入的是空的回調(diào)以及false。
public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
這里就出現(xiàn)了我們之前說的ThreadLoacal類,那么looper值是什么時候設(shè)置進(jìn)去的呢?
它的設(shè)置方法其實是在prepare方法以及prepareMainLooper方法中,我們來分別來看下:
public static void prepare() { prepare(true); } private static void prepare(boolean quitAllowed) { // 在創(chuàng)建looper之前,判斷l(xiāng)ooper是否與threadloacal綁定過,這也是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() { // 這里其實還是調(diào)用的prepare方法 prepare(false); synchronized (Looper.class) { if (sMainLooper != null) { throw new IllegalStateException("The main Looper has already been prepared."); } sMainLooper = myLooper(); } }
通過上面可以prepare方法只能設(shè)置一遍,那么我們在主線程中為什么能直接使用呢? app程序的入口是在ActivityThread中的main方法中:
public static void main(String[] args) { ... //1. 初始化Looper對象 Looper.prepareMainLooper(); // 2. 開啟無限循環(huán) Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }
看到了吧,初始化在這里,那么我們再來看下looper的初始化方法:
private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); }
Looper的初始化做了兩件事:創(chuàng)建消息隊列MessageQueue以及獲取當(dāng)前的線程。 到這里,我們可以得到一個結(jié)論:
- prepare方法在一個線程中只能調(diào)用一次。
- Looper的初始化在一個線程中只能調(diào)用一次。
- 最后可以得知:一個線程對應(yīng)一個Looper,一個Looper對應(yīng)一個MessageQueue。
Looper可以理解為一個工廠線,不斷從MessageQueue中取Message,工廠線開啟的方式就是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. 開啟一個死循環(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方法通過開啟一個死循環(huán),不斷從MessageQueue中取Message消息,當(dāng)message為空時,退出該循環(huán),否則調(diào)用msg.target.dispatchMessage(msg)方法,target就是msg綁定的Handler對象。
Handler的工作原理
好了到這里又回到了Handler類中。
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
這個handleMessage就是我們需要實現(xiàn)的方法。 那么Handler是如何設(shè)置到Message中的呢?我們來看下我們熟知的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)鍵代碼來了! msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
可以看到,通過一系列的方法,在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ù)時間順序插入MessageQueue中。
到這里基本的流程已經(jīng)梳理完了,回到起初我們的問題:Looper.loop()是一個死循環(huán),為什么不會堵塞主線程呢?
我們來看下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方法是一個 native 方法,當(dāng)調(diào)用此 native 方法時,主線程會釋放 CPU 資源進(jìn)入休眠狀態(tài),直到下條消息到達(dá)或者有事務(wù)發(fā)生,通過往 pipe 管道寫端寫入數(shù)據(jù)來喚醒主線程工作,這里采用的 epoll 機(jī)制。關(guān)于 nativePollOnce 的詳細(xì)分析可以參考:nativePollOnce函數(shù)分析
總結(jié)
- app程序啟動從ActivityThread中的main方法中開始,通過Looper.prepare()進(jìn)行Looper以及MessageQueue的創(chuàng)建以及ThreadLocal與線程之間的綁定。
- 我們在創(chuàng)建Handler時,通過ThreadLocal來獲取該線程中的Looper以及在Looper上綁定的MessageQueue。
- 通過Handler.sendMessage()方法來將msg與Handler之間進(jìn)行綁定,然后將msg通過時間順序插入MessageQueue中。
- 主線程創(chuàng)建后,Looper.loop()來啟動一個(不占用資源)死循環(huán),從Looper已經(jīng)存在的MessageQueue中不斷取出Message,然后調(diào)用不為空的Message綁定的Handler的dispatchMessage(msg)方法,最后會調(diào)用我們復(fù)寫的handlerMessage方法中。
參考資料
以上就是從源碼角度分析Android的消息機(jī)制的詳細(xì)內(nèi)容,更多關(guān)于Android 消息機(jī)制的資料請關(guān)注腳本之家其它相關(guān)文章!
- 詳解Android系統(tǒng)啟動過程
- Android 如何獲取設(shè)備唯一標(biāo)識
- Android LiveData使用需要注意的地方
- Android nativePollOnce函數(shù)解析
- Android端代碼量非常小的分頁加載庫
- Android如何使用Glide加載清晰長圖
- Android如何實現(xiàn)動態(tài)滾動波形圖(心電圖)功能
- Android使用 Coroutine + Retrofit打造簡單的HTTP請求庫
- Kotlin + Flow 實現(xiàn)Android 應(yīng)用初始化任務(wù)啟動庫
- Android 側(cè)滑抽屜菜單的實現(xiàn)代碼
- Android AMS啟動詳解
相關(guān)文章
Android實現(xiàn)多次閃退清除數(shù)據(jù)
這篇文章主要介紹了Android實現(xiàn)多次閃退清除數(shù)據(jù)的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-04-04Kotlin+buildSrc更好的管理Gradle依賴譯文
這篇文章主要為大家介紹了Kotlin+buildSrc更好的管理Gradle依賴譯文及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06RecyclerView實現(xiàn)仿支付寶應(yīng)用管理
這篇文章主要為大家詳細(xì)介紹了RecyclerView實現(xiàn)仿支付寶應(yīng)用管理的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Android入門之在Activity之間穿梭的Intent
Intent可以用來啟動Activity(startActivity(Intent))、Serveice(startService(Intent))等組件,可以用來綁定Activity和Service以建立它們之間的通信(bindServiceConnaction(Intent,ServiceConnection,int)),可以作為Broadcast Intent發(fā)送給廣播接收器2021-10-10