天天看點

Android HandlerThread 總結使用

轉載請标明出處:http://www.cnblogs.com/zhaoyanjun/p/6062880.html

本文出自【趙彥軍的部落格】

前言

以前我在 【Android Handler、Loop 的簡單使用】 介紹了子線程和子線程之間的通信。

很明顯的一點就是,我們要在子線程中調用Looper.prepare() 為一個線程開啟一個消息循環,預設情況下Android中新誕生的線程是沒有開啟消息循環的。(主線程除外,主線程系統會自動為其建立Looper對象,開啟消息循環。) Looper對象通過MessageQueue來存放消息和事件。一個線程隻能有一個Looper,對應一個MessageQueue。 然後通過Looper.loop() 讓Looper開始工作,從消息隊列裡取消息,處理消息。

注意:寫在Looper.loop()之後的代碼不會被執行,這個函數内部應該是一個循環,當調用mHandler.getLooper().quit()後,loop才會中止,其後的代碼才能得以運作。

然而這一切都可以用HandlerThread類來幫我們做這些邏輯操作。

HandlerThread

HandlerThread本質上就是一個普通Thread,隻不過内部建立了Looper.

HandlerThread的正常用法

  • 建立一個HandlerThread
mThread = new HandlerThread("handler_thread");
  • 啟動一個HandlerThread
mThread.start();
  • 退出循環

    Looper是通過調用loop方法驅動着消息循環的進行: 從MessageQueue中阻塞式地取出一個消息,然後讓Handler處理該消息,周而複始,loop方法是個死循環方法。

那如何終止消息循環呢?我們可以調用Looper的quit方法或quitSafely方法,二者稍有不同。

/**
     * Quits the looper.
     * <p>
     * Causes the {@link #loop} method to terminate without processing any
     * more messages in the message queue.
     * </p><p>
     * Any attempt to post messages to the queue after the looper is asked to quit will fail.
     * For example, the {@link Handler#sendMessage(Message)} method will return false.
     * </p><p class="note">
     * Using this method may be unsafe because some messages may not be delivered
     * before the looper terminates.  Consider using {@link #quitSafely} instead to ensure
     * that all pending work is completed in an orderly manner.
     * </p>
     *
     * @see #quitSafely
     */
    public void quit() {
        mQueue.quit(false);
    }

    /**
     * Quits the looper safely.
     * <p>
     * Causes the {@link #loop} method to terminate as soon as all remaining messages
     * in the message queue that are already due to be delivered have been handled.
     * However pending delayed messages with due times in the future will not be
     * delivered before the loop terminates.
     * </p><p>
     * Any attempt to post messages to the queue after the looper is asked to quit will fail.
     * For example, the {@link Handler#sendMessage(Message)} method will return false.
     * </p>
     */
    public void quitSafely() {
        mQueue.quit(true);
    }
           

相同點:

将不在接受新的事件加入消息隊列。

不同點

當我們調用Looper的quit方法時,實際上執行了MessageQueue中的removeAllMessagesLocked方法,該方法的作用是把MessageQueue消息池中所有的消息全部清空,無論是延遲消息(延遲消息是指通過sendMessageDelayed或通過postDelayed等方法發送的需要延遲執行的消息)還是非延遲消息。

當我們調用Looper的quitSafely方法時,實際上執行了MessageQueue中的removeAllFutureMessagesLocked方法,通過名字就可以看出,該方法隻會清空MessageQueue消息池中所有的延遲消息,并将消息池中所有的非延遲消息派發出去讓Handler去處理,quitSafely相比于quit方法安全之處在于清空消息之前會派發所有的非延遲消息。

無論是調用了quit方法還是quitSafely方法隻會,Looper就不再接收新的消息。即在調用了Looper的quit或quitSafely方法之後,消息循環就終結了,這時候再通過Handler調用sendMessage或post等方法發送消息時均傳回false,表示消息沒有成功放入消息隊列MessageQueue中,因為消息隊列已經退出了。

需要注意的是Looper的quit方法從API Level 1就存在了,但是Looper的quitSafely方法從API Level 18才添加進來。

小例子:

package com.app;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private HandlerThread myHandlerThread ;
    private Handler handler ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //建立一個線程,線程名字:handler-thread
        myHandlerThread = new HandlerThread( "handler-thread") ;
        //開啟一個線程
        myHandlerThread.start();
        //在這個線程中建立一個handler對象
        handler = new Handler( myHandlerThread.getLooper() ){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //這個方法是運作在 handler-thread 線程中的 ,可以執行耗時操作
                Log.d( "handler " , "消息: " + msg.what + "  線程: " + Thread.currentThread().getName()  ) ;

            }
        };

        //在主線程給handler發送消息
        handler.sendEmptyMessage( 1 ) ;

        new Thread(new Runnable() {
            @Override
            public void run() {
             //在子線程給handler發送資料
             handler.sendEmptyMessage( 2 ) ;
            }
        }).start() ;

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //釋放資源
        myHandlerThread.quit() ;
    }
}


           

運作效果:

/com.app D/handler: 消息: 1  線程: handler-thread
/com.app D/handler: 消息: 2  線程: handler-thread
           

HandlerThread的特點

  • HandlerThread将loop轉到子線程中處理,說白了就是将分擔MainLooper的工作量,降低了主線程的壓力,使主界面更流暢。
  • 開啟一個線程起到多個線程的作用。處理任務是串行執行,按消息發送順序進行處理。HandlerThread本質是一個線程,線上程内部,代碼是串行處理的。
  • 但是由于每一個任務都将以隊列的方式逐個被執行到,一旦隊列中有某個任務執行時間過長,那麼就會導緻後續的任務都會被延遲處理。
  • HandlerThread擁有自己的消息隊列,它不會幹擾或阻塞UI線程。
  • 對于網絡IO操作,HandlerThread并不适合,因為它隻有一個線程,還得排隊一個一個等着。

參考文章:

Android Handler、Loop 的簡單使用

Android 如何有效的解決記憶體洩漏的問題

Android 更新UI的幾種方式

繼續閱讀