【核心研究】消息队列_MessageQueue

消息队列排队过程中的消息。这第一条消息将首先被处理。但假设消息本身指定要处理的时间。我们必须等待,直到时间的消息处理能力。新闻MessageQueue正在使用Message类的表示,队列中的邮件保存结构清单,Message内部对象包括:next变量,此变量指向下一个消息对象。

MessageQueue中的两个主要函数是“取出消息”和“加入消息”,各自是next()和enquenceMessage()。


next()函数

final Message next() {
        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;

        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }
            nativePollOnce(mPtr, nextPollTimeoutMillis);

            synchronized (this) {
                // Try to retrieve the next message.  Return if found.
                final long now = SystemClock.uptimeMillis();
                final Message msg = mMessages;
                if (msg != null) {
                    final long when = msg.when;
                    if (now >= when) {
                        mBlocked = false;
                        mMessages = msg.next;
                        msg.next = null;
                        if (Config.LOGV) Log.v("MessageQueue", "Returning message: " + msg);
                        return msg;
                    } else {
                        nextPollTimeoutMillis = (int) Math.min(when - now, Integer.MAX_VALUE);
                    }
                } else {
                    nextPollTimeoutMillis = -1;
                }

                // If first time, then get the number of idlers to run.
                if (pendingIdleHandlerCount < 0) {
                    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;
        }
    }

该函数的内部流程分三步:

1. 调用nativePollOnce(mPtr, int time)。这是一个JNI函数,作用是从消息队列中取出一个消息。

MessageQueue类内部本身并没有保存消息队列,真正的消息队列数据保存在JNI中的C代码中,在C环境中创建了一个NativeMessageQueue数据对象。这就是nativePollOnce()第一个參数的意义。它是一个int型变量,在C环境中。该变量被强制转换为一个NativeMessageQueue对象。在C环境中。假设消息队列中没有消息,将导致当前线程被挂起(wait),假设消息队列中有消息,则C代码中将把该消息赋值给Java环境中的mMessages变量。

2. 接下来的代码被包括在synchronized(this)中,this被用作取消息和写消息的锁。不过推断消息所指定的运行时间是否到了。

假设到了。就返回该消息。并将mMessages变量置空。假设时间还没有到,则尝试读取下一个消息。

3. 假设mMessages为空,则说明C环境中的消息队列没有可运行的消息了,因此,运行mPendingIdleHanlder列表中的“空暇回调函数”。

能够向MessageQueue中注冊一些“空暇回调函数”。从而当线程中没有消息可处理的时候去运行这些“空暇代码”。


enquenceMessage()函数

final boolean enqueueMessage(Message msg, long when) {
        if (msg.when != 0) {
            throw new AndroidRuntimeException(msg
                    + " This message is already in use.");
        }
        if (msg.target == null && !mQuitAllowed) {
            throw new RuntimeException("Main thread not allowed to quit");
        }
        final boolean needWake;
        synchronized (this) {
            if (mQuiting) {
                RuntimeException e = new RuntimeException(
                    msg.target + " sending message to a Handler on a dead thread");
                Log.w("MessageQueue", e.getMessage(), e);
                return false;
            } else if (msg.target == null) {
                mQuiting = true;
            }

            msg.when = when;
            //Log.d("MessageQueue", "Enqueing: " + msg);
            Message p = mMessages;
            if (p == null || when == 0 || when < p.when) {
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked; // new head, might need to wake up
            } else {
                Message prev = null;
                while (p != null && p.when <= when) {
                    prev = p;
                    p = p.next;
                }
                msg.next = prev.next;
                prev.next = msg;
                needWake = false; // still waiting on head, no need to wake up
            }
        }
        if (needWake) {
            nativeWake(mPtr);
        }
        return true;
    }
该函数内部分为两步:

1. 将參数msg赋值给mMessages。

2. 调用nativeWake(mPtr)。这是一个JNI函数,其内部会将mMessages消息加入到C环境中的消息队列中,并假设该消息线程正在等待(wait)状态,然后唤醒线程。


版权声明:本文博客原创文章,博客,未经同意,不得转载。