publicstaticvoidloop(){ final Looper me = myLooper(); if (me == null) { thrownew RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } final MessageQueue queue = me.mQueue;
for (;;) { Message msg = queue.next(); // might block if (msg == null) { // No message indicates that the message queue is quitting. // 当queue.next为空时,跳出循环. 但是,实际上当queue为空时,并不会返回null,而是一直阻塞在next()方法中. // 只有调用quit方法时,next()方法再能真正的返回null return; } try { // msg的target为Handler, 这里是handler中handleMessage()或者callback具体调用的地方 msg.target.dispatchMessage(msg); } finally { ... // 省略代码 } msg.recycleUnchecked(); } }
2. MessageQueue
关键成员变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// mPtr是native的MessageQueue的指针 privatelong mPtr; // used by native code // mMessages是消息队列的存储, 数据结构是一个单链表. next属性指向下一个message Message mMessages;
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. finallong ptr = mPtr; if (ptr == 0) { returnnull; }
int pendingIdleHandlerCount = -1; // -1 only during first iteration int nextPollTimeoutMillis = 0; for (;;) { if (nextPollTimeoutMillis != 0) { Binder.flushPendingCommands(); }
synchronized (this) { // Try to retrieve the next message. Return if found. finallong now = SystemClock.uptimeMillis(); Message prevMsg = null; // 获取链表的头结点,即第一个message Message msg = mMessages; // 判断msg是否为同步栅栏 // 表示同步栅栏的msg, 其msg.target为null if (msg != null && msg.target == null) { // Stalled by a barrier. Find the next asynchronous message in the queue. // 寻找队列中第一个异步message do { prevMsg = msg; msg = msg.next; } while (msg != null && !msg.isAsynchronous()); }
// 处理 msg 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 { // Got a message. mBlocked = false; if (prevMsg != null) { prevMsg.next = msg.next; } else { mMessages = msg.next; } msg.next = null; return msg; } } else { // No more messages. nextPollTimeoutMillis = -1; }
// msg为null, 有可能是消息队列为空, 有可能是msg的时间没有到
// Process the quit message now that all pending messages have been handled. // 判断是否需要结束循环 if (mQuitting) { // dispose()方法做销毁工作, 会销毁native层的队列, 并把mPtr置0 // 只有在这里next()方法才会返回null dispose(); returnnull; }
// If first time idle, then get the number of idlers to run. // Idle handles only run if the queue is empty or if the first message // in the queue (possibly a barrier) is due to be handled in the future. // 当获取到的message为空, 或者message的执行时间没有到 // 当前没有msg可以处理的,开始处理 idleHandler if (pendingIdleHandlerCount < 0 && (mMessages == null || now < mMessages.when)) { pendingIdleHandlerCount = mIdleHandlers.size(); } if (pendingIdleHandlerCount <= 0) { // No idle handlers to run. Loop and wait some more. // 走到这里表示该线程需要阻塞一段时间, 阻塞时间为nextPollTimeoutMillis 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. // 在消息队列空闲时, 遍历idleHandler集合, 执行idleHandler的相关方法 for (int i = 0; i < pendingIdleHandlerCount; i++) { final IdleHandler idler = mPendingIdleHandlers[i]; mPendingIdleHandlers[i] = null; // release the reference to the handler
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; } }
publicstaticinterfaceIdleHandler{ /** * Called when the message queue has run out of messages and will now * wait for more. Return true to keep your idle handler active, false * to have it removed. This may be called if there are still messages * pending in the queue, but they are all scheduled to be dispatched * after the current time. */ // queueIdle()返回值为false, 表示这个IdleHandler仅仅执行一次就自动销毁; 返回为true, 表示可以执行多次 booleanqueueIdle(); }
booleanenqueueMessage(Message msg, long when){ ... // 省略代码
synchronized (this) { if (mQuitting) { msg.recycle(); returnfalse; }
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. // 添加到队列头部, 有可能是需要wake的 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. // 插入到队列的中间位置. 通常这种情况下, 我们不需要wake线程. 除非有一种请求, 就是队列的头msg是一个同步barrier, 并且新插入的msg是一个异步消息 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); } } returntrue; }