一、問題:在Android啟動后會在新進程里創建一個主線程,也叫UI線程(非線程安全)這個線程主要負責監聽屏幕點擊事件與界面繪制。當Application需要進行耗時操作如網絡請求等,如直接在主線程進行容易發生ANR錯誤。所以會創建子線程來執行耗時任務,當子線程執行完畢需要通知UI線程并修改界面時,不可以直接在子線程修改UI,怎么辦?
解決方法:Message Queue機制可以實現子線程與UI線程的通信。
該機制包括Handler、Message Queue、Looper。Handler可以把消息/Runnable對象發給Looper,由它把消息放入所屬線程的消息隊列中,然后Looper又會自動把消息隊列里的消息/Runnable對象廣播到所屬線程里的Handler,由Handler處理接收到的消息或Runnable對象。
1、Handler
每次創建Handler對象時,它會自動綁定到創建它的線程上。如果是主線程則默認包含一個Message Queue,否則需要自己創建一個消息隊列來存儲。
Handler是多個線程通信的信使。比如在線程A中創建AHandler,給它綁定一個ALooper,同時創建屬于A的消息隊列AMessageQueue。然后在線程B中使用AHandler發送消息給ALooper,ALooper會把消息存入到AMessageQueue,然后再把AMessageQueue廣播給A線程里的AHandler,它接收到消息會進行處理。從而實現通信。
2、Message Queue
在主線程里默認包含了一個消息隊列不需要手動創建。在子線程里,使用Looper.prepare()方法后,會先檢查子線程是否已有一個looper對象,如果有則無法創建,因為每個線程只能擁有一個消息隊列。沒有的話就為子線程創建一個消息隊列。
3、完整創建handler機制
Handler類包含Looper指針和MessageQueue指針,而Looper里包含實際MessageQueue與當前線程指針。
下面分別就UI線程和worker線程講解handler創建過程:
首先,創建handler時,會自動檢查當前線程是否包含looper對象,如果包含,則將handler內的消息隊列指向looper內部的消息隊列,否則,拋出異常請求執行looper.prepare()方法。
- 在UI線程中,系統自動創建了Looper 對象,所以,直接new一個handler即可使用該機制;
- 在worker線程中,如果直接創建handler會拋出運行時異常-即通過查‘線程-value'映射表發現當前線程無looper對象。所以需要先調用Looper.prepare()方法。在prepare方法里,利用ThreadLocal<Looper>對象為當前線程創建一個Looper(利用了一個Values類,即一個Map映射表,專為thread存儲value,此處為當前thread存儲一個looper對象)。然后繼續創建handler,讓handler內部的消息隊列指向該looper的消息隊列(這個很重要,讓handler指向looper里的消息隊列,即二者共享同一個消息隊列,然后handler向這個消息隊列發送消息,looper從這個消息隊列獲取消息)。然后looper循環消息隊列即可。當獲取到message消息,會找出message對象里的target,即原始發送handler,從而回調handler的handleMessage() 方法進行處理。

handler機制
4、核心
 - handler與looper共享消息隊列,所以handler發送消息只要入列,looper直接取消息即可。
- 線程與looper映射表:一個線程最多可以映射一個looper對象。通過查表可知當前線程是否包含looper,如果已經包含則不再創建新looper。
5、基于這樣的機制是怎樣實現線程隔離的,即在線程中通信呢。
核心在于每一個線程擁有自己的handler、message queue、looper體系。而每個線程的Handler是公開的。B線程可以調用A線程的handler發送消息到A的共享消息隊列去,然后A的looper會自動從共享消息隊列取出消息進行處理。反之一樣。

子線程向主線程發送消息

子線程之間通信
二、上面是基于子線程中利用主線程提供的Handler發送消息出去,然后主線程的Looper從消息隊列中獲取并處理。那么還有另外兩種情況:
1、主線程發送消息到子線程中;
采用的方法和前面類似。要在子線程中實例化AHandler并設定處理消息的方法,同時由于子線程沒有消息隊列和Looper的輪詢,所以要加上Looper.prepare(),Looper.loop()分別創建消息隊列和開啟輪詢。然后在主線程中使用該AHandler去發送消息即可。
2、子線程A與子線程B之間的通信。
三、Handler里面有什么實用的API嗎?
請記?。?/p>
Handler只是簡單往消息隊列中發送消息而已(或者使用post方式)
它們有更方便的方法可以幫助與UI線程通信。
如果你現在看看Handler的API,可以清楚看到這幾個方法:
代碼示例
這里的代碼都是很基礎的,不過你可以好好看看注釋。
示例1:使用Handler的“post”方法
public class TestActivity extends Activity { // ...// all standard stuff @Overridepublic 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對象,回調post方法會比較難辦。
示例2:使用postDelayed方法
近期本站新介紹的特性中,我每次都要模擬EditText的自動完成功能,每次文字改變后都會觸發一個API的調用,從服務器中檢索數據。
我想減少APP調用API的次數,所以決定使用Handler的postDelayed方法來實現這個功能。
本例不針對平行處理,只是關于Handler給消息隊列發送消息還有安排消息在未來的某一點執行等。
// 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; @Overridepublic 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”這個方法作為聯系留給讀者們了,掌握Handler了嗎?如果是的話,那么可以盡情使用線程了。
新聞熱點
疑難解答