// p为队列中头部msg
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
// 把当前 msg 添加到链表的第一个
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
// 不是第一次添加数据,并且 msg 的时间 大于 mMessages(头指针) 的时间
// 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 插入到列表中
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}