從源碼角度分析Android的消息機制
說到Android的消息機制,那么主要的就是指的Handler的運行機制。其中包括MessageQueue以及Looper的工作過程。
在開始正文之前,先拋出兩個問題:
為什么更新UI的操作要在主線程中進行? Android中為什么主線程不會因為Looper.loop()里的死循環卡死?UI線程的判斷是在ViewRootImpl中的checkThread方法中完成的。
對于第一個問題,這里給一個簡單的回答:
如果可以在子線程中修改UI,多線程的并發訪問可能會導致UI控件的不可預期性,采用加鎖的方式,就會降低UI的訪問效率以及會阻塞其他線程的執行,所以最簡單有效的方法就是采用單線程模型來處理UI操作。
Handler的運行離不來底層的MessageQueue和Looper的支撐。MessageQueue翻譯過來是一個消息隊列,里面存儲了Handler需要的Message,MessageQueue并不是一個隊列,其實上是用單鏈表的數據結構來存儲Message。
那么Handler如何拿到Message呢?這時候就需要Looper了,Looper通過Looper.loop()來開啟一個死循環,不斷從MessageQueue中取消息然后傳遞給Handler。
這里還有另一個知識點就是Looper的獲取,這里就要提高一個存儲類:ThreadLocal
ThreadLocal的工作原理ThreadLocal是線程內部的一個數據存儲類,可以存儲某個線程中的數據,對于其他線程無法獲取該線程的數據。我們通過原理來看一下,這個觀點是否正確。
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方法就是在當前線程中所做的操作,ThreadLocalMap內部是一個數組table。 這樣就保證了在不同線程中的數據互不干擾。
ThreadLocal除了使用在Handler中獲取Looper,還用于一些復雜的場景,比如:監聽器的傳遞。
我們簡單了解了ThreadLocal,那么我們從New Handler()來一步步梳理下消息機制。
Looper的工作原理// Handler.java public Handler() { this(null, false); } // callback 消息回調;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; }
我們平常用的是無參數的方法,它傳入的是空的回調以及false。
public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
這里就出現了我們之前說的ThreadLoacal類,那么looper值是什么時候設置進去的呢?
它的設置方法其實是在prepare方法以及prepareMainLooper方法中,我們來分別來看下:
public static void prepare() { prepare(true); } private static void prepare(boolean quitAllowed) { // 在創建looper之前,判斷looper是否與threadloacal綁定過,這也是prepare只能設置一遍的原因。 if (sThreadLocal.get() != null) { throw new RuntimeException('Only one Looper may be created per thread'); } sThreadLocal.set(new Looper(quitAllowed)); } public static void prepareMainLooper() { // 這里其實還是調用的prepare方法 prepare(false); synchronized (Looper.class) { if (sMainLooper != null) { throw new IllegalStateException('The main Looper has already been prepared.'); } sMainLooper = myLooper(); } }
通過上面可以prepare方法只能設置一遍,那么我們在主線程中為什么能直接使用呢? app程序的入口是在ActivityThread中的main方法中:
public static void main(String[] args) { ... //1. 初始化Looper對象 Looper.prepareMainLooper(); // 2. 開啟無限循環 Looper.loop(); throw new RuntimeException('Main thread loop unexpectedly exited'); }
看到了吧,初始化在這里,那么我們再來看下looper的初始化方法:
private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); }
Looper的初始化做了兩件事:創建消息隊列MessageQueue以及獲取當前的線程。 到這里,我們可以得到一個結論:
prepare方法在一個線程中只能調用一次。 Looper的初始化在一個線程中只能調用一次。 最后可以得知:一個線程對應一個Looper,一個Looper對應一個MessageQueue。Looper可以理解為一個工廠線,不斷從MessageQueue中取Message,工廠線開啟的方式就是Looper.loop()
public static void loop() { final Looper me = myLooper(); // 1. 判斷looper是否存在 if (me == null) { throw new RuntimeException('No Looper; Looper.prepare() wasn’t called on this thread.'); } final MessageQueue queue = me.mQueue; ... //2. 開啟一個死循環 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方法通過開啟一個死循環,不斷從MessageQueue中取Message消息,當message為空時,退出該循環,否則調用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就是我們需要實現的方法。 那么Handler是如何設置到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) { // 關鍵代碼來了! msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
可以看到,通過一系列的方法,在enqueueMessage中將handler賦值到msg的target中。最后調用的是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是否存在以及是否在使用中。然后根據時間順序插入MessageQueue中。
到這里基本的流程已經梳理完了,回到起初我們的問題:Looper.loop()是一個死循環,為什么不會堵塞主線程呢?
我們來看下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 方法,當調用此 native 方法時,主線程會釋放 CPU 資源進入休眠狀態,直到下條消息到達或者有事務發生,通過往 pipe 管道寫端寫入數據來喚醒主線程工作,這里采用的 epoll 機制。關于 nativePollOnce 的詳細分析可以參考:nativePollOnce函數分析
總結 app程序啟動從ActivityThread中的main方法中開始,通過Looper.prepare()進行Looper以及MessageQueue的創建以及ThreadLocal與線程之間的綁定。 我們在創建Handler時,通過ThreadLocal來獲取該線程中的Looper以及在Looper上綁定的MessageQueue。 通過Handler.sendMessage()方法來將msg與Handler之間進行綁定,然后將msg通過時間順序插入MessageQueue中。 主線程創建后,Looper.loop()來啟動一個(不占用資源)死循環,從Looper已經存在的MessageQueue中不斷取出Message,然后調用不為空的Message綁定的Handler的dispatchMessage(msg)方法,最后會調用我們復寫的handlerMessage方法中。參考資料Androi開發藝術探索
以上就是從源碼角度分析Android的消息機制的詳細內容,更多關于Android 消息機制的資料請關注好吧啦網其它相關文章!
相關文章: