android異步消息機(jī)制 源碼層面徹底解析(1)
Handler、Message、Loopler、MessageQueen
首先看一下我們平常使用Handler的一個(gè)最常見用法。
Handler handler =new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //這里進(jìn)行一些UI操作等處理 } new Thread(new Runnable() { @Override public void run() { Message message = Message.obtain(); ........ handler.sendMessage(message); } }); };
看一下handler的構(gòu)造函數(shù)的源碼
public Handler() { this(null, false); } //他會(huì)調(diào)用本類中的如下構(gòu)造函數(shù) public Handler(Callback callback, boolean async) { if (FIND_POTENTIAL_LEAKS) { final Class<? extends Handler> klass = getClass(); if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && (klass.getModifiers() & Modifier.STATIC) == 0) { Log.w(TAG, "The following Handler class should be static or leaks might occur: " + klass.getCanonicalName()); } } mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = callback; mAsynchronous = async; }
看到當(dāng)mLooper == null時(shí)會(huì)拋一個(gè)“Can't create handler inside thread that has not called Looper.prepare()”這個(gè)異常,所以我們?cè)趧?chuàng)建handler實(shí)例前首先需要調(diào)用Looper.prepare()
public static void prepare() { prepare(true); } //將looper保存到ThreadLocal中,這里可以把ThreadLocal理解為一個(gè)以當(dāng)前線程為鍵的Map,所以一個(gè)線程中只會(huì)有一個(gè)looper private static void prepare(boolean quitAllowed) { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); } //我們看到在new Looper(quitAllowed)中,創(chuàng)建了一個(gè)消息隊(duì)列MessageQueen private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); }
接下來(lái)我們看handler.sendMessage(message)這個(gè)方法,從字面意思就是將信息發(fā)送出去。一般sendMessage累的方法最終都會(huì)調(diào)用sendMessageAtTime(Message msg, long uptimeMillis)這個(gè)方法
public boolean sendMessageAtTime(Message msg, long uptimeMillis) { 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, uptimeMillis); }
我們看到最終會(huì)執(zhí)行enqueueMessage(queue, msg, uptimeMillis)這個(gè)方法
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
最終又會(huì)調(diào)用MessageQueen中的queue.enqueueMessage(msg, uptimeMillis)這個(gè)方法,這里的queue就是looper構(gòu)造方法中創(chuàng)建的那個(gè)消息隊(duì)列
//MessageQueen的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 { // 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. 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; }
MessageQueen雖然名字是一個(gè)隊(duì)列,但實(shí)質(zhì)上他是一個(gè)單向鏈表,這個(gè)結(jié)構(gòu)能快速進(jìn)行插入和刪除操作。從上面源碼可以看出來(lái),主要是按照發(fā)送消息的時(shí)間順序?qū)sg插入到消息隊(duì)列中。接下來(lái)我們就需要從消息隊(duì)列中取出msg了。這時(shí)候就需要調(diào)用Looper.loop()方法。
public static void loop() { final Looper me = myLooper(); if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } final MessageQueue queue = me.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(); final long ident = Binder.clearCallingIdentity(); for (;;) { //不斷從消息隊(duì)列中取出msg Message msg = queue.next(); // might block if (msg == null) { // 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 Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } //將msg交由handler處理 msg.target.dispatchMessage(msg); if (logging != null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); } // 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(); } }
可以看到Looper.loop()方法通過(guò)在一個(gè)死循環(huán)中調(diào)用Message msg = queue.next()將消息不斷的從消息隊(duì)列中取出來(lái)。queue.next()方法的作用就是從消息隊(duì)列中取msg,唯一跳出循環(huán)的方式是MessageQueen的next方法返回了null?,F(xiàn)在msg已經(jīng)取出來(lái),下一步就是怎樣將他傳遞給handler了對(duì)吧。所以在死循環(huán)中還有一個(gè)方法msg.target.dispatchMessage(msg) ,而msg.target就是handler,在上面handler的enqueueMessage()方法中傳入的msg.target = this,this就是handler本身,接下來(lái)就看看handler的dispatchMessage()方法
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
如果我們采用無(wú)參的構(gòu)造函數(shù)創(chuàng)建handler,msg.callback與mCallback均為空,所以我們會(huì)調(diào)用handleMessage(msg),這樣文章開頭的那個(gè)實(shí)例整個(gè)流程就走完了,handleMessage(msg)會(huì)在handler實(shí)例所在的線程中執(zhí)行。
//當(dāng)我們通過(guò)這種方式創(chuàng)建handler時(shí),dispatchMessage中的mCallback就不為null public Handler(Callback callback) { this(callback, false); } //Callback是一個(gè)接口,里面正好也有我們需要的handleMessage(Message msg),dispatchMessage中的 if (mCallback != null) 語(yǔ)句內(nèi)的內(nèi)容,就是我們需要重寫的handleMessage(Message msg)方法 public interface Callback { public boolean handleMessage(Message msg); }
//當(dāng)我們調(diào)用handler.post()方法執(zhí)行異步任務(wù)時(shí) public final boolean post(Runnable r) { return sendMessageDelayed(getPostMessage(r), 0); } //getPostMessage(r)這個(gè)方法中我們看到給m.callback賦值了,就是我們傳入的runnable接口 private static Message getPostMessage(Runnable r) { Message m = Message.obtain(); m.callback = r; return m; } //最后在handleCallback方法中我們執(zhí)行了它的run方法,這也就解釋了為什么在子線程中可以用handler.post(Runnable r)更新UI private static void handleCallback(Message message) { message.callback.run(); }
總結(jié)
梳理整個(gè)執(zhí)行過(guò)程
1.調(diào)用Looper.prepare()方法,這是創(chuàng)建handler所必須的。在主線程中由于ActivityThread已經(jīng)通過(guò)Looper.prepareMainLooper()方法創(chuàng)建過(guò)looper,所以在主線程中創(chuàng)建handler以前無(wú)需創(chuàng)建looper,并通過(guò)Looper.loop()來(lái)開啟主線程的消息循環(huán)。
2.通過(guò)調(diào)用handler.sendMessage(message)方法最終會(huì)執(zhí)行enqueueMessage(queue, msg, uptimeMillis),enqueueMessage又會(huì)調(diào)用MessageQueen的queue.enqueueMessage(msg, uptimeMillis),這樣消息就會(huì)被添加到消息隊(duì)列中。
3.調(diào)用Looper.loop()方法在死循環(huán)中執(zhí)行Message msg = queue.next(),不斷的將msg從消息隊(duì)列中取出來(lái),同時(shí)執(zhí)行msg.target.dispatchMessage(msg),將消息傳遞給handler,由handler來(lái)處理,如我們調(diào)用的handleMessage就是處理消息的方式之一。
異步處理機(jī)制流程圖
從子線程進(jìn)行UI 操作的幾種方式
Android 提供了幾種途徑來(lái)從其他線程訪問(wèn) UI 線程。以下列出了幾種有用的方法:
• Activity.runOnUiThread(Runnable)
• View.post(Runnable) 這里的view就是我們需要改變的ui控件
• View.postDelayed(Runnable, long)
• Handler.post(Runnable, long)
但是,隨著操作日趨復(fù)雜,這類代碼也會(huì)變得復(fù)雜且難以維護(hù)。 要通過(guò)工作線程處理更復(fù)雜的交互,可以考慮在工作線程中使用 Handler 處理來(lái)自 UI 線程的消息。當(dāng)然,最好的解決方案或許是擴(kuò)展 AsyncTask 類,此類簡(jiǎn)化了與 UI 進(jìn)行交互所需執(zhí)行的工作線程任務(wù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:com.android.support版本沖突解決方法
欄 目:Android
下一篇:android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
本文標(biāo)題:android異步消息機(jī)制 源碼層面徹底解析(1)
本文地址:http://mengdiqiu.com.cn/a1/Android/9198.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10com.android.support版本沖突解決方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改