关于Android Handler同步屏障那些事
前一段时间,肥柴突然听到一句:Handler同步屏障,一脸懵逼,啥玩意?
于是就去了解并总结了一下Handler的异步消息和同步屏障的相关知识,只能说,肥柴确实学到了。
一、Handler Message种类
首先我们来了解一下Handler的Message种类:
1、普通消息
2、异步消息
3、屏障消息
其中普通消息又称为同步消息,肥柴平时发送的消息基本都是同步消息,而我们又基本很少有发送异步甚至屏障消息的需求,所以就没怎么了解到。
二、异步消息
其实肥柴在“一切从Android的Handler讲起”的专栏中分析了Handler的原理,其中涉及到了MessageQueue,我们平时的用法就是直接向MessageQueue添加Message,Looper从MessageQueue依次获取Message并处理,很明显就是同步消息。
那如何添加一个异步的Message呢?有两种方式。
1、Handler构造方法设置全局为异步消息
Handler的构造方法有个async参数,默认的构造方法此参数是false,只要我们构造Handler对象的时候,设置async为true即可。
/** Handler.class */
public Handler(@Nullable Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread " + Thread.currentThread()
+ " that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
/** 注释1 异步消息标志位 */
mAsynchronous = async;
}
async设置为true后,对全局的mAsynchronous设置为true。然后在enqueueMessage()方法里,调用msg.setAsynchronous(true),将message设置为异步的。
/** Handler.class */
private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
long uptimeMillis) {
msg.target = this;
msg.workSourceUid = ThreadLocalWorkSource.getUid();
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
2、将Message设置为异步消息
在创建Message对象时,直接调用Message的setAsynchronous(true)方法。这就雷同于上述方法,只不过这里是针对当个Message,方法1是针对所有插入MessageQueue的Message都会被设置为异步的。
那问题来了,异步消息和我们平时使用的同步消息到底有什么区别呢?
答案是:没有区别。啊哈哈哈哈。
先别打肥柴,听我细细说来,事实上,在一般情况下,异步消息和同步消息没有什么区别,但是一旦开启了同步屏障以后就有区别了。
三、同步屏障
所以,肥柴为了避免被打,就需要来聊聊同步屏障了。
一般来说,MessageQueue里面的所有Message是按照时间从前往后有序排列的。这一点肥柴在“一切从Android的Handler讲起”专栏中已经提到了。
同步屏障消息就是在消息队列中插入一个屏障,在屏障之后的所有普通消息都会被挡着,不能被处理。不过异步消息却例外,屏障不会挡住异步消息,因此可以认为,屏障消息就是为了确保异步消息的优先级,设置了屏障后,只能处理其后的异步消息,同步消息会被挡住,除非撤销屏障。
同步屏障是通过MessageQueue的postSyncBarrier方法开启的。
/** MessageQueue.class */
private int postSyncBarrier(long when) {
// Enqueue a new sync barrier token.
// We don't need to wake the queue because the purpose of a barrier is to stall it.
synchronized (this) {
// 步骤1 获取屏障的的唯一标示
final int token = mNextBarrierToken++;
// 步骤2 设置屏障消息
final Message msg = Message.obtain();
msg.markInUse();
msg.when = when;
msg.arg1 = token;
// 步骤3 消息队列中的第一个Message赋值给p
Message prev = null;
Message p = mMessages;
if (when != 0) {
// 步骤4 通过p的时间和屏障的时间,确定屏障消息插入的位置
while (p != null && p.when <= when) {
prev = p;
p = p.next;
}
}
if (prev != null) { // invariant: p == prev.next
// 步骤5 说明屏障消息不是插入消息队列的头部
msg.next = p;
prev.next = msg;
} else {
// 步骤6 屏障消息在消息队列的头部
msg.next = p;
mMessages = msg;
}
return token;
}
}
从开启同步屏障的postSyncBarrier()方法的源码,我们可以知道开启同步屏障的步骤如下。
1、获取屏障的的唯一标示,标示从0开始,自加1。
2、设置屏障消息。从Message消息对象池中获取一个msg,设置msg为正在使用状态,并且重置msg的when和arg1,arg1的值设置为token值,但是这里并没有给tareget赋值。所以msg的target是否为空是判断这个msg是否是屏障消息的标志。
3、创建变量pre和p,为下一步做准备。其中p被赋值为mMessages,mMessages指向消息队列中的第一个元素。
4、通过对队列中的第一个Message的when和屏障的when进行比较,决定屏障消息在整个消息队列中的位置,因为消息队列中的消息都是按时间排序的。
5、prev != null,代表不是消息的头部,把msg插入到消息队列中。
6、prev == null,代表是消息队列的头部,把msg插入消息的头部。
这里我们重点关注:屏障消息就是一个target为空的Message。
四、屏障消息的工作原理
上面我们已经知道通过postSyncBarrier方法屏障消息被插入到消息队列,那么屏障是如何挡住普通消息只允许异步消息通过的呢?
我们来看看MessageQueue的next方法在获取消息的时候做了什么操作。这里我们只是关注重点涉及到同步屏障的相关代码(注释1、注释2)。
/** MessageQueue.class */
@UnsupportedAppUsage
Message next() {
// ...
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
// Message获取
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
// 注释1 msg.target为null,说明是屏障消息
if (msg != null && msg.target == null) {
// 注释2 循环遍历,退出循环的条件是,message到末尾了,或者msg是异步消息
do {
prevMsg = msg;
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());
}
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
// ...
}
// ...
}
}
从源码可以看出,利用屏障消息是一个target为null的Message这一特性,当获取到的Message为屏障消息的时候,进入循环,遍历过滤掉所有的同步消息,直到取出异步消息为止。
当设置了同步屏障之后,next函数将会忽略所有的同步消息,返回异步消息。换句话说就是,设置了同步屏障之后,Handler只会处理异步消息。再换句话说,同步屏障为Handler消息机制增加了一种简单的优先级机制,异步消息的优先级要高于同步消息。
五、移除同步屏障
那么为了能够继续处理MessageQueue中的同步消息,就需要移除同步屏障,同步屏障的移除是在MessageQueue的removeSyncBarrier()方法。
/** MessageQueue.class */
@UnsupportedAppUsage
@TestApi
public void removeSyncBarrier(int token) {
// Remove a sync barrier token from the queue.
// If the queue is no longer stalled by a barrier then wake it.
synchronized (this) {
Message prev = null;
Message p = mMessages;
// 步骤1 从消息队列头开始遍历获取屏障消息
while (p != null && (p.target != null || p.arg1 != token)) {
prev = p;
p = p.next;
}
if (p == null) {
throw new IllegalStateException("The specified message queue synchronization "
+ " barrier token has not been posted or has already been removed.");
}
final boolean needWake;
// 步骤2 屏障消息存在,删除屏障消息p
if (prev != null) {
prev.next = p.next;
needWake = false;
} else {
mMessages = p.next;
needWake = mMessages == null || mMessages.target != null;
}
p.recycleUnchecked();
// If the loop is quitting then it is already awake.
// We can assume mPtr != 0 when mQuitting is false.
if (needWake && !mQuitting) {
nativeWake(mPtr);
}
}
}
删除屏障消息的方法很简单,就是不断遍历消息队列找到屏障消息,找到屏障消息后,把它从消息队列中删除并回收。
六、总结
肥柴总结一下异步消息和同步屏障。
1、在一般情况下,异步消息和同步消息没有什么区别,但是一旦开启了同步屏障以后就有区别了。
2、通过MessageQueue的postSyncBarrier方法可开启同步屏障,屏障消息是一个target为null的Message。
3、在Looper获取Message的时候,利用屏障消息是一个target为null的Message这一特性,当获取到的Message为屏障消息的时候,进入循环,遍历过滤掉所有的同步消息,直到取出异步消息为止。
4、Looper获取处理的异步Message依旧按照时间先后顺序执行。
5、同步屏障的移除是在MessageQueue的removeSyncBarrier()方法。
屏障消息的工作原理图大致如下(屏蔽掉部分流程)。