Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android消息機制Handler的實現原理解析

Android消息機制Handler的實現原理解析

編輯:關於Android編程

線程是一個動態執行的過程,從產生到死亡包括五個狀態:新建、就緒、運行、死亡和堵塞。只要線程沒有執行完畢或者沒有被其它線程殺死,線程就不會進入死亡狀態。Android中的主線程一直存在是因為主線程中一直在監聽消息,從而使線程無法被執行完畢。

線程的五種狀態:

新建new Thread
當創建Thread類的一個實例對象時,此線程進入新建狀態未被啟動。 就緒runnable
線程已經被啟動,正在等待被分配給CPU時間片,也就是說此時線程正在就緒隊列中排隊等候得到CPU資源。 運行running
線程獲得CPU資源正在執行任務run()方法,此時除非此線程自動放棄CPU資源或者有優先級更高的線程進入,線程將一直運行到結束。 死亡dead
當線程執行完畢或被其它線程殺死,線程就進入死亡狀態,這時線程不可能再進入就緒狀態等待執行。 堵塞blocked
由於某種原因導致正在運行的線程讓出CPU並暫停自己的執行,即進入堵塞狀態。

什麼是Handler?

Android是消息驅動的,消息驅動包括Java層實現和Native層實現。Java層的消息機制包括Message、MessageQueue、Looper、Handler四要素, Native層的消息機制包括Looper、MessageQueue兩要素。Handler是Android消息機制的統稱,它主要是解決手機中進程內兩個線程之間如何通訊。

消息驅動是圍繞消息的產生與處理展開的,並依靠消息循環機制來實現。

消息的四要素:

Message
消息的主體 MessageQueue
消息隊列 Looper
消息循環,用於循環取出消息進行處理 Handler
消息處理,Looper從MessageQueue取出Message後要對Message進行處理

這裡寫圖片描述
Hanlder消息循環機制原理圖

主線程通過調用Looper.prepare()初始化Looper、MessageQueue、NativeMessageQueue(Native)和Looper(Native);

Looper和MessageQueue是Java層的類,NativeMessageQueue(Native)和Looper(Native)是JNI層的類。Looper是在prepare()方法被調用的時候初始化的,初始化Looper的時候會跟著初始化MessageQueue,MessageQueue的初始化工作會通過JNI方法將NativeMessageQueue(Native)和MessageQueue關聯起來,關聯也就是在Native層初始化NativeMessageQueue,然後將NativeMessageQueue對象的收地址存放到MessageQueue對象中的mPtr變量中,NativeMessageQueue初始化的時候會跟著初始化Looper(Native)。

主線程通過調用Looper.loop()進入無限消息循環,保證主線程一直存活;

消息循環是通過Looper.loop()開始的,這個方法可以理解為是一個死循環的方法,可以通過阻塞和非阻塞兩種方式去獲取消息。當使用阻塞方式獲取消息的時候跟Socket客戶端的用法很相似。Looper(Native)是實現阻塞的地方,實現的原理是 I/O 多路復用中的epoll,這裡我不詳細講它實現的原理,epoll你大致可以理解為它可以支持多個socket客戶端連接,當某個socket客戶端收到消息後阻塞過程被恢復,MessageQueue通過檢測消息隊列可以知道當前是不是有消息可以返回給loop方法。這個阻塞操作只是為了讓線程空閒下來,沒有其它的實際意義。


消息循環機制的建立

一、消息循環機制的初始化

不管是主線程還是子線程,都可以建立消息循環機制,主線程的消息循環機制是由系統替我們建立的,如果我們需要在子線程中建立消息循環機制,那麼就要調用相關的API函數去建立,下面我們以主線程的消息循環機制為例講解系統是如何為主線程初始化消息循環機制的。首先,每一個Activity創建都是從ActivityThread.main()方法開始的。
frameworks\base\core\java\android\app\ActivityThread.java

    public static void main(String[] args) {
       ......
        Looper.prepareMainLooper();
        ActivityThread thread = new ActivityThread();
        thread.attach(false);
       ......
        Looper.loop();
        throw new RuntimeException("Main thread loop unexpectedly exited");
    }

