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;
![](https://img.haomeiwen.com/i2707100/e89b8f402c5eced6.png)
2、关于sPool
![](https://img.haomeiwen.com/i2707100/740becec7e90b34b.png)
3、sPool = m.next;
![](https://img.haomeiwen.com/i2707100/91136a5275592eae.png)
Message m 指向了spool
原来的sPool指向了sPool.next
4、m.next =null; m.flags =0;
![](https://img.haomeiwen.com/i2707100/cfb19b02d92974a8.png)
切断联系
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;
![](https://img.haomeiwen.com/i2707100/a531a550aec0e922.png)
2、sPool =this;
![](https://img.haomeiwen.com/i2707100/f265043d3e345912.png)
总结
1、obtain1() 方法取出来的message为第一个
2、回收 message 时 将回收的message 放置在第一个
参考:http://www.jianshu.com/p/f6f357b3db89
网友评论