handle:是发送消息,处理消息
looper:是轮询消息
messageQueen:消息队列
程序的启动有个主线程,也就是我们说的ui线程,ActivityThread线程有个main方法,在里面调用了looper的启动。
ActivityThread:
public static void main(String[] args) {
...
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
}
// End of event ActivityThreadMain.
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
ui线程是系统给我们写了looper的创建,但是我们自己写的线程是没有开启消息循环的,必须要自己调用Looper.prepare(), 然后调用Looper.loop();
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
Looper是如何与线程绑定的呢?
ThreadLocal<Looper>, Looper.prepare方法里面,我们看到ThreadLocal.set方法将looper传递到Threadlocal里面,这个里面存在一个类似于map结构的model,将looper与thread作为<k,value>传递进去。
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
handle,looper与messagequeue是如何联系的:
handle的构造方法里面存在
public Handler(Callback callback, boolean async) {
....
mLooper = Looper.myLooper(); //获取到当前线程里面的looper。
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue; //messagequeue在looper里面new出来的,可以直接获取
mCallback = callback;
mAsynchronous = async;
}
Looper里面:
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}