從上面的代碼可以看出loop()方法的後面直接拋出異常,說明正常情況Android是不允許主線程退出的,也就是說正常情況下下來loop()方法不可能執行完,接下來先看 Looper.prepareMainLooper()是如何實現的。
frameworks\base\core\java\android\os\Looper.java

    /**
     * Initialize the current thread as a looper, marking it as an
     * application's main looper. The main looper for your application
     * is created by the Android environment, so you should never need
     * to call this function yourself.  See also: {@link #prepare()}
     */
    public static void prepareMainLooper() {
        prepare(false);
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();
        }
    }

其它的代碼不用看,只看 prepare(false);這一行就行了,我們來看一下prepare的實現

frameworks\base\core\java\android\os\Looper.java

    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }

prepare只是初始化了Looper並交給ThreadLocal去管理,,注意這裡有一個quitAllowed參數,它表示當前Looper消息循環機制是否可以退出,true表示可以退出,false表示不能退出,主線程的Looper默認是不能退出的,在上面prepareMainLooper中調用 prepare(false)就可以知道,我們來看Looper的構造函數干了啥。
frameworks\base\core\java\android\os\Looper.java

    private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }

在這裡對MessageQueue進行了初始化,接著看MessageQueue的實現

frameworks\base\core\java\android\os\MessageQueue.java

    MessageQueue(boolean quitAllowed) {
        mQuitAllowed = quitAllowed;
        mPtr = nativeInit();
    }

這裡直接調用Native層的方法nativeInit()來初始化Native層的對象,繼續看Native層是如何初始化的
frameworks\base\core\jni\android_os_MessageQueue.cpp

static jlong android_os_MessageQueue_nativeInit(JNIEnv* env, jclass clazz) {
    NativeMessageQueue* nativeMessageQueue = new NativeMessageQueue();
    if (!nativeMessageQueue) {
        jniThrowRuntimeException(env, "Unable to allocate native queue");
        return 0;
    }

    nativeMessageQueue->incStrong(env);
    return reinterpret_cast(nativeMessageQueue);
}

第一行直接初始化了一個Native層的NativeMessageQueue對象,並將其首地址返回給Java層,從上一步可以知道是保存在MessageQueue的mPtr變量中。至此,整個初始化過程就已經完畢了。

二、消息循環機制

消息循環機制主要是實現了一個循環監聽,通過linux中的IO多路復用裡面的epoll機制來實現阻塞,不斷的從MessageQueue中取出消息,然後執行消息,下面接著來看Looper.loop()是如何建立消息循環機制的。
frameworks\base\core\java\android\os\Looper.java


    /**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the loop.
     */
    public static void loop() {
        final Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        final MessageQueue queue = me.mQueue;

        // Make sure the identity of this thread is that of the local process,
        // and keep track of what that identity token actually is.
        Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();

        for (;;) {
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }
            ......

            msg.target.dispatchMessage(msg);
            ......

            msg.recycleUnchecked();
        }
    }

在這裡方法中我們可以看到loop方法永遠不會退出,只有當獲取到的Message是null的時候才退出,queue.next()是獲取Message的方法,我們等會再說,繼續看下面的代碼,當獲取到的Message不為null的是就會調用 msg.target.dispatchMessage(msg)來分發消息,這裡的msg.target指向的實際上就是我們代碼中的Handler,我們來看Handler中的dispatchMessage的實現。
frameworks\base\core\java\android\os\Handler.java

    /**
     * Handle system messages here.
     */
    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }

