Code Fragment-Message Pool的实现与处理

Android Message实现处理大致如下


涉及到的几个问题

  • Message的数据结构:通过next成员,保持一种链表的链接关系,这样做的好处是在没有超出poolsize的时候,始终只有一个message对象

  1. // sometimes we store linked lists of these things  
  2. /*package*/ Message next;  
// sometimes we store linked lists of these things
/*package*/ Message next;

  • pool的处理
    • 形式:就是一个静态的节点,由于next的链表结构,能够保持一种链接。其中sPool总是指向最上面的meesage pool中最上面的message对象。
    • 大小:
      • 初始大小
          1. private static int sPoolSize = 0;  
          private static int sPoolSize = 0;
      • 限制大小
          1. private static final int MAX_POOL_SIZE = 10;  
          private static final int MAX_POOL_SIZE = 10;

      • 增长
          1. public void recycle() {  
          2.     clearForRecycle();  
          3.   
          4.     synchronized (sPoolSync) {  
          5.         if (sPoolSize < MAX_POOL_SIZE) {//判断大小是否满足最大值  
          6.             next = sPool;  
          7.             sPool = this;  
          8.             sPoolSize++;//设置增长  
          9.         }  
          10.     }  
          11. }  
          public void recycle() {
              clearForRecycle();
          
              synchronized (sPoolSync) {
                  if (sPoolSize < MAX_POOL_SIZE) {//判断大小是否满足最大值
                      next = sPool;
                      sPool = this;
                      sPoolSize++;//设置增长
                  }
              }
          }
      • 减少
          1. public static Message obtain() {  
          2.     synchronized (sPoolSync) {  
          3.         if (sPool != null) {  
          4.             Message m = sPool;  
          5.             sPool = m.next;  
          6.             m.next = null;  
          7.             sPoolSize--;//减少  
          8.             return m;  
          9.         }  
          10.     }  
          11.     return new Message();  
          12. }  
          public static Message obtain() {
              synchronized (sPoolSync) {
                  if (sPool != null) {
                      Message m = sPool;
                      sPool = m.next;
                      m.next = null;
                      sPoolSize--;//减少
                      return m;
                  }
              }
              return new Message();
          }
    • 节点增删
      • 向pool中添加节点
          1. public void recycle() {  
          2.     clearForRecycle();  
          3.   
          4.     synchronized (sPoolSync) {  
          5.         if (sPoolSize < MAX_POOL_SIZE) {//判断当前pool的大小,当已经满足pool的上限的时候,不再添加  
          6.             next = sPool;//把当前节点的next指向pool中最上面的节点,建立链表关系  
          7.             sPool = this;//把sPool指针上移,指向新添加的节点  
          8.             sPoolSize++;  
          9.         }  
          10.     }  
          11. }  
          public void recycle() {
              clearForRecycle();
          
              synchronized (sPoolSync) {
                  if (sPoolSize < MAX_POOL_SIZE) {//判断当前pool的大小,当已经满足pool的上限的时候,不再添加
                      next = sPool;//把当前节点的next指向pool中最上面的节点,建立链表关系
                      sPool = this;//把sPool指针上移,指向新添加的节点
                      sPoolSize++;
                  }
              }
          }


      • 从pool中获取节点
          1. public static Message obtain() {  
          2.     synchronized (sPoolSync) {  
          3.         if (sPool != null) {//判断pool是不是空的,如果非空,从pool里取出最上面的节点  
          4.             Message m = sPool;//获取最上面message的引用  
          5.             sPool = m.next;//sPool节点下移,相当于将上面的节点弹出。  
          6.             m.next = null;  
          7.             sPoolSize--;//大小减一  
          8.             return m;  
          9.         }  
          10.     }  
          11.     return new Message();//如果pool是空的,直接创建一个新的message去使用  
          12. }  
          public static Message obtain() {
              synchronized (sPoolSync) {
                  if (sPool != null) {//判断pool是不是空的,如果非空,从pool里取出最上面的节点
                      Message m = sPool;//获取最上面message的引用
                      sPool = m.next;//sPool节点下移,相当于将上面的节点弹出。
                      m.next = null;
                      sPoolSize--;//大小减一
                      return m;
                  }
              }
              return new Message();//如果pool是空的,直接创建一个新的message去使用
          }
    • 线程安全
      • 同步对象
          1. private static final Object sPoolSync = new Object();  
          private static final Object sPoolSync = new Object();
      • 增删加锁
          1. public static Message obtain() {  
          2.     synchronized (sPoolSync) {  
          3.         if (sPool != null) {//加锁  
          4.             ...//从pool中弹出message  
          5.         }  
          6.     }  
          7.     ....   
          8. }  
          public static Message obtain() {
              synchronized (sPoolSync) {
                  if (sPool != null) {//加锁
                      ...//从pool中弹出message
                  }
              }
              .... 
          }
          1. public void recycle() {  
          2.     ....  
          3.   
          4.     synchronized (sPoolSync) {//加锁  
          5.         if (sPoolSize < MAX_POOL_SIZE) {  
          6.             ...//向pool中添加message  
          7.         }  
          8.     }  
          9. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值