Android提供了一个封装好的带有looper的线程类,即为HandlerThread。 本质就是个Thread。Thread线程是一次性消费品,当Thread线程执行完一个耗时的任务之后,线程就会被自动销毁了。如果我们需要一个工作者线程,而不是把它当作一次性消耗品,那么就可以使用HandlerThread。
Android 5.1中HandlerThread源码分析
/**
* 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.
*/
/**
*该工具类用于创建一个带Looper循环的线程,Looper对象用于创建Handler对象,
*注意必须调用start()方法启动线程。
*/
public class HandlerThread extends Thread {
//线程优先级
int mPriority;
//当前线程id
int mTid = -1;
//当前线程持有的与Handler关联的Looper对象
Looper mLooper;
//构造方法
public HandlerThread(String name) {
//调用父类默认的方法创建线程
super(name);
//设置为默认应用的优先级
mPriority = Process.THREAD_PRIORITY_DEFAULT;
}
/**
* Constructs a HandlerThread.
* @param name
* @param priority The priority to run the thread at. The value supplied must be from
* {@link android.os.Process} and not from java.lang.Thread.
*/
//带优先级参数的构造方法
public HandlerThread(String name, int priority) {
//调用父类默认的方法创建线程
super(name);
mPriority = priority;
}
/**
* Call back method that can be explicitly overridden if needed to execute some
* setup before Looper loops.
*/
//可重写的回调方法,Looper.loop之前在线程中需要处理的其他逻辑在这里实现。
protected void onLooperPrepared() {
}
@Override
//HandlerThread线程的run方法
public void run() {
//获得当前线程的id
mTid = Process.myTid();
//准备循环条件
//创建Looper对象,先调用Looper.prepare()方法,才能创建Handler对象。
Looper.prepare();
//持有锁机制来获得当前线程的Looper对象
synchronized (this) {
mLooper = Looper.myLooper();
//调用notifyAll通知其他可能block在当前对象上的线程,这里主要是唤醒getLooper方法中的wait。
notifyAll();
}
//设置当前线程的优先级
Process.setThreadPriority(mPriority);
//该方法作用就是在线程循环之前做一些准备工作,当然子类也可以不实现。
onLooperPrepared();
//建立了消息循环
Looper.loop();
// 重置为非法值
mTid = -1;
}
/**
* This method returns the Looper associated with this thread. If this thread not been started
* or for any reason is isAlive() returns false, this method will return null. If this thread
* has been started, this method will block until the looper has been initialized.
* @return The looper.
*/
/*
* 该方法返回当前HandlerThread线程中相关联的mLooper对象。
* 首先判断当前线程是否存活,如果不是存活的,这直接返回null。其次如果当前线程存活的,在判断线程的成员变量mLooper是否为null,
* 如果为null,说明当前线程已经创建成功,但是还没来得及创建Looper对象,因此,这里会调用wait方法去等待,
* 当run方法中的notifyAll方法调用之后通知当前线程的wait方法等待结束,跳出循环,获得mLooper对象的值。
*/
public Looper getLooper() {
//如果线程不是alive状态,则直接返回null
if (!isAlive()) {
return null;
}
// If the thread has been started, wait until the looper has been created.
//同步代码块,正好和上面run方法中同步块对应
//一直等待,直到mLooper被设置成有效值了才退出while
synchronized (this) {
while (isAlive() && mLooper == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
//最后返回mLooper
return mLooper;
}
/**
* Quits the handler thread's looper.
* <p>
* Causes the handler thread's looper 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>
*
* @return True if the looper looper has been asked to quit or false if the
* thread had not yet started running.
*
* @see #quitSafely
*/
/*
*
*/
public boolean quit() {
// 注意这里是调用getLooper而不是直接使用mLooper,
Looper looper = getLooper();
if (looper != null) {
//退出消息循环
looper.quit();
return true;
}
return false;
}
/**
* Quits the handler thread's looper safely.
* <p>
* Causes the handler thread's looper to terminate as soon as all remaining messages
* in the message queue that are already due to be delivered have been handled.
* Pending delayed messages with due times in the future will not be delivered.
* </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>
* If the thread has not been started or has finished (that is if
* {@link #getLooper} returns null), then false is returned.
* Otherwise the looper is asked to quit and true is returned.
* </p>
*
* @return True if the looper looper has been asked to quit or false if the
* thread had not yet started running.
*/
//安全退出循环
public boolean quitSafely() {
Looper looper = getLooper();
if (looper != null) {
//退出消息循环
looper.quitSafely();
return true;
}
return false;
}
/**
* Returns the identifier of this thread. See Process.myTid().
*/
public int getThreadId() {
return mTid;
}
}
分析源码后可以得知:
1. HandlerThread操作了一个Looper,提供getLooper()方法把一个Looper对象给新创建的Handler对象,使得Handler处理消息事件在子线程中处理。
2. 创建Handler之前必须先调用HandlerThread线程的start方法,因为创建Handler需要的Looper参数是在HandlerThread的run方法中创建。
3. HandlerThread是工作者线程(循环线程)。如果你只是要做某些后台的操作(短暂的,下载文件),而不需要更新UI的话,那你可以优先使用HandlerThread而不是AsyncTask。
HandlerThread的使用
1. 创建HandlerThread实例对象
HandlerThread mHandlerThread = new HandlerThread("handlerThread");
参数的作用主要是标记当前线程的名字。
2. 启动HandlerThread线程
mHandlerThread.start();
3. 构建循环消息处理机制
将当前mHandlerThread子线程的Looper传入mThreadHandler,使得
mThreadHandler的消息队列在子线程中执行
Handler mThreadHandler= new Handler(mHandlerThread.getLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
//....
return true;
}
});
handlerMessage方法来处理耗时任务。
扩展:Message、Handler、MessageQueue 和Looper
Android 中的异步消息处理主要由四个部分组成,Message、Handler、MessageQueue 和
Looper。
- Message
Message 是在线程之间传递的消息,它可以在内部携带少量的信息,用于在不同线程之间交换数据。 - Handler
Handler 顾名思义也就是处理者的意思,它主要是用于发送和处理消息的。发送消息一般是使用 Handler 的 sendMessage()方法,而发出的消息经过一系列地辗转处理后,最终会传递到 Handler 的 handleMessage()方法中。 - MessageQueue
MessageQueue 是消息队列的意思,它主要用于存放所有通过 Handler 发送的消息。这部分消息会一直存在于消息队列中,等待被处理。每个线程中只会有一个 MessageQueue对象。 - Looper
Looper 是每个线程中的 MessageQueue 的管家,调用 Looper 的 loop()方法后,就会进入到一个无限循环当中,然后每当发现 MessageQueue 中存在一条消息,就会将它取出, 并传递到 Handler 的 handleMessage()方法中。 每个线程中也只会有一个 Looper 对象。
参考:
http://blog.csdn.net/guolin_blog/article/details/9991569
http://blog.csdn.net/feiduclear_up/article/details/46840523