這裡會首先判斷msg.callback是不是null,msg.callback實際上就是我們調用handler的post方法傳入的Runnable,而handleCallback方法也就是執行Runnable.run(),當msg.callback不為null時執行handleCallback,當msg.callback為null時會先判斷mCallback是不是null,mCallback是我們在初始化Handler時傳入的參數,如果外部mCallback.handleMessage(msg)返回true,表示不執行Handler中的handleMessage,否則就繼續往下執行。消息的處理就講到這裡,接著上一步的queue.next(),我們來看這個函數是怎麼實現的。
frameworks\base\core\java\android\os\MessageQueue.java


    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();
            }
            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 (false) Log.v("MessageQueue", "Returning message: " + msg);
                        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;
                }

                // 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.
            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("MessageQueue", "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.
            nextPollTimeoutMillis = 0;
        }
    }

首先判斷mPtr是不是為0,為0表示跟Native層NativeMessageQueue對象關聯失敗,直接返回null,從上一步可以知道這裡返回null loop方法就會退出,同樣線程也就跟著退出了。nextPollTimeoutMillis表示下一個消息將要在未來多久執行,nextPollTimeoutMillis的取值很重要!在下面通過nativePollOnce(ptr, nextPollTimeoutMillis)將nextPollTimeoutMillis的值傳入了底層,nextPollTimeoutMillis的取值有三種情況,0,-1,和大於0。當等於0時,nativePollOnce不會阻塞,當等於-1時,nativePollOnce會一直阻塞知道被wake,大於0時,nativePollOnce會等待nativePollOnce指定值的時間,如果一直沒有wake那麼就會超時返回。nativePollOnce的實現我們暫時先不分析,下面我們繼續分析nativePollOnce返回之後的流程

                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 (false) Log.v("MessageQueue", "Returning message: " + msg);
                        return msg;
                    }
                } else {
                    // No more messages.
                    nextPollTimeoutMillis = -1;
                }

上面這段代碼主要是做邏輯判斷,從消息隊列中取出一條可以執行的消息,如果當前消息隊列為空,那麼nextPollTimeoutMillis = -1,nativePollOnce就會一直被阻塞,直到有新的Message被添加到隊列並調用wake,wake是讓Looper(Native)取消阻塞的,具體怎麼實現後面再講。如果消息隊列不為空,那麼就去找到當前可以執行的消息,找到了就直接返回這條消息,並將其從消息隊列中移除。如果沒找到可以執行的消息,就算出離現在執行時間最近的時間,將這個時間賦值給nextPollTimeoutMillis。 if (msg != null && msg.target == null) 這個if語句用於處理barrier消息的,barrier是什麼我們在後面發送消息的時候再介紹,繼續往下分析

                // 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);

這裡先判斷有沒有退出,即mQuitting是不是true,是true就返回一個null消息,至此Looper消息循環機制就終止了。mQuitting前面介紹初始化的時候有介紹這個值。pendingIdleHandlerCount表示的是IdleHandler的數目,這裡大致可以理解為當有IdleHandler的時候就會往下執行IdleHandler,沒有就繼續回到上面執行nativePollOnce。

            // Run the idle handlers.
            // We only ever reach this code block during the first iteration.
            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("MessageQueue", "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.
            nextPollTimeoutMillis = 0;

這裡就是執行IdleHandler的地方,注意如果IdleHandler的queueIdle方法返回false就會從隊列中移除,表示這個IdleHandler只會執行一次,注意執行完IdleHandler後會將nextPollTimeoutMillis置為0,前面講了0表示不阻塞直接返回。到這裡Java層的消息循環就完成了,下面接著講Native層是如何實現阻塞的,即nativePollOnce的實現。
frameworks\base\core\jni\android_os_MessageQueue.cpp

static void android_os_MessageQueue_nativePollOnce(JNIEnv* env, jclass clazz,
        jlong ptr, jint timeoutMillis) {
    NativeMessageQueue* nativeMessageQueue = reinterpret_cast(ptr);
    nativeMessageQueue->pollOnce(env, timeoutMillis);
}

ptr是我們前面初始化NativeMessageQueue的時候存放其對象收地址的變量,這裡通過首地址直接獲取到NativeMessageQueue對象,獲取到之後調用pollOnce,注意這裡的timeoutMillis就是前面講到的nextPollTimeoutMillis。
system\core\libutils\Looper.cpp

int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
    int result = 0;
    for (;;) {
        while (mResponseIndex < mResponses.size()) {
            const Response& response = mResponses.itemAt(mResponseIndex++);
            int ident = response.request.ident;
            if (ident >= 0) {
                int fd = response.request.fd;
                int events = response.events;
                void* data = response.request.data;
#if DEBUG_POLL_AND_WAKE
                ALOGD("%p ~ pollOnce - returning signalled identifier %d: "
                        "fd=%d, events=0x%x, data=%p",
                        this, ident, fd, events, data);
#endif
                if (outFd != NULL) *outFd = fd;
                if (outEvents != NULL) *outEvents = events;
                if (outData != NULL) *outData = data;
                return ident;
            }
        }

        if (result != 0) {
#if DEBUG_POLL_AND_WAKE
            ALOGD("%p ~ pollOnce - returning result %d", this, result);
#endif
            if (outFd != NULL) *outFd = 0;
            if (outEvents != NULL) *outEvents = 0;
            if (outData != NULL) *outData = NULL;
            return result;
        }

        result = pollInner(timeoutMillis);
    }
}

