android src 4.0,Looper.java文件:
/**
* Run the message queue in this thread. Be sure to call quit() to end the loop
*/
public static void loop() {
Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process, and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
while (true) {
Message msg = queue.next(); // might block
if (msg != null) {
if (msg.target == null) { // target为null,结束消息,结束循环
// No target is a magic identifier for the quit message.
return;
}
}
}
private Looper() {
mQueue = new MessageQueue();
mRun = true;
mThread = Thread.currentThread();
}
public void quit() {
Message msg = Message.obtain();
// NOTE: By enqueueing directly into the message queue, the message is left with a null target. This is how we know it is a quit message
// 发送一个target为null的消息,作为结束
mQueue.enqueueMessage(msg, 0);
}
经测试,如果使用Looper.prepare()、Looper.loop()为Thread添加消息队列后,并启动thread;
在不需要该thread中创建的Handler时,要调用looper.quit(),结束消息队列,进而结束线程。
如果不这么做,thread会长时间存在不销毁。
前段时间,碰到一个问题:压力测试某个模块,一段时间后,总是crash,tombstones报的堆栈信息中,线程数量达上百个;排查后发现,是滥用HandlerThread导致的,把它等同与Thread来用了,使用完成后也没有调用quit进行销毁,导致线程数量一直在增加。
android 4.1.2 Looper.java:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/os/Looper.java#Looper.quit%28%29