Android-Message

源码解析

Java-链表

Message成员变量静态变量的含义

/**
 * 当前事可以序列化的
 */
public final class Message implements Parcelable {

    /**
     * 用户定义的消息代码,以便接收者(Handler)可以识别这个消息是关于什么的。 每个处理程序(Handle)
     * 都有自己的消息代码名称空间,因此您不必担心与其他处理程序(Handler)发生冲突。
     */
    public int what;

    /**
     * 这两个参数,如果用户只需要存储几个整数值,相比较于setData(Bundle)setData(),代价更低
     */
    public int arg1;
    public int arg2;

    /**
     * 这是一个发送给接受者的一个随意的数据,如果使用Messager来进行发送数据进行跨进程的通信,那么当前
     * 的obj如果实现了Parcelable就一定不能够为空指针,对于其他的数据的传输,我们一般使用setData方法就
     * 可以了。但是需要注意的是,对于高于android.os.Build.VERSION_CODES#FROYO的版本,这里的Parcelable
     * 对象是不支持的。
     **/
    public Object obj;

    /**
     * 可选的Messenger,可以发送对此消息的回复。如何使用的语义取决于发送者和接收者。
     */
    public Messenger replyTo;

    /*package*/ static final int FLAG_IN_USE = 1 << 0;  //标记消息被使用
    /*package*/ static final int FLAG_ASYNCHRONOUS = 1 << 1; // 标记消息是异步

    /**
     * flags表示这个Message有没有在使用,1表示在池中,等待复用,0表示正在被使用
     */
    /*package*/ int flags;

    /**
     * Message发送之后,何时才能被处理
     */
    /*package*/ long when;

    /**
     * Message所携带的数据 
     */
    /*package*/ Bundle data;

    /**
     * 表示这个消息被哪个Handler处理 这个就是发送处理message的Handler 
     */
    /*package*/ Handler target;

    /**
     * 用Handler.post一个Runnable,这个Runnable也是被包装成Message对象的
     */
    /*package*/ Runnable callback;

    /**
     * 表示下一个可用的消息(记录链表中的下一个元素)
     */
    /*package*/ Message next;

    /**
     * 第一个变量其实就是充当锁的作用,避免多线程争抢资源,导致脏数据
     */
    private static final Object sPoolSync = new Object();

    /**
     * sPool代码消息池的头消息
     */
    private static Message sPool;

    /**
     * sPoolSize表示有多少个可以被重用的对象(消息池中可用的消息个数即没有被使用的Message对象的个数)
     * 记录链表长度
     */
    private static int sPoolSize = 0;

    /**
     * 定义队列缓存消息的最大的长度(表示链表的最大长度)   Message 对象池中最多保存50个消息对象
     */
    private static final int MAX_POOL_SIZE = 50;

    /**
     * 消息池存放的核心方法
     */
    private static boolean gCheckRecycle = true;

}

Message的构造函数

    /**
     * Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}).
     * 不建议直接使用当前来创建一个Message对象 
     */
    public Message() {
      
    }

obtain()系列