pollOnce函數裡面前面所有的方法都是對Native層消息的處理,Native層消息的處理我們以後再講,我們來看關鍵阻塞的地方最後那一句pollInner的實現。

int Looper::pollInner(int timeoutMillis) {
#if DEBUG_POLL_AND_WAKE
    ALOGD("%p ~ pollOnce - waiting: timeoutMillis=%d", this, timeoutMillis);
#endif

    // Adjust the timeout based on when the next message is due.
    if (timeoutMillis != 0 && mNextMessageUptime != LLONG_MAX) {
        nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
        int messageTimeoutMillis = toMillisecondTimeoutDelay(now, mNextMessageUptime);
        if (messageTimeoutMillis >= 0
                && (timeoutMillis < 0 || messageTimeoutMillis < timeoutMillis)) {
            timeoutMillis = messageTimeoutMillis;
        }
#if DEBUG_POLL_AND_WAKE
        ALOGD("%p ~ pollOnce - next message in %lldns, adjusted timeout: timeoutMillis=%d",
                this, mNextMessageUptime - now, timeoutMillis);
#endif
    }

    // Poll.
    int result = POLL_WAKE;
    mResponses.clear();
    mResponseIndex = 0;

    // We are about to idle.
    mIdling = true;

    struct epoll_event eventItems[EPOLL_MAX_EVENTS];
    int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);

    // No longer idling.
    mIdling = false;

    // Acquire lock.
    mLock.lock();

    // Check for poll error.
    if (eventCount < 0) {
        if (errno == EINTR) {
            goto Done;
        }
        ALOGW("Poll failed with an unexpected error, errno=%d", errno);
        result = POLL_ERROR;
        goto Done;
    }

    // Check for poll timeout.
    if (eventCount == 0) {
#if DEBUG_POLL_AND_WAKE
        ALOGD("%p ~ pollOnce - timeout", this);
#endif
        result = POLL_TIMEOUT;
        goto Done;
    }

    // Handle all events.
#if DEBUG_POLL_AND_WAKE
    ALOGD("%p ~ pollOnce - handling events from %d fds", this, eventCount);
