private static void prepare(boolean quitAllowed) {//quitAllowed 主线程传入为false,而子线程为true if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); } // Looper的构造方法 private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); }
public static void loop() { final MessageQueue queue = me.mQueue; for (;;) { Message msg = queue.next(); // might block 有可能能阻塞 nativePollOnce。 if (msg == null) { return; } // This must be in a local variable, in case a UI event sets the logger final long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs; msg.target.dispatchMessage(msg); msg.recycleUnchecked(); } }
Message next() { //... int pendingIdleHandlerCount = -1; // 仅仅在首次迭代过程中,该值设置为-1。 int nextPollTimeoutMillis = 0; for (;;) { //阻塞操作,当等待nextPollTimeoutMillis时长,或者nativeWake(mPtr)唤醒,都会结束阻塞。 nativePollOnce(ptr, nextPollTimeoutMillis); synchronized (this) { // 不断尝试的从链表中找到首个同步消息msg。 final long 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. do { prevMsg = msg; msg = msg.next; } while (msg != null && !msg.isAsynchronous()); } if (msg != null) { if (now < msg.when) { // 消息尚未达到执行时间,重新计算nextPollTimeoutMillis,以便在下次循环中进行阻塞。 nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE); } else { // 消息已经达到或者超过执行时间,取出消息,同时设置mBlocked为false。 //mBlocked涉及nativeWake是否执行。 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,一直等待。 nextPollTimeoutMillis = -1; }
// Process the quit message now that all pending messages have been handled. if (mQuitting) { dispose(); return null; }
// 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. 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. //在该代码块执行一次过后,pendingIdleHandlerCount就被设置为0了,永远不会再运行。 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; } }
近期评论