/**
     * Same as {@link #obtain()}, but copies the values of an existing
     * message (including its target) into the new one.
     *
     * @param orig Original message to copy.
     * @return A Message object from the global pool.
     */
    public static Message obtain(Message orig) {
        Message m = obtain();
        m.what = orig.what;
        m.arg1 = orig.arg1;
        m.arg2 = orig.arg2;
        m.obj = orig.obj;
        m.replyTo = orig.replyTo;
        m.sendingUid = orig.sendingUid;
        m.workSourceUid = orig.workSourceUid;
        if (orig.data != null) {
            m.data = new Bundle(orig.data);
        }
        m.target = orig.target;
        m.callback = orig.callback;

        return m;
    }

    /**
     * Same as {@link #obtain()}, but sets the value for the target member on the Message returned.
     *
     * @param h Handler to assign to the returned Message object's target member.
     * @return A Message object from the global pool.
     */
    public static Message obtain(Handler h) {
        Message m = obtain();
        m.target = h;

        return m;
    }

    /**
     * Same as {@link #obtain(Handler)}, but assigns a callback Runnable on
     * the Message that is returned.
     *
     * @param h        Handler to assign to the returned Message object's target member.
     * @param callback Runnable that will execute when the message is handled.
     * @return A Message object from the global pool.
     */
    public static Message obtain(Handler h, Runnable callback) {
        Message m = obtain();
        m.target = h;
        m.callback = callback;

        return m;
    }

    /**
     * Same as {@link #obtain()}, but sets the values for both target and
     * what members on the Message.
     *
     * @param h    Value to assign to the target member.
     * @param what Value to assign to the what member.
     * @return A Message object from the global pool.
     */
    public static Message obtain(Handler h, int what) {
        Message m = obtain();
        m.target = h;
        m.what = what;

        return m;
    }

    /**
     * Same as {@link #obtain()}, but sets the values of the target, what, and obj
     * members.
     *
     * @param h    The target value to set.
     * @param what The what value to set.
     * @param obj  The object method to set.
     * @return A Message object from the global pool.
     */
    public static Message obtain(Handler h, int what, Object obj) {
        Message m = obtain();
        m.target = h;
        m.what = what;
        m.obj = obj;

        return m;
    }

    /**
     * Same as {@link #obtain()}, but sets the values of the target, what,
     * arg1, and arg2 members.
     *
     * @param h    The target value to set.
     * @param what The what value to set.
     * @param arg1 The arg1 value to set.
     * @param arg2 The arg2 value to set.
     * @return A Message object from the global pool.
     */
    public static Message obtain(Handler h, int what, int arg1, int arg2) {
        Message m = obtain();
        m.target = h;
        m.what = what;
        m.arg1 = arg1;
        m.arg2 = arg2;

        return m;
    }

    /**
     * Same as {@link #obtain()}, but sets the values of the target, what,
     * arg1, arg2, and obj members.
     *
     * @param h    The target value to set.
     * @param what The what value to set.
     * @param arg1 The arg1 value to set.
     * @param arg2 The arg2 value to set.
     * @param obj  The obj value to set.
     * @return A Message object from the global pool.
     */
    public static Message obtain(Handler h, int what,
                                 int arg1, int arg2, Object obj) {
        Message m = obtain();
        m.target = h;
        m.what = what;
        m.arg1 = arg1;
        m.arg2 = arg2;
        m.obj = obj;

        return m;
    }
最后的调用obtain()
    /**
     * 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) {
            // 第一次进来的时候 sPool 是null 走下面的 new 一个 Message
            // 注意 只有当 Message 第一次发生回收之后 sPool才会有值 看recycle()-->recycleUnchecked();
            if (sPool != null) {
                Message m = sPool; // 将上一个使用过的 Message 复用 这个就是链表的头部保存的那个消息对象
                sPool = m.next;    // 将当前的下一个作为链表的头部 
                m.next = null;     // 重置当前取出来的 
                m.flags = 0; // clear in-use flag
                sPoolSize--; // 从池子里面取出一个数据
                return m;
            }
        }
        // 创建一个 Message
        return new Message();
    }

消息的回收

    /**
     * Return a Message instance to the global pool.
     * 

* 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. *

*/ 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. */ @UnsupportedAppUsage 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 = UID_NONE; workSourceUid = UID_NONE; when = 0; target = null; callback = null; data = null; synchronized (sPoolSync) { // 加一把锁 if (sPoolSize < MAX_POOL_SIZE) { // 池子里面还有空间 next = sPool; // 将回收的对象添加到链表的头部 sPool = this; sPoolSize++; // 记录一下池子的数量 } } }

什么时候调用的回收

总结

  • Message的消息池原来是一个链表
  • 每一个Message 对象通过next指向下一个Message(最后一个Message的next为null)形成一个链表,Message对象就成了一个可用的Message池。
  • 消息池是一些回收的对象,也就是说Message对象是在回收的时候将其添加到链表中的。
  • private static Message sPool; 这个是static的 。


你可能感兴趣的:(Android-Message)