#endif

    for (int i = 0; i < eventCount; i++) {
        int fd = eventItems[i].data.fd;
        uint32_t epollEvents = eventItems[i].events;
        if (fd == mWakeReadPipeFd) {
            if (epollEvents & EPOLLIN) {
                awoken();
            } else {
                ALOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents);
            }
        } else {
            ssize_t requestIndex = mRequests.indexOfKey(fd);
            if (requestIndex >= 0) {
                int events = 0;
                if (epollEvents & EPOLLIN) events |= EVENT_INPUT;
                if (epollEvents & EPOLLOUT) events |= EVENT_OUTPUT;
                if (epollEvents & EPOLLERR) events |= EVENT_ERROR;
                if (epollEvents & EPOLLHUP) events |= EVENT_HANGUP;
                pushResponse(events, mRequests.valueAt(requestIndex));
            } else {
                ALOGW("Ignoring unexpected epoll events 0x%x on fd %d that is "
                        "no longer registered.", epollEvents, fd);
            }
        }
    }
Done: ;

    // Invoke pending message callbacks.
    mNextMessageUptime = LLONG_MAX;
    while (mMessageEnvelopes.size() != 0) {
        nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
        const MessageEnvelope& messageEnvelope = mMessageEnvelopes.itemAt(0);
        if (messageEnvelope.uptime <= now) {
            // Remove the envelope from the list.
            // We keep a strong reference to the handler until the call to handleMessage
            // finishes.  Then we drop it so that the handler can be deleted *before*
            // we reacquire our lock.
            { // obtain handler
                sp handler = messageEnvelope.handler;
                Message message = messageEnvelope.message;
                mMessageEnvelopes.removeAt(0);
                mSendingMessage = true;
                mLock.unlock();

#if DEBUG_POLL_AND_WAKE || DEBUG_CALLBACKS
                ALOGD("%p ~ pollOnce - sending message: handler=%p, what=%d",
                        this, handler.get(), message.what);
#endif
                handler->handleMessage(message);
            } // release handler

            mLock.lock();
            mSendingMessage = false;
            result = POLL_CALLBACK;
        } else {
            // The last message left at the head of the queue determines the next wakeup time.
            mNextMessageUptime = messageEnvelope.uptime;
            break;
        }
    }

    // Release lock.
    mLock.unlock();

    // Invoke all response callbacks.
    for (size_t i = 0; i < mResponses.size(); i++) {
        Response& response = mResponses.editItemAt(i);
        if (response.request.ident == POLL_CALLBACK) {
            int fd = response.request.fd;
            int events = response.events;
            void* data = response.request.data;
#if DEBUG_POLL_AND_WAKE || DEBUG_CALLBACKS
            ALOGD("%p ~ pollOnce - invoking fd event callback %p: fd=%d, events=0x%x, data=%p",
                    this, response.request.callback.get(), fd, events, data);
#endif
            int callbackResult = response.request.callback->handleEvent(fd, events, data);
            if (callbackResult == 0) {
                removeFd(fd);
            }
            // Clear the callback reference in the response structure promptly because we
            // will not clear the response vector itself until the next poll.
            response.request.callback.clear();
            result = POLL_CALLBACK;
        }
    }
    return result;
}

找到epoll_wait, epoll_wait調用之後線程就會阻塞在這個地方,前面介紹的三種阻塞方式其實就是epoll_wait最後一個參數timeoutMillis設置的,等0會直接返回,等-1會一直阻塞,大於0會一直阻塞直到超時。Native層大致就將這麼多,以後再專門寫一篇文章詳細分析Native層的實現。

三、發送消息

Java層發送消息是通過Handler實現的,有三種類型的消息,第一種是發送一個空的消息,只需要指定int what這個值就行,第二種是可以發送Runnable為參數的消息,第三種是直接發送Message msg為參數的消息,其實前面兩種方式跟第三種方是一樣的,都會構建一個Message對象,然後添加到MessageQueue隊列中。下面來看Handler中三種方式的具體實現。

frameworks\base\core\java\android\os\Handler.java

第一種方式:
    /**
     * Sends a Message containing only the what value.
     *  
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */
    public final boolean sendEmptyMessage(int what)
    {
        return sendEmptyMessageDelayed(what, 0);
    }

    /**
     * Sends a Message containing only the what value, to be delivered
     * after the specified amount of time elapses.
     * @see #sendMessageDelayed(android.os.Message, long) 
     * 
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */
    public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
        Message msg = Message.obtain();
        msg.what = what;
        return sendMessageDelayed(msg, delayMillis);
    }

