android消息处理机制之Message是如何重复利用的?

  • 问题的引出

我们知道Android开发中我们是一般不会在子线程中去更新UI,而是利用Handler将当前子线程的消息post(Runnable)到主线程中去,这样就可以安全的更新UI了,看过Handler源码的同学应该知道post(Runnable
)底层就是将Runnable转化为Message然后交给Handler去处理的。这时我们发现一个问题,那就是在android中大量的UI更新是不是会创建大量的Message呢?

  • 探究android是如何去处理处理大量的Message

看看Message源码类注释,从下面的注释可以知道,它推荐我们通过Message.obtain()的方式去获取一个Message使用,还有我们看到一个关键字”recycled”,说明通过这个方法获取的Message是从回收池pool中获取的。

/**
 * 
 * Defines a message containing a description and arbitrary data object that can be
 * sent to a {@link Handler}.  This object contains two extra int fields and an
 * extra object field that allow you to not do allocations in many cases.  
 *
 * <p class="note">While the constructor of Message is public, the best way to get
 * one of these is to call {@link #obtain Message.obtain()} or one of the
 * {@link Handler#obtainMessagerecycleUnchecked Handler.obtainMessage()} methods, which will pull
 * them from a pool of recycled objects.</p>
 */

  • 关注怎么怎么从回收池中获取Message的

在首次调用obtain方法时,①当前pool中没有Message可用,因此会创建一个Message对象②当前pool有Message可用则直接取出表头Message。现在我发现一个问题,那就是创建了一个Message对象之后,它是什么时候添加到pool中的?

  • next:是Message的属性,是Message类型的,指向的是下一个Messae对象,因此从这点可以看出,它是一个链表的方式实现的。
  • sPoolSync:一个普通的object,纯粹是为了同步锁使用
  • sPool:表头
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--;
            return m;
        }
    }
    return new Message();
}

public Message() {
}
  • 探索Message是怎么被存到链表中的?

首先我们要Handler的工作需要谁来配合?很简单,那就是Message,MessageQueue和Looper。下面我简单的说说每一个类的作用,详细请参考另一篇博客[Handler消息处理机制][1]
[1]:http://blog.csdn.net/lwj_zeal/article/details/52814068 “Handler消息处理机制”

  • Message:Handler处理的消息。
  • MessageQueue:消息队列,专门负责存储消息的。
  • Looper:消息轮训器,轮训消息队列获取消息交给Handler去处理。

知道了上面的关系之后,那么我们得找到切入点,我们知道Looper是负责轮训消息队列的,然后取出消息交给Handler去处理,那么我们就可以猜测它既然可以取消息,那么肯定需要解决handler处理的Message的,追踪源码Looper.loop()瞅瞅:

public static void loop() {
    final Looper me = myLooper();
    ...
    for (;;) {//阻塞
        //取出一个消息
        Message msg = queue.next(); // might block
        if (msg == null) {
            // No message indicates that the message queue is quitting.
            return;
        }
        ...
        try {
            //让handler去处理这个消息
            msg.target.dispatchMessage(msg);
        } finally {
            if (traceTag != 0) {
                Trace.traceEnd(traceTag);
            }
        }
        //handler处理完消息之后,回收消息
        //关键代码
        msg.recycleUnchecked();
    }
}

Message:recycleUnchecked
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;
    synchronized (sPoolSync) {
        //当前pool中的Message还没有超过MAX_POOL_SIZE时
        //将将消息添加到链表中,这里就实现了pool添加Message的操作了
        if (sPoolSize < MAX_POOL_SIZE) {
            next = sPool;
            sPool = this;
            sPoolSize++;
        }
    }
}
  • 结论

从Message类的源码注释中也可以看出,它推荐通过Message.obtain()方法获取一个Message对象,这个可以减少内存的消耗,也是比较规范的写法。

    原文作者:未见哥哥
    原文地址: https://www.jianshu.com/p/b780ac97a9d2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