转自http://blog.youkuaiyun.com/zlp1992/article/details/50295773
先看Message的静态成员方法 Message.obtain();
// sometimes we store linked lists of these things
/*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;
/**
* 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;
sPoolSize--;
return m;
}
}
return new Message();
}
可以看到obtain()方法说会从全局消息池中取消息,假设是第一次获得一个Message对象,那么sPool肯定为null,也就说第一次获取消息Message对象时是还没有消息池的,必须通过Message的构造方法获取一个Message对象的,Message的构造方法什么也没干
/** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}).
*/
public Message() {
}
那这就有点奇怪了,它的消息池是什么时候初始化呢?难道不是先new几个Message对象然后存着?这是我们注意到Message类的另一个成员方法recycle(),注意这个方法不是静态的
/**
* Return a Message instance to the global pool. You MUST NOT touch
* the Message after calling this function -- it has effectively been
* freed.
*/
public void recycle() {
clearForRecycle();
synchronized (sPoolSync) {
if (sPoolSize < MAX_POOL_SIZE) {
next = sPool;
sPool = this;
sPoolSize++;
}
}
}
里面的clearForRecycle()方法只是把一些成员变量置为null,以便垃圾回收
/*package*/ void clearForRecycle() {
flags = 0;
what = 0;
arg1 = 0;
arg2 = 0;
obj = null;
replyTo = null;
when = 0;
target = null;
callback = null;
data = null;
}
从这里我们就可以发现,消息池中Message的对象是通过recycle()放进去的. 但是我们自己并没有调用recycle()方法,那这个方法是在哪里调用的?看下Looper的源码就知道,在Looper的loop()方法的最后调用了Message对象msg的recycle()方法来回收这个Message对象,通过recycle()将这个Message对象的数据清空然后链接到消息池中(采用的头插法)。