美文网首页
Message源码解读

Message源码解读

作者: 宋小超 | 来源:发表于2017-03-15 15:07 被阅读0次

    1、关于obtain()如何获取message对象

    源码如下:

    public static Messageobtain(){
      synchronized(sPoolSync) {
          if(sPool !=null) {
                Message m = sPool;
                sPool = m.next;
                m.next =null;
                m.flags =0;// clear in-use flagsPoolSize--;returnm;      
          }   
        }
        return new  Message();
    }

    分布示意图:

    1、Message m = sPool;


    2、关于sPool

    3、sPool = m.next;

    Message m 指向了spool

    原来的sPool指向了sPool.next

    4、m.next =null;            m.flags =0;

    切断联系

    2、关于recycleUnchecked()回收message

    源码如下

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

    1、next = sPool;

    2、sPool =this;

    总结

    1、obtain1() 方法取出来的message为第一个

    2、回收 message 时 将回收的message 放置在第一个

    参考:http://www.jianshu.com/p/f6f357b3db89

    相关文章

      网友评论

          本文标题:Message源码解读

          本文链接:https://www.haomeiwen.com/subject/dhvlnttx.html