當我們調用sendEmptyMessage發送消息時,實際上就是調用的sendEmptyMessageDelayed,默認將delayMillis設置為0,在sendEmptyMessageDelayed可以看到通過Message.obtain()獲取了一個消息體,然後將what變量賦值,最終調用sendMessageDelayed來發送這條Message。sendMessageDelayed的實現我們在介紹第三種方式的時候再將,這裡我們先來看一下為什麼要調用Message.obtain()去獲取一條消息,而不是直接new Message的方式,事實上我們也不建議new Message的方式來獲取消息,先來看Message.obtain()的代碼實現。
frameworks\base\core\java\android\os\Message.java

    /**
     * Return a new Message instance from the global pool. Allows us to
     * avoid allocating new objects in many cases.
     */
    public static Message obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {
                Message m = sPool;
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                /// M: Add message protect mechanism
                m.hasRecycle = false;
                return m;
            }
        }
        return new Message();
    }


    /**
     * Recycles a Message that may be in-use.
     * Used internally by the MessageQueue and Looper when disposing of queued Messages.
     */
    void recycleUnchecked() {
        // Mark the message as in use while it remains in the recycled object pool.
        // Clear out all other details.
        flags = FLAG_IN_USE;
        what = 0;
        arg1 = 0;
        arg2 = 0;
        obj = null;
        replyTo = null;
        sendingUid = -1;
        when = 0;
        target = null;
        callback = null;
        data = null;
        hasRecycle = true;
        synchronized (sPoolSync) {
            if (sPoolSize < MAX_POOL_SIZE) {
                next = sPool;
                sPool = this;
                sPoolSize++;
            }
        }
    }

從上面的代碼可以看出調用obtain獲取Message的流程是,首先判斷消息池裡面有沒有消息,如果有Message就把消息池中的第一個Message取出來,並把這個Message從消息池中移除,然後把後面的Message放到消息池的第一個位置,做完這些操作後就直接把取出的Message返回給調用者。如果消息池中沒有Message的話就直接new Message。看到這裡可能大家還是沒明白這個消息池到底是怎麼一回事,沒事,我們接著看下面那個函數recycleUnchecked的實現,這個函數是在Message執行完後被調用的,我們通過Handler發送一個Message後,通過Handler的handleMessage處理完這條Message後,recycleUnchecked就會被調用。分析代碼,recycleUnchecked其實就是將這條已經使用過的Message重新初始化,如果當前消息池中的數目小於MAX_POOL_SIZE系統限定的值,就將其加入消息池的第一個位置,原消息池第一個位置的Message會被移動到第二個位置,大於就不會添加了。分析完這裡我們就明白了為什麼不建議new Message去創建一條消息,因為我們的Message使用完後會將其添加到消息池,而不會被虛擬機回收,達不到Message復用的效果。 消息的創建就講到這裡了,接下來繼續將發送消息的第二種方式,先看代碼。

frameworks\base\core\java\android\os\Handler.java

第二種方式:
    /**
     * Causes the Runnable r to be added to the message queue.
     * The runnable will be run on the thread to which this handler is 
     * attached. 
     *  
     * @param r The Runnable that will be executed.
     * 
     * @return Returns true if the Runnable was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */
    public final boolean post(Runnable r)
    {
       return  sendMessageDelayed(getPostMessage(r), 0);
    }


    private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        m.callback = r;
        return m;
    }

第二種方式可以接受一個Runnable類型的參數,其實也就是將Runnable參數傳遞給了Message的callback變量,這裡代碼很簡單,我就不做詳細分析了。看第三種方式吧。
frameworks\base\core\java\android\os\Handler.java

