深入理解Android中的Handler異步通信機(jī)制
一、問題:在Android啟動后會在新進(jìn)程里創(chuàng)建一個主線程,也叫UI線程(非線程安全)這個線程主要負(fù)責(zé)監(jiān)聽屏幕點(diǎn)擊事件與界面繪制。當(dāng)Application需要進(jìn)行耗時操作如網(wǎng)絡(luò)請求等,如直接在主線程進(jìn)行容易發(fā)生ANR錯誤。所以會創(chuàng)建子線程來執(zhí)行耗時任務(wù),當(dāng)子線程執(zhí)行完畢需要通知UI線程并修改界面時,不可以直接在子線程修改UI,怎么辦?
解決方法:Message Queue機(jī)制可以實(shí)現(xiàn)子線程與UI線程的通信。
該機(jī)制包括Handler、Message Queue、Looper。Handler可以把消息/Runnable對象發(fā)給Looper,由它把消息放入所屬線程的消息隊(duì)列中,然后Looper又會自動把消息隊(duì)列里的消息/Runnable對象廣播到所屬線程里的Handler,由Handler處理接收到的消息或Runnable對象。
1、Handler
每次創(chuàng)建Handler對象時,它會自動綁定到創(chuàng)建它的線程上。如果是主線程則默認(rèn)包含一個Message Queue,否則需要自己創(chuàng)建一個消息隊(duì)列來存儲。
Handler是多個線程通信的信使。比如在線程A中創(chuàng)建AHandler,給它綁定一個ALooper,同時創(chuàng)建屬于A的消息隊(duì)列AMessageQueue。然后在線程B中使用AHandler發(fā)送消息給ALooper,ALooper會把消息存入到AMessageQueue,然后再把AMessageQueue廣播給A線程里的AHandler,它接收到消息會進(jìn)行處理。從而實(shí)現(xiàn)通信。
2、Message Queue
在主線程里默認(rèn)包含了一個消息隊(duì)列不需要手動創(chuàng)建。在子線程里,使用Looper.prepare()方法后,會先檢查子線程是否已有一個looper對象,如果有則無法創(chuàng)建,因?yàn)槊總€線程只能擁有一個消息隊(duì)列。沒有的話就為子線程創(chuàng)建一個消息隊(duì)列。
3、完整創(chuàng)建handler機(jī)制
Handler類包含Looper指針和MessageQueue指針,而Looper里包含實(shí)際MessageQueue與當(dāng)前線程指針。
下面分別就UI線程和worker線程講解handler創(chuàng)建過程:
首先,創(chuàng)建handler時,會自動檢查當(dāng)前線程是否包含looper對象,如果包含,則將handler內(nèi)的消息隊(duì)列指向looper內(nèi)部的消息隊(duì)列,否則,拋出異常請求執(zhí)行l(wèi)ooper.prepare()方法。
- 在UI線程中,系統(tǒng)自動創(chuàng)建了Looper 對象,所以,直接new一個handler即可使用該機(jī)制;
- 在worker線程中,如果直接創(chuàng)建handler會拋出運(yùn)行時異常-即通過查‘線程-value'映射表發(fā)現(xiàn)當(dāng)前線程無looper對象。所以需要先調(diào)用Looper.prepare()方法。在prepare方法里,利用ThreadLocal<Looper>對象為當(dāng)前線程創(chuàng)建一個Looper(利用了一個Values類,即一個Map映射表,專為thread存儲value,此處為當(dāng)前thread存儲一個looper對象)。然后繼續(xù)創(chuàng)建handler,讓handler內(nèi)部的消息隊(duì)列指向該looper的消息隊(duì)列(這個很重要,讓handler指向looper里的消息隊(duì)列,即二者共享同一個消息隊(duì)列,然后handler向這個消息隊(duì)列發(fā)送消息,looper從這個消息隊(duì)列獲取消息)。然后looper循環(huán)消息隊(duì)列即可。當(dāng)獲取到message消息,會找出message對象里的target,即原始發(fā)送handler,從而回調(diào)handler的handleMessage() 方法進(jìn)行處理。
handler機(jī)制
4、核心
- handler與looper共享消息隊(duì)列,所以handler發(fā)送消息只要入列,looper直接取消息即可。
- 線程與looper映射表:一個線程最多可以映射一個looper對象。通過查表可知當(dāng)前線程是否包含looper,如果已經(jīng)包含則不再創(chuàng)建新looper。
5、基于這樣的機(jī)制是怎樣實(shí)現(xiàn)線程隔離的,即在線程中通信呢。
核心在于每一個線程擁有自己的handler、message queue、looper體系。而每個線程的Handler是公開的。B線程可以調(diào)用A線程的handler發(fā)送消息到A的共享消息隊(duì)列去,然后A的looper會自動從共享消息隊(duì)列取出消息進(jìn)行處理。反之一樣。
子線程向主線程發(fā)送消息
子線程之間通信
二、上面是基于子線程中利用主線程提供的Handler發(fā)送消息出去,然后主線程的Looper從消息隊(duì)列中獲取并處理。那么還有另外兩種情況:
1、主線程發(fā)送消息到子線程中;
采用的方法和前面類似。要在子線程中實(shí)例化AHandler并設(shè)定處理消息的方法,同時由于子線程沒有消息隊(duì)列和Looper的輪詢,所以要加上Looper.prepare(),Looper.loop()分別創(chuàng)建消息隊(duì)列和開啟輪詢。然后在主線程中使用該AHandler去發(fā)送消息即可。
2、子線程A與子線程B之間的通信。
三、Handler里面有什么實(shí)用的API嗎?
請記?。?/p>
Handler只是簡單往消息隊(duì)列中發(fā)送消息而已(或者使用post方式)
它們有更方便的方法可以幫助與UI線程通信。
如果你現(xiàn)在看看Handler的API,可以清楚看到這幾個方法:
- post
- postDelayed
- postAtTime
代碼示例
這里的代碼都是很基礎(chǔ)的,不過你可以好好看看注釋。
示例1:使用Handler的“post”方法
public class TestActivity extends Activity { // ... // all standard stuff @Override public void onCreate(Bundle savedInstanceState) { // ... // all standard stuff // we're creating a new handler here // and we're in the UI Thread (default) // so this Handler is associated with the UI thread Handler mHandler = new Handler(); // I want to start doing something really long // which means I should run the fella in another thread. // I do that by sending a message - in the form of another runnable object // But first, I'm going to create a Runnable object or a message for this Runnable mRunnableOnSeparateThread = new Runnable() { @Override public void run () { // do some long operation longOperation(); // After mRunnableOnSeparateThread is done with it's job, // I need to tell the user that i'm done // which means I need to send a message back to the UI thread // who do we know that's associated with the UI thread? mHandler.post(new Runnable(){ @Override public void run(){ // do some UI related thing // like update a progress bar or TextView // .... } }); } }; // Cool but I've not executed the mRunnableOnSeparateThread yet // I've only defined the message to be sent // When I execute it though, I want it to be in a different thread // that was the whole point. new Thread(mRunnableOnSeparateThread).start(); } }
如果根本就沒有Handler對象,回調(diào)post方法會比較難辦。
示例2:使用postDelayed方法
近期本站新介紹的特性中,我每次都要模擬EditText的自動完成功能,每次文字改變后都會觸發(fā)一個API的調(diào)用,從服務(wù)器中檢索數(shù)據(jù)。
我想減少APP調(diào)用API的次數(shù),所以決定使用Handler的postDelayed方法來實(shí)現(xiàn)這個功能。
本例不針對平行處理,只是關(guān)于Handler給消息隊(duì)列發(fā)送消息還有安排消息在未來的某一點(diǎn)執(zhí)行等。
// the below code is inside a TextWatcher // which implements the onTextChanged method // I've simplified it to only highlight the parts we're // interested in private long lastChange = 0; @Override public void onTextChanged(final CharSequence chars, int start, int before, int count) { // The handler is spawned from the UI thread new Handler().postDelayed( // argument 1 for postDelated = message to be sent new Runnable() { @Override public void run() { if (noChangeInText_InTheLastFewSeconds()) { searchAndPopulateListView(chars.toString()); // logic } } }, // argument 2 for postDelated = delay before execution 300); lastChange = System.currentTimeMillis(); } private boolean noChangeInText_InTheLastFewSeconds() { return System.currentTimeMillis() - lastChange >= 300 }
最后我就把“postAtTime”這個方法作為聯(lián)系留給讀者們了,掌握Handler了嗎?如果是的話,那么可以盡情使用線程了。
- android的消息處理機(jī)制(圖文+源碼分析)—Looper/Handler/Message
- Android多線程處理機(jī)制中的Handler使用介紹
- Android消息處理機(jī)制Looper和Handler詳解
- Android Handler 機(jī)制實(shí)現(xiàn)原理分析
- Android中Handler消息傳遞機(jī)制
- Android 消息機(jī)制以及handler的內(nèi)存泄露
- Android消息機(jī)制Handler的工作過程詳解
- Android Handler消息派發(fā)機(jī)制源碼分析
- android線程消息機(jī)制之Handler詳解
- Android Handler機(jī)制的工作原理詳析
相關(guān)文章
詳解android webView獨(dú)立進(jìn)程通訊方式
本篇文章主要介紹了android webView獨(dú)立進(jìn)程通訊方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09Win10下Android App安裝配置開發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了Win10下Android App安裝配置開發(fā)環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Android ToolBar 修改邊距的實(shí)現(xiàn)方法
這篇文章主要介紹了Android ToolBar 修改邊距的實(shí)現(xiàn)方法的相關(guān)資料,通過此文希望能幫助到大家,需要的朋友可以參考下2017-08-08Android實(shí)現(xiàn)斷點(diǎn)下載的方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)斷點(diǎn)下載的方法,感興趣的小伙伴們可以參考一下2016-03-03Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法,涉及Android針對按鈕元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2015-11-11Android框架Volley之利用Imageloader和NetWorkImageView加載圖片的方法
這篇文章主要介紹了Android框架Volley之利用Imageloader和NetWorkImageView加載圖片的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式
這篇文章主要介紹了Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03