Android 消息机制之Message
@(Android)
Android的消息机制中,Message的使用是很频繁的,处理消息,处理事件,处理其他我还没有探索的新世界。在Android的消息机制中,Message相当于承载消息的载体,Handler就是Message的处理者,而MessageQueue和Looper就是Message的搬运工。
在Message的内部有一个flag的标志位,用于标识当前Message的状态。有一个标志位是FLAG_IN_USE ,当Message在队列中,正在给Handler处理的时候,就会加上这个标志位,表示当前的Message正在使用,那么为什么要记录Message是否正在被使用呢。因为Message内部有一个对Message的回收机制。
因为Message作为Android系统消息的载体,那么若有大量消息需要处理的时候,就需要大量的Message,就需要不断的new Message吗,显然这样的很浪费时间的。所以Message内部弄了一个回收机制来管理Message。
Message的回收机制
和之前的文章一样,既然是回收机制,那么自然会想到的问题就是,怎么回收,回收之后怎么利用?我们在学习Handler的时候 ,Google推荐我们使用Message的静态方法obtain来得到一个新的Message实例。所以我们可以确定的是,回收之后的Message就是通过obtain来获取新的Message的。那我们来看看obtain的源码。
我们能看到的Message有很多obtain的重载,但是都使用了下面的无参方法,所以我们只看这份代码就可以了
public static Message obtain() {
synchronized (sPoolSync) {
// sPool 就是Message的缓存池啦
if (sPool != null) {
// 下面几行代码就是链表的操作而已
Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
// 缓存池的大小
sPoolSize--;
return m;
}
}
return new Message();
}
obtain方法不难,而且代码很多,很容易理解吧。所以我们继续寻找Message是在哪里被回收的。我找到了下面的两个方法:
/**
* Return a Message instance to the global pool.
* <p>
* 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.
* </p>
*/
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.
*/
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) {
if (sPoolSize < MAX_POOL_SIZE) {
next = sPool;
sPool = this;
sPoolSize++;
}
}
}
可以看到上面的方法就是我们需要的啦,回收Message的方法。在recycle()一开始就判断了这个Message是否在使用,如果在使用就不回收啦。我之前一直以为Message的回收机制这些东西很高大上,其实在recycleUnchecked
里面只是对Message的所有成员变量清空,初始化一遍而已啊。最后缓冲池的大小再加一。
Message的其他东西
Message的源码不是很多,所以我还看了别的代码:
public void copyFrom(Message o) {
this.flags = o.flags & ~FLAGS_TO_CLEAR_ON_COPY_FROM;
this.what = o.what;
this.arg1 = o.arg1;
this.arg2 = o.arg2;
this.obj = o.obj;
this.replyTo = o.replyTo;
this.sendingUid = o.sendingUid;
if (o.data != null) {
this.data = (Bundle) o.data.clone();
} else {
this.data = null;
}
}
这个方法是复制一个Message,除了Mesasge的data数据之外,其他都是浅拷贝的。我觉得这个在平时的开发中可能会写错的,所有今天写下来记录一下
有一点我还是没有想明白的,为什么Message要分同步和异步啊,有没有dalao留个言说说什么回事啊