/** * Callback interface for discovering when a thread is going to block * waiting for more messages. */ 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. */ booleanqueueIdle(); }
/** * Add a new {@link IdleHandler} to this message queue. This may be * removed automatically for you by returning false from * {@link IdleHandler#queueIdle IdleHandler.queueIdle()} when it is * invoked, or explicitly removing it with {@link #removeIdleHandler}. * * <p>This method is safe to call from any thread. * * @param handler The IdleHandler to be added. */ publicvoidaddIdleHandler(@NonNull IdleHandler handler){ if (handler == null) { thrownew NullPointerException("Can't add a null IdleHandler"); } synchronized (this) { mIdleHandlers.add(handler); } }
/** * Remove an {@link IdleHandler} from the queue that was previously added * with {@link #addIdleHandler}. If the given object is not currently * in the idle list, nothing is done. * * <p>This method is safe to call from any thread. * * @param handler The IdleHandler to be removed. */ publicvoidremoveIdleHandler(@NonNull IdleHandler handler){ synchronized (this) { mIdleHandlers.remove(handler); } }
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(); }
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) { // Try to retrieve the next message. Return if found. finallong now = SystemClock.uptimeMillis(); Message prevMsg = null; Message msg = mMessages; if (msg != null && msg.target == null) { // Stalled by a barrier. Find the next asynchronous message // in the queue. // 这里执行的操作是忽略所有的同步消息, 知道找出queue中的异步消息 // 我理解是这个的同步消息会造成线程的阻塞, 所以忽略同步的消息 do { prevMsg = msg; msg = msg.next; } while (msg != null && !msg.isAsynchronous()); } // 走到这一步, 有两种可能, // 一种是遍历到队尾没有发现异步消息, // 另一种是找到queue中的第一个异步消息 if (msg != null) { // 找到queue中的第一个异步消息 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. // 当前消息到达可以执行的时间, 直接返回这个msg 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 { // 遍历到队尾, 没有发现异步消息或者没有消息了 // No more messages. nextPollTimeoutMillis = -1; }
// Process the quit message now that all pending messages have // been handled. // 检查当前的线程是否退出 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. // 如果queue中没有msg, 或者msg没到可执行的时间, // 那么现在线程就处于空闲时间了, 可以执行IdleHandler了 if (pendingIdleHandlerCount < 0 && (mMessages == null || now < mMessages.when)) { // pendingIdleHandlerCount在进入for循环之前是被初始化为-1的 // 并且没有更多地消息要进行处理 pendingIdleHandlerCount = mIdleHandlers.size(); } if (pendingIdleHandlerCount <= 0) { // No idle handlers to run. Loop and wait some more. // 如果没有IdleHandler要进行处理, 则直接进入下次循环 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的相关操作了 for (int i = 0; i < pendingIdleHandlerCount; i++) { final IdleHandler idler = mPendingIdleHandlers[i]; mPendingIdleHandlers[i] = null; // release the reference to the // handler
// Reset the idle handler count to 0 so we do not run them again. // 全部执行完, 重新设置这个值为0, 以便下次可以再次执行 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; } }