欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Message

程序员文章站 2022-05-25 13:01:29
...
  • Messeage 是安卓消息机制中,消息的实体,表示需要处理的消息内容;

本文介绍两部分:
1. 消息的构成
2.消息的缓冲池


首先看下类的注释文档:

 * 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#obtainMessage Handler.obtainMessage()} methods, which will pull
 * them from a pool of recycled objects.</p>

Message是发送到handler的消息对象,包括两个int变量和一个对象等
建议获取Message时采用缓冲池的方法,即Message.obtain() or Handler.obtainMessage()

  • Message的构成
    消息标识: (每个handler的命名空间独立,无需担心冲突问题)

public int what;

存储int类型

public int arg1;
public int arg2;

存储对象

public Object obj;

存储数据

Bundle data
标明指定处理的handler
Handler target

runnable

Runnable callback;


  • 缓存池

/package/ Message next;
private static final Object sPoolSync = new Object();
private static Message sPool; // 当前缓冲池的头结点
private static int sPoolSize = 0;
private static final int MAX_POOL_SIZE = 50;
private static boolean gCheckRecycle = true;

采取链表的方式获取到当前可用缓冲池的头结点,若缓冲池为空,则新建一个(注意新建的对象怎么加入缓冲池?后续会有)

/**
     * 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--;
                return m;
            }
        }
        return new Message();
    }

回收消息到缓冲池

    /**
     * Return a Message instance to the global pool.
     * <p>
     * You MUST NOT touch the Message after calling this function because it has
     * effectively been freed.  It is an error to recycle a message that is currently
     * enqueued or that is in the process of being delivered to a Handler.
     * </p>
     */
    public void recycle() {
        if (isInUse()) {
            if (gCheckRecycle) {
                throw new IllegalStateException("This message cannot be recycled because it "
                        + "is still in use.");
            }
            return;
        }
        recycleUnchecked();
    }

头插法插入结点

/**
     * 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;
        //生成一个缓冲池,大小为MAX_POOL_SIZE
        synchronized (sPoolSync) {
            if (sPoolSize < MAX_POOL_SIZE) {
                next = sPool;
                sPool = this;
                sPoolSize++;
            }
        }
    }

最后呢:看下Handler.obtainMessage怎么实现的

/**
     * Returns a new {@link android.os.Message Message} from the global message pool. More efficient than
     * creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
     *  If you don't want that facility, just call Message.obtain() instead.
     */
    public final Message obtainMessage()
    {
        return Message.obtain(this);
    }
/**
     * Same as {@link #obtain()}, but sets the value for the <em>target</em> member on the Message returned.
     * @param h  Handler to assign to the returned Message object's <em>target</em> member.
     * @return A Message object from the global pool.
     */
    public static Message obtain(Handler h) {
        Message m = obtain();
        m.target = h;

        return m;
    }

可见和Message.obtain()一样的,难怪建议直接用obtain获取~~

转载于:https://www.jianshu.com/p/6aa968e49ab9