摘要

Android应用程序是通过消息来驱动的,当Android主线程启动时就会在内部创建一个消息队列。然后进入一个无限循环中,轮询是否有新的消息需要处理。如果有新消息就处理新消息。如果没有消息,就进入阻塞状态,直到消息循环被唤醒。

那么在Android系统中,消息处理机制是怎么实现的呢?在程序开发时,我们经常会使用Handler处理Message(消息)。所以可以知道Handler是个消息处理者,Message是消息主体。除此之外还有消息队列和消息轮询两个角色。它们分别是MessageQueue和Looper,MessageQueue就是消息队列,Looper负责轮询消息。

简介

我们已经知道Android的消息机制处理主要由Handler、Message、MessageQueue、Looper四个类的实现来完成。那么它们之间的关系是怎样的?

其中,Message是消息主体,它负责存储消息的各种信息,包括发送消息的Handler对象、消息信息、消息标识等。MessageQueue就是消息队列,在其内部以队列的形式维护一组Message(消息)。Handler负责发送和处理消息。Looper负责轮询消息队列。

Android探索之路(一)——消息处理机制_第1张图片

Android消息机制原理

创建线程消息队列

在Android应用程序中,消息处理程序运行前首先要创建消息队列(也就是MessageQueue)。在主线程中,通过调用Looper类的静态成员函数prepareMainLooper()来创建消息队列。在其他子线程中,通过调用静态成员函数prepare()来创建。

prepareMainLooper()与prepare()的实现:

/**     * 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()}     * 用来初始化主线程中的Looper,有Android环境调用,不应该有用户调用.     */    public static void prepareMainLooper() {        prepare(false);        synchronized (Looper.class) {            if (sMainLooper != null) {                throw new IllegalStateException("The main Looper has already been prepared.");            }            sMainLooper = myLooper();        }    }    public static @Nullable Looper myLooper() {        return sThreadLocal.get();    } /** Initialize the current thread as a looper.      * This gives you a chance to create handlers that then reference      * this looper, before actually starting the loop. Be sure to call      * {@link #loop()} after calling this method, and end it by calling      * {@link #quit()}.      * 交给用户自己调用,通过loop()方法开启消息循环.同时当不需要处理消息时,需要手动调用quit()方法退出循环.      */    public static void prepare() {        prepare(true);    }    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));    }

在这两个函数调用的过程中,sThreadLocal变量都有被使用。这个变量是ThreadLocal类型的,用来保存当前线程中的Looper对象。也就是说在Android应用程序中每创建一个消息队列,都有一个并且是唯一 一个与之对应的Looper对象。而且我们可以从源码中看到当对象不唯一时就会抛出异常。