第三種方式:

    /**
     * Pushes a message onto the end of the message queue after all pending messages
     * before the current time. It will be received in {@link #handleMessage},
     * in the thread attached to this handler.
     *  
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */
    public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }
    /**
     * Enqueue a message into the message queue after all pending messages
     * before (current time + delayMillis). You will receive it in
     * {@link #handleMessage}, in the thread attached to this handler.
     *  
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.  Note that a
     *         result of true does not mean the message will be processed -- if
     *         the looper is quit before the delivery time of the message
     *         occurs then the message will be dropped.
     */
    public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }
    /**
     * Enqueue a message into the message queue after all pending messages
     * before the absolute time (in milliseconds) uptimeMillis.
     * The time-base is {@link android.os.SystemClock#uptimeMillis}.
     * Time spent in deep sleep will add an additional delay to execution.
     * You will receive it in {@link #handleMessage}, in the thread attached
     * to this handler.
     * 
     * @param uptimeMillis The absolute time at which the message should be
     *         delivered, using the
     *         {@link android.os.SystemClock#uptimeMillis} time-base.
     *         
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.  Note that a
     *         result of true does not mean the message will be processed -- if
     *         the looper is quit before the delivery time of the message
     *         occurs then the message will be dropped.
     */
    public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;
        if (queue == null) {
            RuntimeException e = new RuntimeException(
                    this + " sendMessageAtTime() called with no mQueue");
            Log.w("Looper", e.getMessage(), e);
            return false;
        }
        return enqueueMessage(queue, msg, uptimeMillis);
    }
    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

sendMessage和sendMessageDelayed很簡單,這裡我就不多做分析了,sendMessageAtTime會先判斷當前消息隊列存不存在,不存在直接返回,不繼續往下執行了。在enqueueMessage中的第一行代碼就是將當前的Handler賦值給了msg.target,這個賦值很關鍵,後面執行Handler的handleMessage方法就是通過這個msg.target執行的,下面判斷mAsynchronous是不是真,為true表示這是異步消息,false表示非異步消息。那麼什麼又是異步消息和非異步消息呢?要明白Asynchronous的作用,需要先了解一個概念Barrier。

Barrier與Asynchronous Message

Barrier是什麼意思呢,從名字看是一個攔截器,在這個攔截器後面的消息都暫時無法執行,直到這個攔截器被移除了,MessageQueue有一個函數叫enqueueSyncBarier可以添加一個Barrier。

frameworks\base\core\java\android\os\Looper.java


    /**
     * Posts a synchronization barrier to the Looper's message queue.
     *
     * Message processing occurs as usual until the message queue encounters the
     * synchronization barrier that has been posted.  When the barrier is encountered,
     * later synchronous messages in the queue are stalled (prevented from being executed)
     * until the barrier is released by calling {@link #removeSyncBarrier} and specifying
     * the token that identifies the synchronization barrier.
     *
     * This method is used to immediately postpone execution of all subsequently posted
     * synchronous messages until a condition is met that releases the barrier.
     * Asynchronous messages (see {@link Message#isAsynchronous} are exempt from the barrier
     * and continue to be processed as usual.
     *
     * This call must be always matched by a call to {@link #removeSyncBarrier} with
     * the same token to ensure that the message queue resumes normal operation.
     * Otherwise the application will probably hang!
     *
     * @return A token that uniquely identifies the barrier.  This token must be
     * passed to {@link #removeSyncBarrier} to release the barrier.
     *
     * @hide
     */
    public int postSyncBarrier() {
        return mQueue.enqueueSyncBarrier(SystemClock.uptimeMillis());
    }

frameworks\base\core\java\android\os\MessageQueue.java

    int enqueueSyncBarrier(long when) {
        // Enqueue a new sync barrier token.
        // We don't need to wake the queue because the purpose of a barrier is to stall it.
        synchronized (this) {
            final int token = mNextBarrierToken++;
            final Message msg = Message.obtain();
            msg.markInUse();
            msg.when = when;
            msg.arg1 = token;

            Message prev = null;
            Message p = mMessages;
            if (when != 0) {
                while (p != null && p.when <= when) {
                    prev = p;
                    p = p.next;
                }
            }
            if (prev != null) { // invariant: p == prev.next
                msg.next = p;
                prev.next = msg;
            } else {
                msg.next = p;
                mMessages = msg;
            }
            return token;
        }
    }

