为何要用HandlerThread,怎么用?

HandlerThread是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.ide

意思就是说HandlerThread帮咱们建立好了Looper。
咱们都知道,Android主线程的Looper是自动建立的,其余线程是没有建立Looper的,须要咱们本身建立。通常作法很简单oop

@Override 
public void run() {
    Looper.prepare();
    Looper.loop();
}

prepare()loop()两个方法再也不赘述,咱们先来看一个不用HandlerThread的例子:ui

Thread newThread = new Thread(new Runnable()
  {    
    @Override    
    public void run() {        
      Looper.prepare();        
      Looper.loop();    
    }
  });
newThread.start();
Handler handler = new Handler(newThread.getLooper());

相信很多人会用上面的方式建立一个异步线程的Handler,有没有问题呢?确定有。
newThread的looper是在这个线程运行以后建立的,因此,当执行到Handler handler = new Handler(newThread.getLooper());的时候,newThread的looper可能尚未建立好!
****这就是为何咱们须要HandlerThread,并不单单是由于它帮咱们建立了一个looper,更重要的是它为咱们处理了这个异步问题。****
来看下HandlerThread的实现:this

@Override
public void run() {    
  mTid = Process.myTid();    
  Looper.prepare();    
  synchronized (this) {        
    mLooper = Looper.myLooper();        
    notifyAll();    
  }    
  Process.setThreadPriority(mPriority);    
  onLooperPrepared();    
  Looper.loop();    
  mTid = -1;
}

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;
}

简单的一个加锁帮咱们作了最重要的事情。线程

有人问我在run()方法里面,mTid开始赋值Process.myTid(),为何后来又复制-1了呢?仔细想一下就有答案了,由于Looper.loop()是个死循环啊,执行到mTid = -1的时候,就是looper退出的时候。code


插一句,这个mTid是干吗的?生命周期

/** * Returns the identifier of this thread. See Process.myTid(). */
public int getThreadId() {    
  return mTid;
}

Thread里面是没有getThreadId()方法的,Process.myTid()方法定义以下:开发

/** * Returns the identifier of the calling thread, which be used with * {@link #setThreadPriority(int, int)}. */
public static final int myTid() {    
  return Libcore.os.gettid();
}

原来tid是修改线程优先级、调度策略时用来作线程惟一标识的。那么在HandleThread中,把mTid置为-1是几个意思?笔者的理解是,HandlerThread自己是为looper服务的,looper终止之后,线程也会立刻终止,为防止开发者错误使用,因此将mTid置为-1。文档


关于HandlerThread另一个容易忽略的问题就是退出Looper。Looper经过quit()quitSafely()方法退出(记得,Looper.loop()以后是一个死循环),看看Looper的loop()方法的注释

/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. */
public static void loop()

Looper使用完毕执行quit自己就是一件容易忽略的事情,若是放到HandlerThread中,更是容易忘得一干二净。因此HandlerThread为咱们提供了两个相同的方法:

public boolean quit(){}
public boolean quitSafely(){}

****不过说到底,维护Looper的生命周期仍是咱们每一个开发者本身的事情,HandlerThread只不过是封装了一下,帮咱们处理一个异步问题罢了。****

相关文章
相关标签/搜索