歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Handler消息傳遞機制詳解

Android Handler消息傳遞機制詳解

日期:2017/3/1 9:31:10   编辑:Linux編程

1.為什麼要用Handler

  出於性能優化的考慮,Android UI操作並不是線程安全,如果有多個線程並發操作UI組件,可能導致線程安全問題。可以設想下,如果在一個Activity中有多個線程去更新UI,並且都沒有加鎖機制,可能會導致什麼問題? 界面混亂,如果加鎖的話可以避免該問題但又會導致性能下降。因此,Android規定只允許UI線程修改Activity的UI組件。當程序第一次啟動時,Android會同時啟動一條主線程(Main Thread),主線程主要負責處理與UI相關的事件,比如用戶按鈕事件,並把相關的事件分發到對應的組件進行處理,因此主線程又稱為UI線程。那麼怎麼在新啟動的線程中更新UI組件呢,這就需要借助handler的消息傳遞機制來實現了。

2.Handler簡介

  Handler類的主要作用主要有兩個:

    1>在新啟動的線程中發送消息

    2>在主線程中獲取和處理消息

  Handler類包含如下方法用於發送、處理消息。(這裡只列出常用的方法,如果想獲取更多方法,建議查看api文檔:http://developer.android.com/reference/android/os/Handler.html)

    ♦ void handlerMessage(Message msg):處理消息的方法,該方法通常用於被重寫。

    ♦ final boolean hasMessage(int what):檢查消息隊列中是否包含what屬性為指定值的消息。

    ♦ sendEmptyMessage(int what):發送空消息

    ♦ final boolean sendMessage(Message msg):立即發送消息,注意這塊返回值,如果message成功的被放到message queue裡面則返回true,反之,返回false;(個人建議:對於這類問題不必主觀去記它,當實際使用時,直接查看源碼即可,源碼中有詳細的注釋)

3.Handler、Message、Looper、MessageQueue之間的關系、工作原理

  為了更好的理解Handler,先來看看和Handler相關的一些組件:

    Message:Handler發送、接收和處理的消息對象

    Looper:每個線程只能擁有一個Looper.它的looper()方法負責循環讀取MessageQueue中的消息並將讀取到的消息交給發送該消息的handler進行處理。

    MessageQueue:消息隊列,它采用先進先出的方式來管理Message。程序在創建Looper對象時,會在它的構造器中創建MessageQueue。源碼如下:

    

1   private Looper(boolean quitAllowed) {
2         mQueue = new MessageQueue(quitAllowed);
3         mThread = Thread.currentThread();
4     }

    從源碼第2行中可以看出,在創建Looper對象時會創建一個與之關聯的MessageQueue對象。構造器是private修飾的,所以程序員是無法創建Looper對象的。

    Handler:前面說Handler作用有兩個---發送消息和處理消息,Handler發送的消息必須被送到指定的MessageQueue,也就是說,要想Handler正常工作必須在當前線程中有一個MessageQueue,否則消息沒法保存。而MessageQueue是由Looper負責管理的,因此要想Handler正常工作,必須在當前線程中有一個Looper對象,這裡分為兩種情況:

      1>主線程(UI線程),系統已經初始化了一個Looper對象,因此程序直接創建Handler即可

      2>程序員自己創建的子線程,這時,程序員必須創建一個Looper對象,並啟動它。

    創建Looper使用:Looper.prepare(),查看源碼:

 1    public static void prepare() {
 2         prepare(true);
 3     }
 4 
 5   private static void prepare(boolean quitAllowed) {
 6         if (sThreadLocal.get() != null) {
 7             throw new RuntimeException("Only one Looper may be created per thread");
 8         }
 9         sThreadLocal.set(new Looper(quitAllowed));
10     }
11 
12   private Looper(boolean quitAllowed) {
13         mQueue = new MessageQueue(quitAllowed);
14         mThread = Thread.currentThread();
15     }

    通過方法調用,第9行創建Looper對象,創建Looper對象時同時會創建MessageQueue對象(第13行)。此外,可以看出prepare()允許一個線程最多有一個Looper被創建。

    然後調用Looper的looper()方法來啟動它,looper()使用一個死循環不斷取出MessageQueue中的消息,並將消息發送給對應的Handler進行處理。下面是Looper類中looper()方法的部分源碼:

 1 for (;;) {
 2             Message msg = queue.next(); // might block
 3             if (msg == null) {
 4                 // No message indicates that the message queue is quitting.
 5                 return;
 6             }
 7 
 8             // This must be in a local variable, in case a UI event sets the logger
 9             Printer logging = me.mLogging;
10             if (logging != null) {
11                 logging.println(">>>>> Dispatching to " + msg.target + " " +
12                         msg.callback + ": " + msg.what);
13             }
14 
15             msg.target.dispatchMessage(msg);
16 
17             if (logging != null) {
18                 logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
19             }
20 
21             // Make sure that during the course of dispatching the
22             // identity of the thread wasn't corrupted.
23             final long newIdent = Binder.clearCallingIdentity();
24             if (ident != newIdent) {
25                 Log.wtf(TAG, "Thread identity changed from 0x"
26                         + Long.toHexString(ident) + " to 0x"
27                         + Long.toHexString(newIdent) + " while dispatching to "
28                         + msg.target.getClass().getName() + " "
29                         + msg.callback + " what=" + msg.what);
30             }
31 
32             msg.recycleUnchecked();
33         }

    很明顯第1行用了一個死循環,第2行從queue中取出Message,第15行通過dispatchMessage(Message msg)方法將消息發送給Handler。

4.HandlerThread介紹

  Android API解釋:

  Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

  意思是說:這個類啟動一個新的線程並且創建一個Looper,這個Looper可以用來創建一個Handler類,完了之後一定要啟動這個線程。

  什麼時候使用HandlerThread?

    1.主線程需要通知子線程執行耗時操作(一般都是子線程執行耗時操作,完了之後,發送消息給主線程更新UI)。

    2.開發中可能會多次創建匿名線程,這樣可能會消耗更多的系統資源。而HandlerThread自帶Looper使他可以通過消息來多次重復使用當前線程,節省開支;

  下面是HandlerThread應用部分代碼:

 1   private static final String TAG = "MainActivity";
 2     private static final int FLAG_TEST = 1;
 3 
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_main);
 8         Log.i(TAG,"main thread:"+Thread.currentThread());
 9         HandlerThread thread = new HandlerThread("handler thread");
