今天继续异步消息机制,复习一下handlerThread;
1.产生背景:
我们知道,在Android开发中,我们经常会遇到耗时操作,最常见的就是网络访问,这时候,我们通常都会开启一个新的线程来进行耗时任务,非常方便,但是却也非常消耗性能,因为耗时操作可能会有多个,当你这个耗时操作完成之后,线程被销毁,新的耗时任务又来了,你又需要去创建线程,所以,多次的创建和销毁线程是非常消耗性能的,在之前的handler复习中,我们就知道了,handler只能创建在主线程中,因为不论是sendMessage()还是post()方法,都需要一个MessageQueue去存储消息,但是在子线程中默认是不会开启Looper轮循器的,而每一个MessageQueue都需要一个Looper去管理,所以,如果你想在子线程使用handler,那么就必须手动的开启looper轮循器;这样会很麻烦,所以Google给我们封装了HandlerThread。
2.概念:
handlerThread可以这样分解,handlerThread=handler+Thread+looper;就相当于Thread中维护了一个looper轮循器,可以进行looper循环;
3.特点:
(1)通过获取HandlerThread的looper对象传递给Handler对象,可以在handlerMessage方法中执行异步任务;
(2)优点是不会有阻塞,减少了对性能的消耗,缺点是不能同时进行多任务的处理,需要等待处理;
(3)与线程池注重并发不同,HandlerThread是一个串行队列,HandlerThread背后只有一个线程;
4.源码解析:
HandlerThread的源码很少,就160多行,少而精,写的非常经典;
/**
* 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.
*/
public class HandlerThread extends Thread {
从这里就可以看出,其实HandlerThread继承了Thread,注释的意思说的很明确了,这个Thread不同于其他Thread,在HandlerThread中是可以创建handler的;
既然是Thread,那么最重要的方法,当然就是它的run方法:
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
可以看到,run方法中,先是通过Looper.prepare()方法去创建线程,然后,在下面做了同步代码,保证了线程安全,之前说过HandlerThread的一个特点就是串行队列,就是如果一个线程没有执行完,那么looper就会处于阻塞状态,直到执行完之后才会执行下一个线程任务,这是怎么做到的呢,奥秘就在他的同步代码块中,其中的notifyAll()方法,就是表示了通知线程开始进入就绪状态;
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;
}
它通知的就是getLooper()方法,在这个方法中,如果looper轮循器还在工作,那么就会让线程等待,直到run方法中notifyall()方法通知到之后,才会返回Looper。