1. 程式人生 > >Handler、Thread、HandlerThread三者的區別

Handler、Thread、HandlerThread三者的區別

轉自Handler、Thread、HandlerThread三者的區別

一、前期知識儲備

(1)Handler類,上官方文件,Handler

public class Handler.A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

2)Thread類,上官方文件,Thread

public class Thread. extends Object implements Runnable.A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

3)HandlerThread類,上官方文件,HandlerThread

public class HandlerThread. extends Thread. 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.

二、三者的區別

①Handler:在android中負責傳送和處理訊息,通過它可以實現其他支線執行緒與主執行緒之間的訊息通訊。

②Thread:Java程序中執行運算的最小單位,亦即執行處理機排程的基本單位。某一程序中一路單獨執行的程式。

③HandlerThread:一個繼承自Thread的類HandlerThread,Android中沒有對Java中的Thread進行任何封裝,而是提供了一個繼承自Thread的類HandlerThread類,這個類對Java的Thread做了很多便利的封裝。

———————————————————我是分割線—————————————————————

其實這個問題,最主要的關注點還是落在了HandlerThread類上,那麼這個類到底有什麼作用,所謂的便利封裝又體現在哪裡?

觀察HandlerThread的官方文件的兩句:①Thread. Handy class for starting a new thread that has a looper.②The looper can then be used to create handler classes.

釋義:HandlerThread物件start後可以獲得其Looper物件,並且使用這個Looper物件例項Handler,之後Handler就可以執行在其他執行緒中了

———————————————————我是分割線—————————————————————

那麼Handler和Looper到底是什麼關係,為什麼HandlerThread要做這樣的處理?觀看下圖:

                      

Andriod提供了 Handler  和  Looper  來滿足執行緒間的通訊。 Handler 先進先出原則。 Looper 類用來管理特定執行緒內物件之間的訊息交換 (MessageExchange) 。 

1)Looper:  一個執行緒可以產生一個 Looper 物件,由它來管理此執行緒裡的 MessageQueue( 訊息佇列 ) 和對訊息進行迴圈 

2)Handler:  你可以構造 Handler 物件來與 Looper 溝通,以便 push 新訊息到 MessageQueue 裡 ; 或者接收 Looper 從 Message Queue 取出 所送來的訊息。 

3) Message Queue( 訊息佇列 ): 用來存放執行緒放入的訊息。 

4) Message:是執行緒間通訊的訊息載體。兩個碼頭之間運輸貨物,Message充當集裝箱的功能,裡面可以存放任何你想傳遞的訊息。

看到這裡就明白了為什麼:如果一個執行緒要處理訊息,那麼它必須擁有自己的Looper,並不是Handler在哪裡建立,就可以在哪裡處理訊息。

注:對應關係Thread(1):Looper(1):MessageQueen(1):Handler(n).

三、HandlerThread的使用

正如前面所說,執行緒間通訊的時候,比如Android中常見的更新UI,涉及到的是子執行緒和主執行緒之間的通訊,實現方式就是Handler+Looper,但是要自己手動操作Looper,不推薦,所以谷歌封裝了HandlerThread類(類似於AsyncTask類)。

上程式碼,具體實現:

①首先新建HandlerThread並且執行start()

private HandlerThread mHandlerThread;

......

mHandlerThread = new HandlerThread("HandlerThread");

handlerThread.start();

②建立Handler,使用mHandlerThread.getLooper()生成Looper:

final Handler handler = new Handler(mHandlerThread.getLooper()){

            @Override

            public void handleMessage(Message msg) {

                System.out.println("收到訊息");

            }

        };

從上面程式碼可以看出,HandlerThread繼承於Thread,所以它本質就是個Thread。與普通Thread的差別就在於,然後在內部直接實現了Looper的實現,這是Handler訊息機制必不可少的。有了自己的looper,可以讓我們在自己的執行緒中分發和處理訊息。如果不用HandlerThread的話,需要手動去呼叫Looper.prepare()和Looper.loop()這些方法。