HandlerThread源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* 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 {
.....
}
public HandlerThread(String name) {
super(name);
mPriority = Process.THREAD_PRIORITY_DEFAULT;
}

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

其实我们初始化和启动了一个线程,既然是线程那就看 run 方法, run 方法中创建了 Looper 对象,然后设置线程等级,开启循环 loop() .

-------------本文结束感谢您的阅读-------------