再次看Handler源码有了新的理解和认识,个人总结的可能有不当之处。
一、Message主要的几个常见属性是what、object、arg1,这几个是在使用时常遇到的,还有几个源码级的属性when(long)、target(Handler)、next(Message),这几个属性关联到四兄弟的工作机制。
首先when是接收handler.sendMessageDelay的delay时间的,一步一步跟进去最终将delay+uptimeMillis赋值给when,而next是Mesaage类型也说明了Message是一个链表结构,便于队列排序:
以sendEmptyMessageDelayed为例
public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
Message msg = Message.obtain();
msg.what = what;
return sendMessageDelayed(msg, delayMillis);
}
里面其实也是调用的sendMessageDelayed,只是消息只给了what赋值,
public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
return false;
}
return enqueueMessage(queue, msg, uptimeMillis);
}
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
在Handler中最后调用的是MessageQueue中的enqueueMessage方法,将延迟时间传入。然后看MessageQueue中代码:
boolean enqueueMessage(Message msg, long when) {
synchronized (this) {
msg.markInUse();
msg.when = when;
Message p = mMessages;
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.这里给链表一个新head,如果阻塞需要唤醒
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
// Inserted within the middle of the queue. Usually we don't have to wake
// up the event queue unless there is a barrier at the head of the queue
// and the message is the earliest asynchronous message in the queue.找到合适位置插进去
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) {
prev = p;
p = p.next;
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
// We can assume mPtr != 0 because mQuitting is false.
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
在这里将时间赋值给Message的when,when的作用是啥呢?就是在发出消息后,按照when进行排序,排成有序链表等待处理。因为Message其实是一个链表结构,下一个消息是当前消息的next,源码中两个关键判断,如果新进来的消息的when<p.when,那么新消息要先被处理,就作为新的链表head,msg.next = p; mMessages = msg;其中p就是已经存在的消息,赋值给新消息的next,再将新消息赋值给当前消息;否则,将会for循环遍历当前消息的next,比较when的值,直到找到适合新消息的位置,将其插入。
总之,延迟消息的发送并不是在处理的时候延迟的,而是在放入消息队列之前就按照各自延迟时间,找到自己的位置,排序好,等待处理。
然后是target属性,它的类型是Handler,在发送消息的方法中,Message的target被赋值为this,也就是当前发送消息的Handler。
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
一个线程之中可以有多个Handler,只有一个Looper,那多个Handler发送消息到队列中,处理的时候怎么区分,就是这个target起到的作用,在Looper的loop()方法中,遍历队列中的消息进行分发,谁的消息分给谁处理。
try {
msg.target.dispatchMessage(msg);
dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
二、MessageQueue消息队列,其中有几个关键点,一个是消息变量mMessages(Message),一个是native方法
private native static void nativeWake(long ptr);
先看mMessages,它的赋值是在Handler发送消息,调用了MessageQueue中的方法中,
boolean enqueueMessage(Message msg, long when) {
synchronized (this) {
msg.markInUse();
msg.when = when;
Message p = mMessages;
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
}
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
新消息进来时,如果mMessages为空,就将新消息赋值给它;不为空但新消息when==0,将新消息作为新的head,其next是旧的mMessages,新消息就成为了新的mMessages。(熟悉链表结构就明白其中道理了)
再看nativeWake()方法,是一个native方法,这里与Looper中loop()方法中调用queue.next()可能会阻塞线程有关,当遍历队列没有消息时,线程会休眠,而在有消息发送进来时,会唤醒。猜测应该是这里起的作用。找不到nativeWake的方法没有验证。
三、Looper,如何初始化,如何跟线程绑定?
首先,主线程ActivityThread的main中已经做了Looper的初始化以及遍历操作,如果新建子线程需要手动初始化以及遍历。
* <pre>
* 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();
* }
* }</pre>
*/这是Looper注释内容,标明了新建线程Looper的使用方法
prepare()方法中先通过sThreadLocal.get(),获取当前线程的Looper,如果存在,就会报错,因为一个线程只能初始化一个Looper;如果没有Looper会new一个出来,并且set到sThreadLocal中。实例化一个Handler用来发送处理消息,最后调用loop方法,遍历消息。
那Looper如何跟线程绑定呢?主要在于Looper中有个ThreadLocal,而ThreadLocal中有个类叫ThreadLocalMap,
static class ThreadLocalMap {
/**
* The entries in this hash map extend WeakReference, using
* its main ref field as the key (which is always a
* ThreadLocal object). Note that null keys (i.e. entry.get()
* == null) mean that the key is no longer referenced, so the
* entry can be expunged from table. Such entries are referred to
* as "stale entries" in the code that follows.
*/
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
可以看到它的key是ThreadLocal,value是个Object其实存放的是Looper。
Looper的prepare方法如下
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));
}
先找set方法,传入的是一个new出来的Looper,
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
这段代码先拿到当前线程,再通过当前线程获取线程的ThreadLocalMap,
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
在Thread类中有维护此变量,赋值是在createMap中
ThreadLocal.ThreadLocalMap threadLocals = null;
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
拿到ThreadLocalMap如果不为空,就将此ThreadLocal与Looper以key-value存在map中,为何就能和线程绑定呢,因为一个线程只有一个Looper,而一个Looper维护了一个静态ThreadLocal。
再看get方法,
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
当然是通过当前线程拿到map,通过当前ThreadLocal作为key找到对应的Looper。
总的来说,prepare方法做了两件大事:一个是检查此线程是否已经有一个Looper,是抛异常;一个是new一个Looper并set到ThreadLocal绑定线程。
四、Handler,主要工作是发送消息,接受并处理消息,其它中间工作都有三兄弟做了。