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的幾種方式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM