Android消息处理机制系列文章整体内容如下
Android消息处理机制1——Handler
Android消息处理机制2——Message
Android消息处理机制3——MessageQueue
Android消息处理机制4——Looper
MessageQueue比较重要的概念是管理消息队列,它的数据结构是一个单向链表。
管理消息队列
一 插入消息
通过boolean enqueueMessage(Message msg, long when)来插入消息
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; //新的message指向原来的链表
mMessages = msg; //含有新的message的单向链表,完成单向链表的插入操作
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; //指向的message
if (p == null || when < p.when) { //p是尾部的message或者要插入的message的when小于指向的message的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的方式
- 从头部插入的条件:
- 当前的消息链表为空
- 当前的消息链表不为空 并且 要插入的message的when属性为0
- 当前的消息链表不为空 并且 要插入的message的when属性不为0 并且 处理message的时间小于当前链表头部message的时间
- 按照处理时间插入
- 不满足从头部插入的条件
二 取消息
只保留取数据的代码
Message next() {
// 省略代码
for (;;) {
synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
//从链表中取出msg的when时间大于now的message
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next; //将从第二个message开始的链表放入mMessages
}
msg.next = null; //断掉原来链表中的第一个message和其他message之间的联系,等于将其从链表中拿出来
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
//取出一个messag然后退出死循环
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
// Process the quit message now that all pending messages have been handled.
if (mQuitting) {
dispose();
return null;
}
}
}
}
三 删除消息
- 根据what删除message
void removeMessages(Handler h, int what, Object object) {
if (h == null) {
return;
}
synchronized (this) {
Message p = mMessages;
// 根据what删除头部message
while (p != null && p.target == h && p.what == what
&& (object == null || p.obj == object)) {
Message n = p.next;
mMessages = n;
p.recycleUnchecked();
p = n;
}
// 根据what删除其他位置的message
while (p != null) {
Message n = p.next;
if (n != null) {
if (n.target == h && n.what == what
&& (object == null || n.obj == object)) {
Message nn = n.next;
n.recycleUnchecked();
p.next = nn;
continue;
}
}
p = n;
}
}
}
- 根据runnalbe删除指定的message
void removeMessages(Handler h, Runnable r, Object object) {
if (h == null || r == null) {
return;
}
synchronized (this) {
Message p = mMessages;
// Remove all messages at front.
while (p != null && p.target == h && p.callback == r
&& (object == null || p.obj == object)) {
Message n = p.next;
mMessages = n;
p.recycleUnchecked();
p = n;
}
// Remove all messages after front.
while (p != null) {
Message n = p.next;
if (n != null) {
if (n.target == h && n.callback == r
&& (object == null || n.obj == object)) {
Message nn = n.next;
n.recycleUnchecked();
p.next = nn;
continue;
}
}
p = n;
}
}
}
- 根据obj删除message
void removeCallbacksAndMessages(Handler h, Object object) {
if (h == null) {
return;
}
synchronized (this) {
Message p = mMessages;
// Remove all messages at front.
while (p != null && p.target == h
&& (object == null || p.obj == object)) {
Message n = p.next;
mMessages = n;
p.recycleUnchecked();
p = n;
}
// Remove all messages after front.
while (p != null) {
Message n = p.next;
if (n != null) {
if (n.target == h && (object == null || n.obj == object)) {
Message nn = n.next;
n.recycleUnchecked();
p.next = nn;
continue;
}
}
p = n;
}
}
}
退出消息队列
void quit(boolean safe) {
//mQuitAllowed是在创建looper的时候设置的状态,是否允许该消息队列退出
if (!mQuitAllowed) {
throw new IllegalStateException("Main thread not allowed to quit.");
}
synchronized (this) {
if (mQuitting) {
return;
}
mQuitting = true;
if (safe) {
//删除所有没有被处理的消息
removeAllFutureMessagesLocked();
} else {
//删除所有的消息,不管该消息有没有被处理
removeAllMessagesLocked();
}
// We can assume mPtr != 0 because mQuitting was previously false.
nativeWake(mPtr);
}
}
删除所有的消息,不管该消息有没有被处理
private void removeAllFutureMessagesLocked() {
final long now = SystemClock.uptimeMillis();
Message p = mMessages;
if (p != null) {
if (p.when > now) {
//头部的message的when值是最小的,如果它都大于now说明该队列里面所有的message的when都大于now,即表示都没有执行
removeAllMessagesLocked();
} else {
//头部message的when等于now或者小于now,但是还在messagequeue里面,说明该message可能正在被处理,可能是处于正在被取出的状态,为了安全则不对其进行写操作,去查看下一个message的when的值,
Message n;
for (;;) {
n = p.next;
if (n == null) {
return;
}
if (n.when > now) {
break;
}
p = n;
}
p.next = null;
do {
p = n;
n = p.next;
p.recycleUnchecked();
} while (n != null);
}
}
}
调用私有方法,循环清空所有的message
private void removeAllMessagesLocked() {
Message p = mMessages;
while (p != null) {
Message n = p.next;
p.recycleUnchecked();
p = n;
}
mMessages = null;
}
(完)
网友评论