10         thread.start();//一定要啟動該線程
11         Handler handler = new Handler(thread.getLooper()){
12             @Override
13             public void handleMessage(Message msg) {
14                 Log.i(TAG,"handler thread:"+Thread.currentThread());
15                 switch (msg.what){
16                     case FLAG_TEST:
17                         //耗時操作...
18                         break;
19                     default:
20                         break;
21                 }
22                  super.handleMessage(msg);
23             }
24         };
25         handler.sendEmptyMessage(FLAG_TEST);
26     }

    log:

com.example.administrator.handlertest I/MainActivity﹕ main thread:Thread[main,5,main]
com.example.administrator.handlertest I/MainActivity﹕ handler thread:Thread[handler thread,5,main]

  通過log可以看出handler處在一個子線程中,這樣就能夠執行一些耗時操作。

  第十一行通過thread.getLooper()來創建handler,那麼我們來看下getLooper()裡面的源碼:

 1 public Looper getLooper() {
 2         if (!isAlive()) {
 3             return null;
 4         }
 5         
 6         // If the thread has been started, wait until the looper has been created.
 7         synchronized (this) {
 8             while (isAlive() && mLooper == null) {
 9                 try {
10                     wait();
11                 } catch (InterruptedException e) {
12                 }
13             }
14         }
15         return mLooper;
16     }

    看第8行代碼,如果這個線程可用並且looper為null時,就會調用wait()方法,處於等待狀態,這樣可以有效的避免多線程並發操作引起的空指針異常。在thread啟動時,會調用run()方法,再來看看run()方法裡面的代碼:

 1 @Override
 2     public void run() {
 3         mTid = Process.myTid();
 4         Looper.prepare();
 5         synchronized (this) {
 6             mLooper = Looper.myLooper();
 7             notifyAll();
 8         }
 9         Process.setThreadPriority(mPriority);
10         onLooperPrepared();
11         Looper.loop();
12         mTid = -1;
13     }

  第4行創建了Looper對象,第6、7行獲取當前線程Looper之後調用notifyAll()方法。這時調用getLooper()方法返回一個Looper對象。 

  上面有提到使用HandlerThread避免多線程並發操作引起的空指針異常,這裡解釋下為什麼:如果onCreate方法第11行通過程序員自定義的一個新線程創建handler時,很可能出現這樣一個結果:創建handler的代碼已經執行了,而新線程卻還沒有Looper.prepare()(創建Looper對象,那麼這樣就會導致空指針異常)。

  對代碼稍做修改:

 1 package com.example.administrator.handlertest;
 2 
 3 import android.os.Bundle;
 4 import android.os.Handler;
 5 import android.os.Looper;
 6 import android.os.Message;
 7 import android.support.v7.app.ActionBarActivity;
 8 import android.util.Log;
 9 
10 public class MainActivity extends ActionBarActivity {
11 
12     private static final String TAG = "MainActivity";
13     private static final int FLAG_TEST = 1;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         Log.i(TAG,"main thread:"+Thread.currentThread());
20 //        HandlerThread thread = new HandlerThread("handler thread");
21 //        thread.start();//一定要啟動該線程
22         MyThread thread = new MyThread();
23         thread.start();
24         Handler handler = new Handler(thread.looper){
25             @Override
26             public void handleMessage(Message msg) {
27                 Log.i(TAG,"handler thread:"+Thread.currentThread());
28                 switch (msg.what){
29                     case FLAG_TEST:
30                         //耗時操作...
31                         break;
32                     default:
33                         break;
34                 }
35                  super.handleMessage(msg);
36             }
37         };
38         handler.sendEmptyMessage(FLAG_TEST);
39     }
40 
41     static class MyThread extends Thread{
42         Looper looper;
43         @Override
44         public void run() {
45             Looper.prepare();looper = Looper.myLooper();
46             //...
47             Looper.loop();
48         }
49     }
50 }

運行結果:

1 Caused by: java.lang.NullPointerException
2 at android.os.Handler.<init>(Handler.java:234)
3 at android.os.Handler.<init>(Handler.java:142)
4 at com.example.administrator.handlertest.MainActivity$1.<init>(MainActivity.java:24)
5 at com.example.administrator.handlertest.MainActivity.onCreate(MainActivity.java:24)
6 at android.app.Activity.performCreate(Activity.java:5211)
7 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1151)
8 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2341)

從異常信息第4行中可以看出:onCreate()方法第24行thread.looper是一個null.這時因為還沒等新線程創建Looper,Handler就已經創建了。如果在第23行thread.start()後面休眠幾秒就不會報空指針異常了。

最後補充一點,Android判斷當前更新UI的線程是否是主線程的對象ViewRootImpl對象在onResume()中,所以只要子線程在onResume()之前完成更新UI也是能夠實現的。這裡只是簡單提一下,知道就行,不過不要這麼做。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved