privatestaticvoidprepare(boolean quitAllowed) { if (sThreadLocal.get() != null) { thrownewRuntimeException("Only one Looper may be created per thread"); } // ->> 分析1.4 sThreadLocal.set(newLooper(quitAllowed)); }
/** * 分析3.1 MessageQueue.next() * 作用: * 用于取出消息队列中的下一个消息 * PS: * 看到这个next你或许就应该懂了,其实这个MessageQueue并不是咱们顾名思义以为的那个Queue,其实他的实现是一个链表 */ 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. // 如果Looper已经退出(调用了dispose方法后mPtr=0) finallongptr= mPtr; if (ptr == 0) { returnnull; }
// 记录空闲时需要处理的IdleHandler数量 intpendingIdleHandlerCount= -1; // -1 only during first iteration // ->> 分析3.2.1 intnextPollTimeoutMillis=0; for (;;) { if (nextPollTimeoutMillis != 0) { // 刷新Binder命令 Binder.flushPendingCommands(); }
synchronized (this) { // Try to retrieve the next message. Return if found. // 得到时间-从手机开机到现在调用经过的时间 finallongnow= SystemClock.uptimeMillis(); MessageprevMsg=null; // 得到队头 Messagemsg= mMessages; // 判断这个message是不是barrier if (msg != null && msg.target == null) { // Stalled by a barrier. Find the next asynchronous message in the queue. // 循环遍历出第一个异步消息,如果设置了barrier,就不能再执行同步消息了,除非将barrier移除。 // 但是异步消息不受影响照样执行,所以在这里要找到异步消息 do { prevMsg = msg; msg = msg.next; } while (msg != null && !msg.isAsynchronous()); } 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; if (DEBUG) Log.v(TAG, "Returning message: " + msg); msg.markInUse(); return msg; } } else { // 如果没有其他消息了 nextPollTimeoutMillis = -1; }
// Process the quit message now that all pending messages have been handled. // 正在退出了,返回null。 if (mQuitting) { 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. // 判断如果这是第一次循环(只有第一次循环时会小于0)并且队列为空或还没到处理第一个的时间 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; }
// Run the idle handlers. // We only ever reach this code block during the first iteration. // 开始循环执行所有的IdleHandler并根据返回值判断是否保留 for (inti=0; i < pendingIdleHandlerCount; i++) { finalIdleHandleridler= mPendingIdleHandlers[i]; mPendingIdleHandlers[i] = null; // release the reference to the handler
// While calling an idle handler, a new message could have been delivered // so go back and look again for a pending message without waiting. // 当执行了IdleHandler后,会消耗一段时间,刺死可能已经到达执行消息的时间了,所以重置该变量再重新检查时间。 nextPollTimeoutMillis = 0; } }