消息通信是Android系统中使用相当普遍的一种线程间通信方式。既然是线程间的通信,就一定存在共享的对象,一定需要处理线程间的同步。本文对Handler、Looper和MessageQueue所实现的消息通信机制做深入的剖析。
图1. Android消息机制的框架
一、Handler
Handler用于发送和处理Message和Runnable对象(两者统称为消息),即Handler既是消息的生产者,也是消息的消费者。Handler会发送消息到MessageQueue中,在Looper遍历MessageQueue、执行到该消息时,会回掉Message的处理函数,即Handler类的handleMessage(Message msg)。
(1) Handler的设计
1.1 Handler类的几个重要的成员变量
//通过Handler的Constructor的参数获得,如果Constructor没有指定Looper,
//则使用当前线程的Looper(通过Looper.myLooper())。
final Looper mLooper;
//MessageQueue的获取依赖于mLooper,mQueue=mLooper。
final MessageQueue mQueue;
//通过Constructor传入,如果Constructor中没有指定,则为null。
final Callback mCallback;
//通过Constructor传入,表示是否异步发送,如果Constructor没有指定,则默认false。
final boolean mAsynchronous;
1.2 Handler类的构造方法:
public Handler();
public Handler(Callback callback);
public Handler(Callback callback, boolean async);
public Handler(boolean async);
public Handler(Looper looper);
public Handler(Looper looper, Callback callback);
public Handler(Looper looper, Callback callback, boolean async);
(2)Handler提供的发送消息的接口:
public final boolean sendMessage(Message msg);
public final boolean sendEmptyMessage(int what);
public final boolean sendEmptyMessageDelayed(int what, long delayMillis);
public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis);
public final boolean sendMessageDelayed(Message msg, long delayMillis);
public boolean sendMessageAtTime(Message msg, long uptimeMillis);
所有的发送消息接口最终都会调用:
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis);
插入到MessageQueue队列中。
二、Looper
1. Looper类的设计
public final class Looper {
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
private static Looper sMainLooper;
final MessageQueue mQueue;
final Thread mThread;
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
return;
}
msg.target.dispatchMessage(msg);
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
//...
}
msg.recycleUnchecked();
}
}
}
2. 对Looper类的思考
Looper类不停的从MessageQueue中取消息,并通过msg.target.dispatchMessage(msg)分发给源Handler,源Handler就是该消息的发送者,直到MessageQueue为空为止。如果MessageQueue为空,则looper()方法阻塞在queue.next()处。
Handler对象的handleMessage()在哪个线程执行,不取决于该Handler对象由哪个线程构建,而是取决于该Handler对象所绑定的Looper属于哪个线程。
三、MessageQueue
MessageQueue维护了一个Message的单链表,对Message的进、出进行管理。只要Handler对象是在本线程内创建的,就可以往MessageQueue中发送消息,因此进、出时都需要同步。
在MessageQueue中将Message按照时间戳(msg.when)进行排序。出队列时,使用当前时间戳与msg.when进行比较,如果当前时间戳> msg.when,则马上将该消息出队;如果当前时间戳< msg.when,则利用epoll_wait设置定时器,时间为Math.min(msg.when - now, Integer.MAX_VALUE),等待定时器时间到了以后将消息出队。