use for
相信于此,絕大多數(shù)同學(xué)都會回答消息機(jī)制是android 為了線程間通信而引入的工具??梢暂p松的將一個任務(wù)切換到handler所在線程執(zhí)行。android開發(fā)規(guī)范有規(guī)定,不允許于子線程更新ui,這樣會觸發(fā)異常;我們平時(shí)使用handler主要都是將子線程切換到主線程中去執(zhí)行;因此從本質(zhì)上來來說,Handler并不是專門用于更新UI的,它只是常被開發(fā)者用來更新UI。
Q?為何不能在主線程外更新ui呢?
A: 因?yàn)锳ndroid的UI線程是非線程安全
的,應(yīng)用更新UI,是調(diào)用invalidate()
方法來實(shí)現(xiàn)界面的重繪,而invalidate()
方法是非線程安全的,也就是說當(dāng)我們在非UI線程來更新UI時(shí),可能會有其他的線程或UI線程也在更新UI,這就會導(dǎo)致界面更新的不同步。因此我們不能在非UI主線程中做更新UI的操作。也就是說我們在使用Android中的線程時(shí),要保證: 更新ui都在UI主線程執(zhí)行.
Q:那為何不將需要更新ui的操作放在UI線程執(zhí)行呢?
A: 我們都知道在java中,線程存在以下幾種基本狀態(tài):創(chuàng)建
,就緒
,運(yùn)行
,阻塞
,死亡
。我們的應(yīng)用啟動后,所有的交互都是在UI
線程完成的;如果在UI
執(zhí)行延時(shí)操作,如常見的網(wǎng)絡(luò)請求
,UI
線程就會進(jìn)入阻塞
狀態(tài);此時(shí)用戶就無法響應(yīng)任何操作了;如果此過程超過5秒,就會讓程序處于ANR(application not response)
,這時(shí)用戶就可能想要和你的應(yīng)用說聲gg
了。
Q:Android提供了哪幾種線程間通信方式?
A: AsyncTask?
,Handler
。為什么AsynTask
打了個?
呢,我們可以簡單看下AsynTask
源碼,他內(nèi)部也是接住handler來進(jìn)行線程間通信的。
Q:MessageQueue存在Targer對象的消息,那和我們正常流程中,由handler傳遞的消息有什么出入呢?
A: 其實(shí)平時(shí)我們使用的Message
,都是通過Handler
發(fā)送的,有一些系統(tǒng)消息,他們會直接通過調(diào)用MessageQueue
發(fā)送一個屏障消息,這類消息沒有Target
,然后配合Handler
發(fā)送異步消息來使用;當(dāng)MessageQueue
讀取到屏障消息后,他們會直接在鏈表中找到最近的異步消息
,直接執(zhí)行。
feature-要素
Message(消息單元)
定義一個可以發(fā)送到Handler
的消息;它定義了消息Id
,兩個額為的int字段和一個額外的object字段
(消息處理對象),它們可以不被初始化;雖然它的構(gòu)造方法是public,但是還是建議我們通過obtain系列函數(shù)進(jìn)行定義。MessageQueue(消息隊(duì)列)
存放所有發(fā)送的消息隊(duì)列,單鏈表結(jié)構(gòu),供Looper從中讀取數(shù)據(jù);延時(shí)消息是怎么存取的,這個很有趣;Looper(消息讀取者)
永動機(jī);其中有個死循環(huán)函數(shù)Loop()
,不斷讀取MessageQueue
中的消息,交給目標(biāo)處理;問題來了,既然是個死循環(huán),那不是始終會阻塞Looper
所在線程嗎。這又是如何解決的。Handler(消息分發(fā)以及處理者)
通過sendMessage
系列函數(shù),會將Message
傳入MessageQueue
中;Looper.loop()
讀取到消息傳遞給Handler
處理。
desc
-
handler
創(chuàng)建前,Looper.loop()
執(zhí)行前;需要保證當(dāng)前線程Looper
有創(chuàng)建,而這個保證即Looper.prepare()
;主線程由于在1ActivityThread1創(chuàng)建時(shí),已經(jīng)做過,所以無需執(zhí)行; -
Looper.loop()
中有一個死循環(huán),所以線程資源不會釋放;在線程運(yùn)行結(jié)束時(shí)調(diào)用MessageQueue
中的quit
函數(shù),我們才能釋放資源; -
Java
中,所有非靜態(tài)成員變量會持有當(dāng)前對象的引用(不然你又是怎么引用外部類的各種成員變量和函數(shù)等);那樣我們在Activity
中通過new Handler()
, 創(chuàng)建的對象會持有當(dāng)前頁面的引用;而我們發(fā)送的每個消息不能保證是立即執(zhí)行,以及迅速執(zhí)行結(jié)束的,handler.sendEmptyMessageDelayed
;消息是會持有handler
做為他的target
,那在這個message
在通過msg.target.dispatchMessage(msg);
會一直被持有;這樣會導(dǎo)致messageQueue->message->handler->activity|fragment
;在頁面被銷毀,聲明周期執(zhí)行到desatory
時(shí),activity
不會得到釋放,從而內(nèi)存泄漏;handler
得到消息處理時(shí),如果當(dāng)前頁面已經(jīng)被銷毀,執(zhí)行Ui
更新,又會導(dǎo)致難以預(yù)料的問題。 - 針對
3
所提的我們可以按以下兩種處理:
1:頁面destory
銷毀時(shí),調(diào)用handler.removeCallbacksAndMessages(null);
2:通過軟引用創(chuàng)建靜態(tài)Handler對象;
流程解析
android handler流程分析晚上有很多資料;我們這兒簡單介紹下:
Looper
,MessageQueue
就緒;調(diào)用Looper.prepare()
,其間會向Looper
靜態(tài)線程變量sThreadLocal
插入一個當(dāng)前線程的Looper
;在調(diào)用Looper
構(gòu)造函數(shù)時(shí),我們會初始化MessageQueue
,并將mThread
設(shè)置為當(dāng)前Thread.currentThread();
-
Looper.prepare()
代碼塊如下:public static void prepare() { prepare(true); } private static void prepare(boolean quitAllowed) { //sThreadLocal->ThreadLocal對象,里面封裝了一個map邏輯,key是線程hash值;static 類變量 if (sThreadLocal.get() != null) {//不允許多次prepare throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed));//設(shè)置當(dāng)前線程的Looper }
Looper
構(gòu)造函數(shù),以及MessageQueue
構(gòu)造函數(shù)如下:private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); //初始化消息池 mThread = Thread.currentThread(); } //是否允許退出 MessageQueue(boolean quitAllowed) { mQuitAllowed = quitAllowed; mPtr = nativeInit(); //線程id }
-
接下來我們看下數(shù)據(jù)插入
sendMessage(Message msg)
sendEmptyMessage(int what)
sendEmptyMessageDelayed(int what, long delayMillis)
sendEmptyMessageAtTime(int what, long uptimeMillis)
sendMessageDelayed(Message msg, long delayMillis)
sendMessageAtTime(Message msg, long uptimeMillis)
sendMessageAtFrontOfQueue(Message msg)
這以上七個方法,可以通過handler向handler所在線程發(fā)送消息;其中
1,2,3,4,5
都是調(diào)用方法6
進(jìn)行執(zhí)行的;其中方法6
中的uptimeMillis
取的是系統(tǒng)非休眠時(shí)間SystemClock.uptimeMillis()
;我們接下來看下
sendMessageAtTime(Message msg, long uptimeMillis)
:public boolean sendMessageAtTime(Message msg, long uptimeMillis) { MessageQueue queue = mQueue; //將線程變量Looper中的queue取出使用 if (queue == null) { //queue判空,其實(shí)創(chuàng)建handler時(shí),也是必須要Looper初始化結(jié)束;queue創(chuàng)建后的 RuntimeException e = new RuntimeException( this + " sendMessageAtTime() called with no mQueue"); Log.w("Looper", e.getMessage(), e); return false; } return enqueueMessage(queue, msg, uptimeMillis);//簡單判斷,交給enqueueMessage函數(shù)執(zhí)行; }
而
7.sendMessageAtFrontOfQueue(Message msg)
調(diào)用的函數(shù)如下:public final boolean sendMessageAtFrontOfQueue(Message msg) { 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, 0);//簡單判斷,交給enqueueMessage函數(shù)執(zhí)行;設(shè)置執(zhí)行時(shí)間0 }
據(jù)此,我們發(fā)現(xiàn)所有消息的發(fā)送都是通過
MessageQueue
的enqueueMessage(Message msg, long when)
方法;
-
我們來看下
enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis)
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; //將msg的target設(shè)置為當(dāng)前handler;這兒可以看出msg和handler是 n:1的關(guān)系 if (mAsynchronous) {//handler是否是異步?默認(rèn)false;將值賦予msg msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis);//可以看到,此處最終調(diào)用了eqeue的enqueueMessage方法 }
那我們看下
enqueueMessage
函數(shù)(handler的消息基本都是通過該函數(shù)放入線程MessageQueue
中):boolean enqueueMessage(Message msg, long when) { if (msg.target == null) {//handler不能為空 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) {//mQuitting;子線程消息池我們在線程即將結(jié)束時(shí),調(diào)用這個mQuitting退出;之后發(fā)送的消息都是不會被收入消息池的;所以如果遇到消息沒有發(fā)送成功,我們可能需要判斷是不是looper已經(jīng)退出了; 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();//標(biāo)志該消息被處理了 msg.when = when;//設(shè)置執(zhí)行的時(shí)間 Message p = mMessages;//鏈表頭消息 boolean needWake;//是否需要喚醒線程 if (p == null || when == 0 || when < p.when) {//表頭無消息||即時(shí)消息||當(dāng)前消息執(zhí)行時(shí)間小于表頭時(shí)間 // New head, wake up the event queue if blocked. msg.next = p; mMessages = msg; //表頭消息替換為放入的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. //入隊(duì)列中,默認(rèn)不喚醒,僅當(dāng)頭部msg是屏障消息,當(dāng)前msg是異步消息 needWake = mBlocked && p.target == null && msg.isAsynchronous(); Message prev; for (;;) { prev = p; p = p.next; //查找到節(jié)點(diǎn) if (p == null || when < p.when) { break; } //需要喚醒&&存在異步? if (needWake && p.isAsynchronous()) { needWake = false; } } //將msg插入對應(yīng)節(jié)點(diǎn) 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; }
-
于此我們所有的消息入棧已經(jīng)看完了;那消息是怎么獲取的呢;
mBlocked
是不是真的代表線程阻塞呢?根據(jù)前面的圖形介紹,我們知道,Looper
中有一個loop
函數(shù),他是一個死循環(huán),負(fù)責(zé)向MessageQueue
讀取數(shù)據(jù),接下來我們來看下這個函數(shù);/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. 在線程結(jié)束時(shí),要調(diào)用looper.quit()退出 */ public static void loop() { final Looper me = myLooper(); //靜態(tài)方法,獲取當(dāng)前線程的Looper;兩個工作線程進(jìn)行通信,需要先在補(bǔ)獲線程調(diào)用prepare(),并在其run()結(jié)束時(shí),調(diào)用quit() if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } final MessageQueue queue = me.mQueue; //獲取mQueue // Make sure the identity of this thread is that of the local process, // and keep track of what that identity token actually is. Binder.clearCallingIdentity(); //清空遠(yuǎn)程調(diào)用端的uid和pid,用當(dāng)前本地進(jìn)程的uid和pid替代 final long ident = Binder.clearCallingIdentity(); // Allow overriding a threshold with a system prop. e.g. // adb shell 'setprop log.looper.1000.main.slow 1 && stop && start' final int thresholdOverride = SystemProperties.getInt("log.looper." + Process.myUid() + "." + Thread.currentThread().getName() + ".slow", 0); boolean slowDeliveryDetected = false; for (;;) { Message msg = queue.next(); // might block 獲取消息可能阻塞線程;我們先分析它,后面的代碼下面分析 …… } }
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) { //在調(diào)用quit結(jié)束loop后,又一次嘗試調(diào)用prepare后,此時(shí)ptr會為0,不支持 return null; } int pendingIdleHandlerCount = -1; // -1 only during first iteration 默認(rèn)pendingIdleHandler為0 int nextPollTimeoutMillis = 0; //需要阻塞時(shí)間,-1表示無限阻塞,直到消息入棧調(diào)用nativeWake喚醒 for (;;) { if (nextPollTimeoutMillis != 0) {//time不為0存在阻塞 Binder.flushPendingCommands(); //native方法,看注釋是配合線程長時(shí)間阻塞使用,用于釋放任何的掛起對象 } nativePollOnce(ptr, nextPollTimeoutMillis);//線程阻塞,time阻塞時(shí)長 //同步鎖 synchronized (this) { // Try to retrieve the next message. Return if found. final long now = SystemClock.uptimeMillis(); Message prevMsg = null; Message msg = mMessages; //當(dāng)前消息的目標(biāo)為屏障消息(消息無target),找尋下一個異步消息執(zhí)行 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) { //when比當(dāng)前時(shí)間大;需要阻塞 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);//計(jì)算消息的延時(shí)時(shí)間 } else { //返回這個將要執(zhí)行的消息;將mBlocked阻塞置false;將當(dāng)前message置為執(zhí)行消息后一個 // 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; //返回消息,退出當(dāng)前循環(huán) } } else { // No more messages.沒有消息;無限阻塞,直到新消息入列喚醒它 nextPollTimeoutMillis = -1; } // Process the quit message now that all pending messages have been handled. if (mQuitting) { //如果執(zhí)行了退出。調(diào)用dispose(); dispose(); return null;//返回null作為next()執(zhí)行結(jié)果,注意,此時(shí)Looper.loop()也會執(zhí)行結(jié)束 } //idleHandlers->idleHandler是指一個線程當(dāng)前沒有需要立即執(zhí)行的消息,(延時(shí)執(zhí)行or無消息)時(shí),會執(zhí)行的一個callback;根據(jù)上面的分析,只有在next()執(zhí)行,且沒有需要返回消息時(shí)執(zhí)行 if (pendingIdleHandlerCount < 0 && (mMessages == null || now < mMessages.when)) {//延時(shí)執(zhí)行or無消息 pendingIdleHandlerCount = mIdleHandlers.size(); //只有調(diào)用addIdleHandler加入idle時(shí),count才會增加 } //默認(rèn)0;無idle時(shí),mBlocked阻塞置為true,執(zhí)行循環(huán) for (;;) {}內(nèi)部內(nèi)容 if (pendingIdleHandlerCount <= 0) { // No idle handlers to run. Loop and wait some more. mBlocked = true; continue; } if (mPendingIdleHandlers == null) { mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)]; } mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); } // Run the idle handlers. // We only ever reach this code block during the first iteration. for (int i = 0; i < pendingIdleHandlerCount; i++) { final IdleHandler idler = mPendingIdleHandlers[i]; mPendingIdleHandlers[i] = null; // release the reference to the handler boolean keep = false; try { keep = idler.queueIdle();//根據(jù)queueIdle返回值,決定是否需要執(zhí)行后移除該idle } catch (Throwable t) { Log.wtf(TAG, "IdleHandler threw exception", t); } if (!keep) { synchronized (this) { mIdleHandlers.remove(idler); } } } //執(zhí)行完P(guān)endingIdleHandler后,我門將count置為0,不再執(zhí)行他 // Reset the idle handler count to 0 so we do not run them again. pendingIdleHandlerCount = 0; //執(zhí)行完idle后,可能有消息準(zhǔn)備就緒,我們重新計(jì)算阻塞時(shí)間 // While calling an idle handler, a new message could have been delivered // so go back and look again for a pending message without waiting. nextPollTimeoutMillis = 0; } }
總結(jié)一下,
MessageQueue
不斷獲取待執(zhí)行消息,并可能阻塞線程(沒有message or 待執(zhí)行message
的when
比當(dāng)前時(shí)間晚);而MessageQueue
提供了一個Idle機(jī)制
,用于在當(dāng)前線程沒有由于沒有待執(zhí)行Message
或者延時(shí)Message
時(shí)執(zhí)行,而addIdleHandler
就是用于添加Idle
的我們再回頭看下
Looper.loop()
:for (;;) { Message msg = queue.next(); // might block 剛剛分析上文,當(dāng)前消息是延時(shí)消息或者消息隊(duì)列為空時(shí),會進(jìn)行阻塞 if (msg == null) { //沒有消息退出循環(huán),loop結(jié)束工作 ; next控制 ;即Mq執(zhí)行quit退出后,不在執(zhí)行任何消息 // No message indicates that the message queue is quitting. return; } // This must be in a local variable, in case a UI event sets the logger //日志輸出;支持自定義 final Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } //Trace用于追蹤一個Message執(zhí)行的;可以結(jié)合TraceView等工具查看,具體請百度吧;而我們的Ui線程所有的ui繪制,事件流執(zhí)行,等都屬于一個消息,可以通過Trace進(jìn)行跟蹤; final long traceTag = me.mTraceTag; long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs; long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs; if (thresholdOverride > 0) { slowDispatchThresholdMs = thresholdOverride; slowDeliveryThresholdMs = thresholdOverride; } final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0); final boolean logSlowDispatch = (slowDispatchThresholdMs > 0); final boolean needStartTime = logSlowDelivery || logSlowDispatch; final boolean needEndTime = logSlowDispatch; if (traceTag != 0 && Trace.isTagEnabled(traceTag)) { Trace.traceBegin(traceTag, msg.target.getTraceName(msg));//跟蹤起始 } final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0; final long dispatchEnd; try { msg.target.dispatchMessage(msg); //分發(fā)消息 dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0; //記錄執(zhí)行結(jié)束時(shí)間 } finally { if (traceTag != 0) { Trace.traceEnd(traceTag);//跟蹤結(jié)束 } } //Slow?沒研究,但也是打印相關(guān)日志信息的。。 if (logSlowDelivery) { if (slowDeliveryDetected) { if ((dispatchStart - msg.when) <= 10) { Slog.w(TAG, "Drained"); slowDeliveryDetected = false; } } else { if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery", msg)) { // Once we write a slow delivery log, suppress until the queue drains. slowDeliveryDetected = true; } } } if (logSlowDispatch) { showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg); } if (logging != null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); //打印結(jié)束時(shí)間 } // Make sure that during the course of dispatching the // identity of the thread wasn't corrupted. final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) { Log.wtf(TAG, "Thread identity changed from 0x" + Long.toHexString(ident) + " to 0x" + Long.toHexString(newIdent) + " while dispatching to " + msg.target.getClass().getName() + " " + msg.callback + " what=" + msg.what); } msg.recycleUnchecked();//msg結(jié)合obtion()實(shí)現(xiàn)對象復(fù)用 }
結(jié)合代碼來看
Looper.loop()
所做的事情不多,主要都是用于記錄分析Message
信息的: 開啟一個死循環(huán),將消息讀取交給
MessageQUeue
的next()
函數(shù),該函數(shù)可能導(dǎo)致線程阻塞;提供一個
Printer
接口,記錄打印每個Message
的執(zhí)行開始和結(jié)束信息;提供的
Trace
函數(shù)用來記錄每個消息的處理信息;通過
msg.target.dispatchMessage(msg)
執(zhí)行消息-
通過
msg.recycleUnchecked()
回收消息,使得Message
消息池得到復(fù)用;Message
是一個鏈表結(jié)構(gòu),提供了Message.obtion()
方法,用于不斷的取鏈表頭對象;在表頭空時(shí)新建;消息執(zhí)行完調(diào)用的recycleUnchecked
會將Message
相關(guān)消息情況,插入鏈表頭至此我們對
Android
的消息機(jī)制發(fā)送和讀取有了一個完整的了解:下面附上一個簡單的流程圖(md的流程圖繪制,真心累啊。)
對于一個消息創(chuàng)建流程,加入消息隊(duì)列,MessageQueue
簡單通過Mq
代表了:
接下來是消息讀取的流程圖:
上面流程圖中有涉及到一個新的消息概念屏障消息(無target的消息)
:
我們向消息隊(duì)列MessageQueue
發(fā)送一個屏障消息,然后再發(fā)送一個異步消息;在我們讀取到這個屏障消息的時(shí)候,我們會找到鏈表后的第一個異步消息;這樣就能快速執(zhí)行該異步消息了;
系統(tǒng)有一個postSyncBarrier()
用來發(fā)送屏障消息,但是被隱藏了;我們可以反射調(diào)用或者直接向MessageQueue
表頭反射插入一個Message
,但是不建議這樣做;
發(fā)送異步消息可以通過:創(chuàng)建Handler
時(shí),傳入異步參數(shù):
public Handler(boolean async);
public Handler(Callback callback, boolean async);
public Handler(Looper looper, Callback callback, boolean async);
這樣就能發(fā)送屏障消息和異步消息了;
在系統(tǒng)源碼ViewRootImpl.scheduleTraversals
中,為了更快響應(yīng)UI刷新事件時(shí)
:
void scheduleTraversals() {
if (!mTraversalScheduled) {
mTraversalScheduled = true;
//設(shè)置同步障礙,確保mTraversalRunnable優(yōu)先被執(zhí)行
mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
//內(nèi)部通過Handler發(fā)送了一個異步消息
mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
if (!mUnbufferedInputDispatch) {
scheduleConsumeBatchedInput();
}
notifyRendererOfFramePending();
pokeDrawLockIfNeeded();
}
mTraversalRunnable
調(diào)用了performTraversals
執(zhí)行measure、layout、draw
為了讓mTraversalRunnable
盡快被執(zhí)行,在發(fā)消息之前調(diào)用MessageQueue.postSyncBarrier
設(shè)置了同步屏障