從源碼角度分析Android的消息機(jī)制
前言
說到Android的消息機(jī)制,那么主要的就是指的Handler的運(yùn)行機(jī)制。其中包括MessageQueue以及Looper的工作過程。
在開始正文之前,先拋出兩個(gè)問題:
- 為什么更新UI的操作要在主線程中進(jìn)行?
- Android中為什么主線程不會(huì)因?yàn)長ooper.loop()里的死循環(huán)卡死?
UI線程的判斷是在ViewRootImpl中的checkThread方法中完成的。
對(duì)于第一個(gè)問題,這里給一個(gè)簡單的回答:
如果可以在子線程中修改UI,多線程的并發(fā)訪問可能會(huì)導(dǎo)致UI控件的不可預(yù)期性,采用加鎖的方式,就會(huì)降低UI的訪問效率以及會(huì)阻塞其他線程的執(zhí)行,所以最簡單有效的方法就是采用單線程模型來處理UI操作。
Handler的運(yùn)行離不來底層的MessageQueue和Looper的支撐。MessageQueue翻譯過來是一個(gè)消息隊(duì)列,里面存儲(chǔ)了Handler需要的Message,MessageQueue并不是一個(gè)隊(duì)列,其實(shí)上是用單鏈表的數(shù)據(jù)結(jié)構(gòu)來存儲(chǔ)Message。
那么Handler如何拿到Message呢?這時(shí)候就需要Looper了,Looper通過Looper.loop()來開啟一個(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ì)于其他線程無法獲取該線程的數(shù)據(jù)。我們通過原理來看一下,這個(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)聽器的傳遞。
我們簡單了解了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í)候設(shè)置進(jìn)去的呢?
它的設(shè)置方法其實(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() { // 這里其實(shí)還是調(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è)置一遍,那么我們?cè)谥骶€程中為什么能直接使用呢? app程序的入口是在ActivityThread中的main方法中:
public static void main(String[] args) { ... //1. 初始化Looper對(duì)象 Looper.prepareMainLooper(); // 2. 開啟無限循環(huán) Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }
看到了吧,初始化在這里,那么我們?cè)賮砜聪耹ooper的初始化方法:
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,工廠線開啟的方式就是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. 開啟一個(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方法通過開啟一個(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中的呢?我們來看下我們熟知的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ù)時(shí)間順序插入MessageQueue中。
到這里基本的流程已經(jīng)梳理完了,回到起初我們的問題:Looper.loop()是一個(gè)死循環(huán),為什么不會(huì)堵塞主線程呢?
我們來看下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ā)生,通過往 pipe 管道寫端寫入數(shù)據(jù)來喚醒主線程工作,這里采用的 epoll 機(jī)制。關(guān)于 nativePollOnce 的詳細(xì)分析可以參考:nativePollOnce函數(shù)分析
總結(jié)
- app程序啟動(dòng)從ActivityThread中的main方法中開始,通過Looper.prepare()進(jìn)行Looper以及MessageQueue的創(chuàng)建以及ThreadLocal與線程之間的綁定。
- 我們?cè)趧?chuàng)建Handler時(shí),通過ThreadLocal來獲取該線程中的Looper以及在Looper上綁定的MessageQueue。
- 通過Handler.sendMessage()方法來將msg與Handler之間進(jìn)行綁定,然后將msg通過時(shí)間順序插入MessageQueue中。
- 主線程創(chuàng)建后,Looper.loop()來啟動(dòng)一個(gè)(不占用資源)死循環(huán),從Looper已經(jīng)存在的MessageQueue中不斷取出Message,然后調(diào)用不為空的Message綁定的Handler的dispatchMessage(msg)方法,最后會(huì)調(diào)用我們復(fù)寫的handlerMessage方法中。
參考資料
以上就是從源碼角度分析Android的消息機(jī)制的詳細(xì)內(nèi)容,更多關(guān)于Android 消息機(jī)制的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 詳解Android系統(tǒng)啟動(dòng)過程
- Android 如何獲取設(shè)備唯一標(biāo)識(shí)
- Android LiveData使用需要注意的地方
- Android nativePollOnce函數(shù)解析
- Android端代碼量非常小的分頁加載庫
- Android如何使用Glide加載清晰長圖
- Android如何實(shí)現(xiàn)動(dòng)態(tài)滾動(dòng)波形圖(心電圖)功能
- Android使用 Coroutine + Retrofit打造簡單的HTTP請(qǐng)求庫
- Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫
- Android 側(cè)滑抽屜菜單的實(shí)現(xiàn)代碼
- Android AMS啟動(dòng)詳解
相關(guān)文章
Android檢測(cè)Cursor泄漏的原理以及使用方法
本文介紹如何在 Android 檢測(cè) Cursor 泄漏的原理以及使用方法,還指出幾種常見的出錯(cuò)示例,同時(shí)該方法同樣適合于其他需要檢測(cè)資源泄露的情況,感興趣的朋友可以了解下2013-01-01Android實(shí)現(xiàn)多次閃退清除數(shù)據(jù)
這篇文章主要介紹了Android實(shí)現(xiàn)多次閃退清除數(shù)據(jù)的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-04-04Kotlin Flow常見場(chǎng)景下的使用實(shí)例
這篇文章主要為大家介紹了Kotlin Flow常見場(chǎng)景下的使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Kotlin+buildSrc更好的管理Gradle依賴譯文
這篇文章主要為大家介紹了Kotlin+buildSrc更好的管理Gradle依賴譯文及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06RecyclerView實(shí)現(xiàn)仿支付寶應(yīng)用管理
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)仿支付寶應(yīng)用管理的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04android GridView多選效果的實(shí)例代碼
在使用 GridView的時(shí)候,有時(shí)需要多選上面顯示的類容,比如批量刪除上面顯示的圖片,批量上傳圖片等。這個(gè)時(shí)候我們可以使用層疊圖來實(shí)現(xiàn),效果如下:2013-06-06Flutter 全局點(diǎn)擊空白處隱藏鍵盤實(shí)戰(zhàn)
這篇文章主要介紹了Flutter 全局點(diǎn)擊空白處隱藏鍵盤實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android自定義view仿淘寶快遞物流信息時(shí)間軸
這篇文章主要為大家詳細(xì)介紹了Android自定義view仿淘寶快遞物流信息時(shí)間軸,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android入門之在Activity之間穿梭的Intent
Intent可以用來啟動(dòng)Activity(startActivity(Intent))、Serveice(startService(Intent))等組件,可以用來綁定Activity和Service以建立它們之間的通信(bindServiceConnaction(Intent,ServiceConnection,int)),可以作為Broadcast Intent發(fā)送給廣播接收器2021-10-10