android線程消息機(jī)制之Handler詳解
android線程消息機(jī)制主要由Handler,Looper,Message和MessageQuene四個(gè)部分組成。平常在開發(fā)中,我們常用來在子線程中通知主線程來更新,其實(shí)整個(gè)安卓生命周期的驅(qū)動(dòng)都是通過Handler(ActivityThread.H)來實(shí)現(xiàn)的。
首先我們先介紹這四個(gè)類的作用:
Handler:消息的發(fā)送者。負(fù)責(zé)將Message消息發(fā)送到MessageQueue中。以及通過Runnable,Callback或者h(yuǎn)andleMessage()來實(shí)現(xiàn)消息的回調(diào)處理
Looper:是消息的循環(huán)處理器,它負(fù)責(zé)從MessageQueue中取出Message對(duì)象進(jìn)行處理。(Looper含有MessageQueue的引用)
Message:是消息載體,通過target來指向handler的引用。通過object來包含業(yè)務(wù)邏輯數(shù)據(jù)。其中MessagePool為消息池,用于回收空閑的Message對(duì)象的。
MessageQueue:消息隊(duì)列,負(fù)責(zé)維護(hù)待處理的消息對(duì)象。
通過上面的圖,我們可以比較清楚地知道他們的作用以及關(guān)系。接下來,我們從源碼角度來分析這種關(guān)系是如何建立的。
public Handler(Looper looper, Callback callback, boolean async) { mLooper = looper; mQueue = looper.mQueue; mCallback = callback; mAsynchronous = async; }
hander的其它構(gòu)造方法可以自己去查看,通過這個(gè)構(gòu)造方法,我們知道,handler持有MessageQueue的引用。所以可以方便地將Message加入到隊(duì)列中去。
通過源碼我們發(fā)現(xiàn),sendMessage->sendMessageDelayed->sendMessageAtTime->enqueueMessage
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); }
都是通過enqueueMessage將message將加入到MessageQueue中。
接下來,我們看Message是如何構(gòu)造的。通過Message的構(gòu)造方法。
public static Message obtain() { synchronized (sPoolSync) { if (sPool != null) { Message m = sPool; sPool = m.next; m.next = null; m.flags = 0; // clear in-use flag sPoolSize--; return m; } } return new Message(); }
我們看到,Message是通過obtain的靜態(tài)方法從消息池sPool中拿到的。這樣可以做到消息的復(fù)用。
public static Message obtain(Handler h) { Message m = obtain(); m.target = h; return m; }
其中有一個(gè)重載方法中m.target = h;這段代碼非常重要,便于后面找到消息的目標(biāo)handler進(jìn)行處理。
接下來,我們來看Looper。我們知道Looper通過過Looper.loop來進(jìn)入循環(huán)的,而循環(huán)是通過線程的run方法的驅(qū)動(dòng)的。
首先我們知道,我們?cè)趧?chuàng)建Handler的時(shí)候,都沒有去創(chuàng)建Looper,那么Looper哪里來的呢?
public Handler(Callback callback, boolean async) { ... 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; }
再看看Looper.myLooper()
public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
ThreadLocal是線程創(chuàng)建線程局部變量的類。表示此變量只屬于當(dāng)前線程。
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(); }
我們看到了sThreadLocal.get()的方法實(shí)際是取當(dāng)前線程中的Looper對(duì)象。
那么我們主線程的Looper到底在哪里創(chuàng)建的呢?
而我們清楚地知道,如果在子線程中創(chuàng)建handler調(diào)用,則需要使用Looper.prepare方法。
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)); }
我們看到此方法中,如果此線程中沒有Looper對(duì)象,則創(chuàng)建一個(gè)Looper對(duì)象。接下來我們?cè)谠创a中看到一個(gè)熟悉的方法。
public static void prepareMainLooper() { prepare(false); synchronized (Looper.class) { if (sMainLooper != null) { throw new IllegalStateException("The main Looper has already been prepared."); } sMainLooper = myLooper(); } }
此方法單獨(dú)的創(chuàng)建了一個(gè)sMainLooper用于主線程的Looper。這個(gè)prepareMainLooper到底在哪里調(diào)用呢?
高過引用指向發(fā)現(xiàn),我們?cè)贏ctivityThread.main()方法中發(fā)現(xiàn)
public static void main(String[] args) { ... Looper.prepareMainLooper(); ActivityThread thread = new ActivityThread(); thread.attach(false); if (sMainThreadHandler == null) { sMainThreadHandler = thread.getHandler(); } if (false) { Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, "ActivityThread")); } // End of event ActivityThreadMain. Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }
而ActivityThread.main()是程序的入口方法。這樣我們就非常清楚了,主線程的Looper在程序的啟動(dòng)過程中就已經(jīng)創(chuàng)建并循環(huán)。
那么如果在子線程中創(chuàng)建Looper該如何正確調(diào)用呢?
class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } }
接下來,我們需要看下Looper.loop()的執(zhí)行方法
public static void loop() { final Looper me = myLooper();//拿到當(dāng)前線程的looper if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } final MessageQueue queue = me.mQueue;//拿到當(dāng)前l(fā)ooper的消息隊(duì)列 // 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 (;;) {//死循環(huán)遍歷消息體。如果為null,則休眠。 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 final Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } final long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs; final long traceTag = me.mTraceTag; if (traceTag != 0 && Trace.isTagEnabled(traceTag)) { Trace.traceBegin(traceTag, msg.target.getTraceName(msg)); } final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis(); final long end; try { msg.target.dispatchMessage(msg);//此處是真正的分發(fā)消息。此處的target即是handler對(duì)象 end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis(); } finally { if (traceTag != 0) { Trace.traceEnd(traceTag); } } if (slowDispatchThresholdMs > 0) { final long time = end - start; if (time > slowDispatchThresholdMs) { Slog.w(TAG, "Dispatch took " + time + "ms on " + Thread.currentThread().getName() + ", h=" + msg.target + " cb=" + msg.callback + " msg=" + msg.what); } } 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(); } }
最后我們看下dispatchMessage的處理方法。
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
我們看到,dispatchMessage是優(yōu)化處理msg.callback,然后就是實(shí)現(xiàn)的Callback接口,最后才是handleMessage方法。
重點(diǎn)說明:
1、handler在實(shí)例化的時(shí)候,持有Looper的引用。是通過ThreadLocal與Handler進(jìn)行關(guān)聯(lián)的。
2、Message在實(shí)例化的過程中,通過target 持有Handler的引用。
3、通常一個(gè)線程對(duì)應(yīng)一個(gè)Looper.一個(gè)Looper可以屬于多個(gè)Handler。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 圖片存入系統(tǒng)相冊(cè)更新顯示實(shí)例詳解
這篇文章主要介紹了Android 圖片存入系統(tǒng)相冊(cè)更新顯示實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Java語言讀取配置文件config.properties的方法講解
今天小編就為大家分享一篇關(guān)于Java語言讀取配置文件config.properties的方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-08-08Android webview打開本地圖片上傳實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android webview打開本地圖片上傳的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android-Jetpack-Navigation組件使用示例
這篇文章主要介紹了Android-Jetpack-Navigation組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android基于OpenCV實(shí)現(xiàn)圖像修復(fù)
實(shí)際應(yīng)用中,圖像常常容易受損,如存在污漬的鏡頭、舊照片的劃痕、人為的涂畫(比如馬賽克),亦或是圖像本身的損壞。修復(fù)圖像就成為一個(gè)常見的需求了,本文講述Android基于OpenCV實(shí)現(xiàn)圖像修復(fù)的步驟,有此需求的朋友可以參考下2021-06-06android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02