Android 消息机制之深入学习MessageQueue

一,简述

      MessageQueue在Android中指消息队列,顾名思义就是存放消息的消息池,但是它的内部实现并不是队列而是一个单链表,可能是单链表的删除和插入比较有优势吧MessageQueue的内部对消息的主要操作就是插入,读取删除,不具备处理消息的能力。

二,源码分析

1,重要属性信息介绍

  // True if the message queue can be quit.
    private final boolean mQuitAllowed;

    @SuppressWarnings("unused")
    private long mPtr; // used by native code

    Message mMessages;
    private final ArrayList mIdleHandlers = new ArrayList();
    private SparseArray mFileDescriptorRecords;
    private IdleHandler[] mPendingIdleHandlers;
    private boolean mQuitting;

    // Indicates whether next() is blocked waiting in pollOnce() with a non-zero timeout.
    private boolean mBlocked;

mQuitAllowed:是否允许MessageQueue退出

mPtr:可以理解成是C/C++的指针,是一个内存地址,当为0的时候说明消息队列被释放了

mMessage:表示存储消息链表的头Head

mQuitting:当前队列是否处于正在退出状态

mBlocked:是否被阻塞

2,构造方法

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

可以看到创建MessageQueue的时候就需要指定MessageQueue是否可以退出。nativeInit();是一个Jni方法用来初始化mPtr

3,enqueueMessage(Message msg,long when)

 boolean enqueueMessage(Message msg, long when) {
      msg.target 指消息机制中的Handler
       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(TAG, e.getMessage(), e);
                msg.recycle();
                return false;
            }

            msg.markInUse();
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            将新消息放在链表头部的条件:
            1,队列为空 ;2,接收到的新消息需要立即处理:when = 0;3,新消息等待处理的时间比链表队头要短
            这时候只需要将新消息的next指向当前链表的头部,让mMessages指向新消息,如果当前的队列是阻塞的就唤醒队列
         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(); 
               遍历处理新消息的位置:prev指向p的上一个消息,p开始向队尾移动,如果新消息需要执行的等待时间小于p所指向的消息,
               就将新消息放在prev和p之间
                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.
            如果需要唤醒队列,调用nativeWake(mPtr);唤醒队列
            if (needWake) {
                nativeWake(mPtr);
            }
        }
        return true;
    }

enqueueMessage();是MessageQueue的核心方法,主要功能是向MessageQueue中插入消息

4,next()

 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;
        mPtr == 0说明消息队列被释放了
        if (ptr == 0) {
            return null;
        }

        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;
        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }
           阻塞操作,等待nextPollTimeoutMillis时长
            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;
                }

                如果消息队列正在处于退出状态返回null,调用dispose();释放该消息队列
                // 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(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.
            nextPollTimeoutMillis = 0;
        }
    }

next();方法是MessageQueue的和新方法也是比较难理解的一个方法。主要做的工作是从消息队列中取出消息。

 void removeMessages(Handler h, int what, Object object) {
        if (h == null) {
            return;
        }

        synchronized (this) {
            Message p = mMessages;

            // Remove all messages at front.
            while (p != null && p.target == h && p.what == what
                   && (object == null || p.obj == object)) {
                Message n = p.next;
                mMessages = n;
                p.recycleUnchecked();
                p = n;
            }

            // Remove all messages after front.
            while (p != null) {
                Message n = p.next;
                if (n != null) {
                    if (n.target == h && n.what == what
                        && (object == null || n.obj == object)) {
                        Message nn = n.next;
                        n.recycleUnchecked();
                        p.next = nn;
                        continue;
                    }
                }
                p = n;
            }
        }
    }

移除消息队列中的消息removeMessage();这个方法有两个重载方法,大致逻辑是一样的。在这个方法中有两个while循环,对这两个while讲解比较详细的博客。

上面说过MessageQueue是一个链表,链表分两种:带头节点的不带头节点的。这两种链表的遍历方式不同:不带头节点的链表中,第一个元素需要单独处理,然后将后续部分当作是带头节点的链表使用while循环遍历。MessageQueue是不带头节点的链表,所以我们可以看到有两个while循环。

第一个while循环:如果队列中的第一个Message的target,what,object与指定的handler,what,object相同,删除第一个元素。后续部分就成了带头节点的链表。第二个循环删除链表中后续中符合条件的Message

以上就是对MessageQueue的全部分析内容,有不足的地方或是有误的地方欢迎大家提出指正。

你可能感兴趣的:(Android)