Handler的前世今生5 —— MessageQueue

MessageQueue文檔
通過前面的文章,我們知道很多操作其實都是MessageQueue來負責:

  1. Looper 的loop() 中使用 MessageQueue的next() 方法;
  2. Looper的quit()中使用MessageQueuequit() 方法;
  3. Handler中的enqueueMessage() 使用MessageQueueenqueueMessage() 方法;
  4. Handler中的removeCallbacksAndMessages()使用的是MessageQueue中的removeCallbacksAndMessages方法;

其實在開發過程中,我們是不與MessageQueue直接打交道的。

But毫不誇張的講,只有真正的理解了MessageQueue,我們才能真正地明白整個Handler通信機制的設計之美。


MessageQueue的三大功能:

  1. 消息入隊操作及按照時間排序操作
  2. 消息出隊列線程阻塞實現
  3. 阻塞時提供IdleHandler執行一些其它任務(肯定不能是耗時操作);
MessageQueue(boolean quitAllowed) {
        mQuitAllowed = quitAllowed;
        // 這個就是Native層的MessageQueue.一切關於MessageQueue的操作都是圍繞它展開的
        mPtr = nativeInit();
    }

1. enqueueMessage()入隊操作

需要我們特別注意的是:在消息入隊列的時候,直接按照when進行排序

具體的來下面的源碼註釋:上來肯定是要對Message 以及MessageQueue 的狀態進行判斷。

boolean enqueueMessage(Message msg, long when) {
        // 1. 判斷消息的所有者
        if (msg.target == null) {
            throw new IllegalArgumentException("Message must have a target.");
        }
        // 2. 判斷消息的狀態
        if (msg.isInUse()) {
            throw new IllegalStateException(msg + " This message is already in use.");
        }

        synchronized (this) {
            // 3. 判斷MessageQueue的狀態
            if (mQuitting) {
                IllegalStateException e = new IllegalStateException(
                        msg.target + " sending message to a Handler on a dead thread");
                Log.w(TAG, e.getMessage(), e);
                msg.recycle();
                // 入隊失敗
                return false;
            }
            // 4. 更新消息的狀態
            msg.markInUse();
            // 5. 重點:Message的when屬性是在這裏賦值的
            msg.when = when;
            // 6.消息隊列的頭節點
            Message p = mMessages;
            // 7. 是否需要喚醒線程
            boolean needWake;
        
            /**
            * 從這裏開始是真正處理消息入隊列。
            * 插入表頭的三種情況:
            * 1. 當 頭節點爲空即(消息隊列爲空)時,
            * 2. Handler發送消息時要求直接插入到隊列頭部:(對應的應該是Handler的sendMessageAtFrontOfQueue()),when = 0
            * 3. 當前消息的延遲時間小於頭節點的時間
            */
            if (p == null || when == 0 || when < p.when) {
                // New head, wake up the event queue if blocked.
                //新消息的next指針指向頭節點;
                msg.next = p;
                // 更新頭節點
                mMessages = msg;
                // 若當前是阻塞狀態,則直接喚醒;
                needWake = mBlocked;
            } else {
                // 根據註釋,我們也能看出個大概,將消息插入到隊列的中間,一般情況下,不會直接喚醒的。
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;
                    // 注意這裏:這裏是保證 Message 在入隊列時按照 when 進行排序的關鍵。
                    //在入隊的時候就進行排序了,而不是先入隊列再sort.... 233333
                    if (p == null || when < p.when) {
                        break;
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }

                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }

            // 這裏的mPtr,其實是C++層次的一個MessageQueue.當mPtr != 0 ,即 表示消息隊列沒有退出。
            // 如果是阻塞,就是調用native方法nativeWake進行喚醒。 
            if (needWake) {
                nativeWake(mPtr);
            }
        }
        // 入隊成功
        return true;
    }

2. IdleHandler

我們都知道Looper在loop(),如果沒有消息則會wait,進行阻塞(而且我們知道,真正的阻塞邏輯是在MessageQueue的next()方法中實現的)。但是這樣阻塞着,是不是有點浪費資源呢?

不管咱們覺的,Android的設計者也認爲這是一種浪費,所以MessageQueue還擔當了另一個角色:當線程阻塞時,給我們提供了IdleHandler 用來在線程空閒時處理任務。
MessageQueue的IdleHandler說明

 /**
     * Callback interface for discovering when a thread is going to block
     * waiting for more messages.
     */
    public static interface IdleHandler {
        boolean queueIdle();
    }

這裏先提到IdleHandler是因爲next()方法在出現阻塞時,要對應的去處理一些IdleHandler事件。

整個MessageQueue的阻塞邏輯都在這個next()方法裏面。

3. next()出隊操作

根據上面的內容,我們知道next()方法有以下作用:

  1. 返回Message;
  2. 阻塞;
  3. 阻塞時處理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();
            }
            
            // 注意這裏:根據參數nextPollTimeoutMillis,Native層的MessageQueue決定是否阻塞
            nativePollOnce(ptr, nextPollTimeoutMillis);

            synchronized (this) {
                // Try to retrieve the next message.  Return if found.
                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) {
                        // 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 {
                    // No more messages.
                    // 沒有消息時 阻塞
                    nextPollTimeoutMillis = -1;
                }

                // Process the quit message now that all pending messages have been handled.
                if (mQuitting) {
                    dispose();
                    return null;
                }


            /********************處理IdleHandler的邏輯************************/

                // 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.
            // 運行IdleHandler...
            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(TAG, "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.
            // 當調用一個IdleHandler時,一個新的Message可能被傳遞過來,
            // 所以 需要回過頭重新判斷一下消息的狀態;
            nextPollTimeoutMillis = 0;
        }
    }

注意觀察:代碼中只有兩處給我們給我們返回null。都是根據MessageQueue 中的狀態給我們的。這也證實了Looper裏面的當message爲空時,採用return 退出循環 。

真正的重點在於:

nativePollOnce(ptr, nextPollTimeoutMillis); 阻塞邏輯就在這裏
阻塞方法,主要是通過native層的epoll監聽文件描述符的寫入事件來實現的。
如果nextPollTimeoutMillis=-1,一直阻塞不會超時。
如果nextPollTimeoutMillis=0,不會阻塞,立即返回。
如果nextPollTimeoutMillis>0,最長阻塞nextPollTimeoutMillis毫秒(超時),如果期間有程序喚醒會立即返回。

推薦閱讀 深入理解MessageQueue

至於後面的退出移除操作,就不必多講啦.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章