public class HandlerThread extends Thread {
可以知道HandlerThread其实也是一个Thread,
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;
}
初始化
@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,首先通过Looper.prepare开启消息循环,再通过Looper.loop进行消息抽取
关于handler looper threadlocal messagequene之前的关系可以参考
http://blog.youkuaiyun.com/eandroidhu/article/details/50914010