從上面的代碼分析,我們要設置一個Barrier,只能調用Looper中的postSyncBarrier方法,在enqueueSyncBarrier中,obtain了一個Message,並設置msg.arg1=token,token僅是一個每次調用enqueueSyncBarrier時自增的int值,目的是每次調用enqueueSyncBarrier時返回唯一的一個token,這個Message同樣需要設置執行時間,然後插入到消息隊列,特殊的是這個Message沒有設置target,即msg.target為null。 進入消息循環後會不停地從MessageQueue中取消息執行,調用的是MessageQueue的next函數,其中有這麼一段:

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());
}

如果隊列頭部的消息的target為null就表示它是個Barrier,因為只有兩種方法往mMessages中添加消息,一種是enqueueMessage,另一種是enqueueBarrier,而enqueueMessage中如果mst.target為null是直接拋異常的,後面會看到。 所謂的異步消息其實就是這樣的,我們可以通過enqueueBarrier往消息隊列中插入一個Barrier,那麼隊列中執行時間在這個Barrier以後的同步消息都會被這個Barrier攔截住無法執行,直到我們調用removeBarrier移除了這個Barrier,而異步消息則沒有影響,消息默認就是同步消息,除非我們調用了Message的setAsynchronous,這個方法是隱藏的。只有在初始化Handler時通過參數指定往這個Handler發送的消息都是異步的,這樣在Handler的enqueueMessage中就會調用Message的setAsynchronous設置消息是異步的,從上面Handler.enqueueMessage的代碼中可以看到。 所謂異步消息,其實只有一個作用,就是在設置Barrier時仍可以不受Barrier的影響被正常處理,如果沒有設置Barrier,異步消息就與同步消息沒有區別,可以通過Looper中的removeSyncBarrier移除Barrier。

enqueueMessage的實現

frameworks\base\core\java\android\os\MessageQueue.java


    boolean enqueueMessage(Message msg, long when) {
        if (msg.target == null) {
            throw new IllegalArgumentException("Message must have a target.");
        }
        if (msg.isInUse()) {
            throw new IllegalStateException(msg + " This message is already in use.");
        }

        synchronized (this) {
            if (mQuitting) {
                IllegalStateException e = new IllegalStateException(
                        msg.target + " sending message to a Handler on a dead thread");
                Log.w("MessageQueue", e.getMessage(), e);
                msg.recycle();
                return false;
            }

            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.
                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.
                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);
            }
        }
        return true;
    }

enqueueMessage首先會檢測msg.target是不是為null和msg.isInUse()是不是成立,如果條件成立就會拋出異常,然後再判斷消息循環機制是不是退出了,如果退出了就直接返回。下面的邏輯我梳理一下,可以這麼去理解,首先要明白一點是消息隊列中的Message排序是通過Message將要執行的時間間隔實現的,間隔越小的排在隊列前面。enqueueMessage在插入新Message的情況是消息隊列中沒有消息或者消息隊列中第一個Message的執行間隔大於當前要設置Message的執行間隔。 如果不滿足這些條件就會插入消息隊列中的合適的位置。注意 nativeWake(mPtr)這個函數的作用是喚醒Native層的阻塞。使MessageQueue中next方法的nativePollOnce得到返回,可以繼續執行nativePollOnce後面的代碼。nativeWake喚醒的原理就是向管道發送一個字符,kernel層檢測到管道中有數據之後,epoll_wait就能夠返回了。epoll有三種喚醒方式,可讀,可寫和超時。這裡就是可讀的方式來喚醒epoll_wait。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved