亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Thread、Handler和HandlerThread關(guān)系詳解

 更新時間:2016年09月19日 09:26:23   作者:SillyMonkey  
這篇文章主要介紹了Thread、Handler和HandlerThread關(guān)系詳解的相關(guān)資料,需要的朋友可以參考下

前言

前幾天看到一道面試題:Thread、Handler和HandlerThread有什么區(qū)別?,這個題目有點意思,對于很多人來說,可能對Thread和Handler很熟悉,主要涉及到Android的消息機制(Handler、Message、Looper、MessageQueue),詳見《 從Handler.post(Runnable r)再一次梳理Android的消息機制(以及handler的內(nèi)存泄露)》

但是這個HandlerThread是拿來做什么的呢?它是Handler還是Thread?我們知道Handler是用來異步更新UI的,更詳細的說是用來做線程間的通信的,更新UI時是子線程與UI主線程之間的通信。那么現(xiàn)在我們要是想子線程與子線程之間的通信要怎么做呢?當(dāng)然說到底也是用Handler+Thread來完成(不推薦,需要自己操作Looper),Google官方很貼心的幫我們封裝好了一個類,那就是剛才說到的:HandlerThread。(類似的封裝對于多線程的場景還有AsyncTask)

使用方法

還是先來看看HandlerThread的使用方法:
首先新建HandlerThread并且執(zhí)行start()

private HandlerThread mHandlerThread;
......
mHandlerThread = new HandlerThread("HandlerThread");
handlerThread.start();

創(chuàng)建Handler,使用mHandlerThread.getLooper()生成Looper:

 final Handler handler = new Handler(mHandlerThread.getLooper()){
   @Override
   public void handleMessage(Message msg) {
    System.out.println("收到消息");
   }
  };

然后再新建一個子線程來發(fā)送消息:

 new Thread(new Runnable() {
   @Override
   public void run() {
    try {
     Thread.sleep(1000);//模擬耗時操作
     handler.sendEmptyMessage(0);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }).start();

最后一定不要忘了在onDestroy釋放,避免內(nèi)存泄漏:

 @Override
 protected void onDestroy() {
  super.onDestroy();
  mHandlerThread.quit();
 }

執(zhí)行結(jié)果很簡單,就是在控制臺打印字符串:收到消息

原理

整個的使用過程我們根本不用去關(guān)心Handler相關(guān)的東西,只需要發(fā)送消息,處理消息,Looper相關(guān)的東西交給它自己去處理,還是來看看源碼它是怎么實現(xiàn)的,先看構(gòu)造方法:

public class HandlerThread extends Thread {}

HandlerThread其實還是一個線程,它跟普通線程有什么不同?

public class HandlerThread extends Thread {
 int mPriority;
 int mTid = -1;
 Looper mLooper;

 public HandlerThread(String name) {
  super(name);
  mPriority = Process.THREAD_PRIORITY_DEFAULT;
 }
 ......
}

答案是多了一個Looper,這個是子線程獨有的Looper,用來做消息的取出和處理。繼續(xù)看看HandlerThread這個線程的run方法: 

 protected void onLooperPrepared() {
 }
 @Override
 public void run() {
  mTid = Process.myTid();
  Looper.prepare();
  synchronized (this) {
   mLooper = Looper.myLooper();//生成Looper
   notifyAll();
  }
  Process.setThreadPriority(mPriority);
  onLooperPrepared();//空方法,在Looper創(chuàng)建完成后調(diào)用,可以自己重寫邏輯
  Looper.loop();//死循環(huán),不斷從MessageQueue中取出消息并且交給Handler處理
  mTid = -1;
 }

主要就是做了一些Looper的操作,如果我們自己使用Handler+Thread來實現(xiàn)的話也要進行這個操作,再來看看getLooper()方法:   

public Looper getLooper() {
  if (!isAlive()) {
   return null;
  }

  // If the thread has been started, wait until the looper has been created.
  synchronized (this) {
   while (isAlive() && mLooper == null) {
    try {
     wait();
    } catch (InterruptedException e) {
    }
   }
  }
  return mLooper;
 }

方法很簡單,就是加了個同步鎖,如果已經(jīng)創(chuàng)建了(isAlive()返回true)但是mLooper為空的話就繼續(xù)等待,直到mLooper創(chuàng)建成功,最后看看quit方法,值得一提的是有兩個:

 public boolean quit() {
  Looper looper = getLooper();
  if (looper != null) {
   looper.quit();
   return true;
  }
  return false;
 }
 public boolean quitSafely() {
  Looper looper = getLooper();
  if (looper != null) {
   looper.quitSafely();
   return true;
  }
  return false;
 }

quitSafely是針對在消息隊列中還有消息或者是延遲發(fā)送的消息沒有處理的情況,調(diào)用這個方法后都會被停止掉。

總結(jié)

HandlerThread的使用方法還是比較簡單的,但是我們要明白一點的是:如果一個線程要處理消息,那么它必須擁有自己的Looper,并不是Handler在哪里創(chuàng)建,就可以在哪里處理消息的。

如果不用HandlerThread的話,需要手動去調(diào)用Looper.prepare()和Looper.loop()這些方法。

以上就是對Thread、Handler和HandlerThread關(guān)系 的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!

相關(guān)文章

最新評論