官方注释:
MessageQueue是保存消息列表的低级别类,消息由Looper对象派发。消息并不是直接添加到MessageQueue中的,而是通过与Looper对象关联的MessageQueue.IdleHandler对象添加。
调用Looper.myQueue方法可以获取当前线MessageQueue。
参数解释:
private int mPtr; // 使用本机code
表示MessageQueue是否允许退出
mQuitAllowed
存放IdleHandler对象的一个ArrayList
private final ArrayList<IdleHandler> mIdleHandlers = new
ArrayList<IdleHandler>();
一个IdleHandler数组
private IdleHandler[] mPendingIdleHandlers;
判断Thread是否退出
private boolean mQuitting;
是否被阻塞
private boolean mBlocked;
下一个障碍记号???
private int mNextBarrierToken;
jni调底层的c
private native static long nativeInit();
private native static void nativeDestroy(long ptr);
private native static void nativePollOnce(long ptr, int timeoutMillis);
private native static void nativeWake(long ptr);
private native static boolean nativeIsIdling(long ptr);
IdleHandler在Handler空闲时执行,好处在于可以不用指定一个将来时间,只要线程空闲了,就可以执行它指定的操作。比较适合那种需要在将来执行操作,但是又不知道需要指定多少延迟时间的操作
public static interface IdleHandler {
boolean queueIdle();
}
添加和删除IdleHandler的方法
addIdleHandler(IdleHandler handler)
removeIdleHandler(IdleHandler handler)
创建消息队列
MessageQueue(boolean quitAllowed) {
mQuitAllowed = quitAllowed;
mPtr = nativeInit();//通过native方法初始化消息队列
}
对象销毁时,该方法finalize()被自动调用。主要是通过native方法销毁前创建的nativeMessageQueue方法
protected void finalize() throws Throwable {
try {
dispose();
} finally {
super.finalize();
}
}
private void dispose() {
if (mPtr != 0) {
nativeDestroy(mPtr);
mPtr = 0;
}
}
//next方法会取出下一个Message(从头部取),如果没有Message可以处理,就可以处理下IdleHandler
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
return null;
}
int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
//等待刷新
Binder.flushPendingCommands();
}
//调用native层进行消息标示 0立即返回 -1 等待
//一:是当消息队列中没有消息时线程进入等待,二:消息指定了时间,而现在还没有到这个时间
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) {
// 试图获取下一条消息。如果发现返回。
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
//寻找下一个异步消息队列中
if (msg != null && msg.target == null) {
do {
prevMsg = msg;
msg = msg.next;
//(flags & FLAG_ASYNCHRONOUS) != 0; //FLAG_ASYNCHRONOUS = 1 << 1;
} while (msg != null && !msg.isAsynchronous());
}
if (msg != null) {
if (now < msg.when) {
// 下一个消息没有准备好。设置一个超时时间作为下次休眠的依据
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (false) Log.v("MessageQueue", "Returning message: " + msg);
return msg;
}
} else {
// 没有更多信息
nextPollTimeoutMillis = -1;
}
// 如果还有消息没处理,交给底层处理
if (mQuitting) {
dispose();
return null;
}
//?????
if (pendingIdleHandlerCount < 0
&& (mMessages == null || now < mMessages.when)) {
pendingIdleHandlerCount = mIdleHandlers.size();
}
if (pendingIdleHandlerCount <= 0) {
// No idle handlers to run. Loop and wait some more.
mBlocked = true;//阻塞
continue;
}
if (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
}
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
//消息处理完毕后,是否还保持活跃
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf("MessageQueue", "IdleHandler threw exception", t);
}
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}
总结:
每个Message之间是串连在一起的,Message只要知道自己的前面一个Message和后面一个Message就可以了,Message之间的排序通过用时间戳来排序,时间戳小的排在前面。
再配合上handler对于消息的取出,第一个Message的时间到了,就取队列的第一个Message,取完之后,将第一个Message置空,这样第二个Message就排在第一个了,依此类推。
遗留问题:
1.pendingIdleHandlerCount < 0 完后为什么还有一个
pendingIdleHandlerCount <= 0 的判断,为什么这样写?
2.if (false) Log.v 这个false的写法奇怪?
3.为什么一定要用jni来处理?