private Looper(boolean quitAllowed) {        mQueue = new MessageQueue(quitAllowed); //创建消息队列        mThread = Thread.currentThread();    }

从上面的源码中可以看到,当Looper对象实例化的过程的同时会创建一个消息队列。

消息循环过程

在消息队列建立完成之后,调用Looper对象的静态成员方法loop()就开始了消息循环。

    /**     * 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(); // 在接收消息时有可能阻塞            if (msg == null) {                //message为null时,退出消息循环                return;            }            // This must be in a local variable, in case a UI event sets the logger            final Printer logging = me.mLogging;            if (logging != null) {...}            final long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;            final long traceTag = me.mTraceTag;            if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {...}            final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();            final long end;            try {                msg.target.dispatchMessage(msg);  //处理消息                end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();            } finally {                if (traceTag != 0) {                    Trace.traceEnd(traceTag);                }            }            if (slowDispatchThresholdMs > 0) {                final long time = end - start;                if (time > slowDispatchThresholdMs) {...}            }            if (logging != null) {...}            // Make sure that during the course of dispatching the            // identity of the thread wasn't corrupted.            final long newIdent = Binder.clearCallingIdentity();            if (ident != newIdent) {...}            msg.recycleUnchecked();        }    }

上面的源码就是消息循环的过程,只用调用了loop()方法消息循环才开始起作用。当循环开始时:

  • 获取当前线程的Looper对象,如果为null,抛出异常;

  • 获取消息队列,开始进入消息循环;

  • 从消息队列中获取消息(调用MessageQueue的next()方法),如果为null,结束循环;否则,继续执行;

  • 处理消息,回收消息资源( msg.recycleUnchecked())。

在消息循环过程中,通过MessageQueue的next()方法提供消息,在没有信息时进入睡眠状态,同时处理其他接口。这个过程至关重要,通过next()方法也决定了消息循环是否退出。

 Message next() {        final long ptr = mPtr; //与native方法相关,当mPtr为0时返回null,退出消息循环        if (ptr == 0) {            return null;        }        int pendingIdleHandlerCount = -1; // -1 only during first iteration        int nextPollTimeoutMillis = 0;  //0不进入睡眠,-1进入书面        for (;;) {            if (nextPollTimeoutMillis != 0) {            //处理当前线程中待处理的Binder进程间通信请求                Binder.flushPendingCommands();              }            //native方法,nextPollTimeoutMillis为-1时进入睡眠状态            nativePollOnce(ptr, nextPollTimeoutMillis);             synchronized (this) {                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;                }                // 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.            nextPollTimeoutMillis = 0;        }    }

消息循环退出过程

从上面可以看到loop()方法是一个死循环,只有当MessageQueue的next()方法返回null时才会结束循环。那么MessageQueue的next()方法何时为null呢?

在Looper类中我们看到了两个结束的方法quit()和quitSalely()。两者的区别就是quit()方法直接结束循环,处理掉MessageQueue中所有的消息,而quitSafely()在处理完消息队列中的剩余的非延时消息(延时消息(延迟发送的消息)直接回收)时才退出。这两个方法都调用了MessageQueue的quit()方法。

void quit(boolean safe) {        if (!mQuitAllowed) {            throw new IllegalStateException("Main thread not allowed to quit.");        }        synchronized (this) {            if (mQuitting) {                return;            }            mQuitting = true; //设置退出状态//处理消息队列中的消息            if (safe) {                removeAllFutureMessagesLocked();  //处理掉所有延时消息            } else {                removeAllMessagesLocked(); //处理掉所有消息            }            // We can assume mPtr != 0 because mQuitting was previously false.            nativeWake(mPtr);  // 唤醒消息循环        }    }

处理消息队列中的消息:根据safe标志选择不同的处理方式。

/** * API Level 1 * 处理掉消息队列中所有的消息 */private void removeAllMessagesLocked() {        Message p = mMessages;        while (p != null) {            Message n = p.next;            p.recycleUnchecked();  //回收消息资源            p = n;        }        mMessages = null;    }/** * API Level 18 * 处理掉消息队列中所有的延时消息 */    private void removeAllFutureMessagesLocked() {        final long now = SystemClock.uptimeMillis();        Message p = mMessages;        if (p != null) {            if (p.when > now) {                removeAllMessagesLocked();            } else {                Message n;                for (;;) {                    n = p.next;                    if (n == null) {                        return;                    }                    if (n.when > now) {                    //找出延时消息                        break;                    }                    p = n;                }                p.next = null;                //由于在消息队列中按照消息when(执行时间排序,所以在第一个延时消息后的所有消息都是延时消息)                do {                    p = n;                    n = p.next;                    p.recycleUnchecked(); //回收消息资源                } while (n != null);            }        }    }

消息发送过程

在Android应用程序中,通过Handler类向线程的消息队列发送消息。在每个Handler对象中持有一个Looper对象和MessageQueue对象。

public Handler(Callback callback, boolean async) {        if (FIND_POTENTIAL_LEAKS) {            final Class<? extends Handler> klass = getClass();            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&                    (klass.getModifiers() & Modifier.STATIC) == 0) {                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +                    klass.getCanonicalName());            }        }        mLooper = Looper.myLooper(); //获取Looper对象        if (mLooper == null) {...}        mQueue = mLooper.mQueue;  //获取消息队列        mCallback = callback;        mAsynchronous = async;    }

在Handler类中,我们可以看到多种sendMessage方法,而它们最终都调用了同一个方法sendMessageAtTime()方法。

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

可以看出这两个方法十分容易理解,就是通过MessageQueue对象调用enqueueMessage()方法向消息队列中添加消息。

boolean enqueueMessage(Message msg, long when) {// Handler为null        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;            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;    }

从源码可以看出,一个消息插入到消息队列中需要以下步骤:

  • 消息持有的Handler对象为null,抛出异常;当消息已经被消费,抛出异常;

  • 当消息队列没有消息时,直接插入;

  • 当消息队列存在消息时,通过比较消息的执行时间,将消息插入到相应的位置;

  • 判断是否需要唤醒消息循环。

消息处理过程

在消息循环过程中,如果有新的消息加入,就开始处理消息。从上面的分析中,我们可以看到在消息循环中,目标消息会调用其Handler对象的dispatchMessage()方法,这个就是处理消息的方法。

 /**     * Handle system messages here.     */    public void dispatchMessage(Message msg) {    // 消息Callback接口不为null,执行Callback接口        if (msg.callback != null) {            handleCallback(msg);        } else {            if (mCallback != null) {            //Handler Callback接口不为null,执行接口方法                if (mCallback.handleMessage(msg)) {                    return;                }            }            handleMessage(msg); //处理消息        }    }

从源码可以看出,Handler处理消息分为3中情况。

  1. 当Message中的callback不为null时,执行Message中的callback中的方法。这个callback时一个Runnable接口。

  2. 当Handler中的Callback接口不为null时,执行Callback接口中的方法。

  3. 直接执行Handler中的handleMessage()方法。

当Looper开始调用loop()时主线程为什么不会卡死

在进行完上面的分析后,我们都知道Looper.loop()进入到了一个死循环,那么在主线程中执行这个死循环为什么没有造成主线程卡死或者说在主线程中的其他操作还可以顺利的进行,比如说UI操作。因为Android应用程序是通过消息驱动的,所以Android应用程序的操作也是通过Android的消息机制来实现的。这个时候就需要分析一下Android程序启动的入口类ActivityThread。我们都知道当Android程序启动时在Java层就是以ActivityThread的main()方法为入口的,这也就是我们所说的主线程。

public static void main(String[] args) {        ...        ...        ...        Looper.prepareMainLooper();        ActivityThread thread = new ActivityThread();        thread.attach(false); //建立Binder通道 (创建新线程),与ActivityManagerService建立链接        if (sMainThreadHandler == null) {            sMainThreadHandler = thread.getHandler();        }.........        Looper.loop();        throw new RuntimeException("Main thread loop unexpectedly exited");    }

从ActivityThread的main()方法中我们可以看到Looper的初始化以及消息循环的开始,同时还有一个关键的方法attach()与ActivityManagerService建立链接,这里建立链接是为了之后相应Activity中各种事件的发生。讲到这里还涉及到Native层Looper的初始化,在Looper初始化时会建立一个管道来维护消息队列的读写并通过epoll机制监听读写事件(一种IO多路复用机制)。

  • 当没有新消息需要处理时,主线程就会阻塞在管道上,直到有新的消息需要处理;

  • 当其他线程有消息发送到消息队列时会通过管道来写数据;

在我们调试程序时,我们通过函数的调用栈就可以发现其中的道理:

Android探索之路(一)——消息处理机制_第2张图片

这也印证了开始的那句话——Android应用程序是通过消息来驱动的。

总结

至此Android系统的消息处理机制就分析完毕了。在Android应用程序中消息处理主要分为3个过程:

  1. 启动Looper中的消息循环,开始监听消息队列。

  2. 通过Handler发送消息到消息队列。

  3. 通过消息循环调用Handler对象处理新加入的消息。

在使用消息队列时,主线程中在程序启动时就会创建消息队列,所以我们使用主线程中的消息机制时,不需要初始化消息循环和消息队列。在子线程中我们需要初始化消息队列,并且注意在不需要使用消息队列时,应该及时调用Looper的quit或者quitSafely方法关闭消息循环,否则子线程可能一直处于等待状态。

更多相关文章

  1. 在Android设备上搭建Web服务器的方法
  2. Android消息机制的源码理解
  3. Android setTag方法的key问题
  4. Android RSA与Java RSA加密不同标准产生问题的解决方法
  5. Android sdk manager不能更新下载缓慢的解决方法
  6. android截屏代码实现方法
  7. Android Cannas.drawTextView 方法坐标问题

随机推荐

  1. Netty学习心得 netty服务端和客户端的连
  2. 线程“main”中的异常java.lang.RuntimeE
  3. java中jcom操作excel
  4. JavaWeb-1-IOS或Android客户端上传图片到
  5. 使用超类中的构造函数
  6. execl中设置的格式无法实现
  7. Java ThreadPoolExecutor 线程池调度器
  8. C/C++比java效率高的原因?
  9. 新手求教RandomAccessFile读写文件到底写
  10. Java中double型数组的HashCode产生