Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
if (!mUnbufferedInputDispatch) {
scheduleConsumeBatchedInput();
}
notifyRendererOfFramePending();
pokeDrawLockIfNeeded();
}
}
…
//在doTraversal方法中移除同步消息屏障
void doTraversal() {
if (mTraversalScheduled) {
mTraversalScheduled = false;
//移除同步屏障
mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
…
}
}
在这个方法中,涉及到三个比较重要的信息
-
mTraversalRunnable
-
Choreographer编舞者
-
同步屏障消息
-
首先看mTraversalRunnable,它的作用就是从ViewRootImpl 从上往下执行performMeasure、performLayout、performDraw。
-
Choreographer主要是为了配合Vsync信号,给上层app的渲染提供一个稳定的Message处理时机,也就是Vsync信号到来时,系统通过对Vsync信号的调整,来控制每一帧绘制操作的时机。当Vsync信号到来时,会往主线程的MessageQueue中插入一条异步消息,由于在scheduleTraversals中给MessageQueue中插入了同步屏障消息,那么当执行到同步屏障时,会取出异步消息执行。
看下Choreography中插入消息的方法是如何实现的:
private void postCallbackDelayedInternal(int callbackType,
Object action, Object token, long delayMillis) {
synchronized (mLock) {
…
if (dueTime <= now) {
scheduleFrameLocked(now);
} else {
Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);
msg.arg1 = callbackType;
//设置为异步消息
msg.setAsynchronous(true);
mHan
dler.sendMessageAtTime(msg, dueTime);
}
}
}
通过以上的分析,我们知道了,在刷新ui的时候原来会有这么多的参与者,但是那些什么同步消息、异步消息、消息屏障又是些什么东西呢?接下来我们就来研究一下。
何为同步屏障?
message分类
Handler的message分为三种
-
同步消息
-
异步消息
-
屏障消息
通常我们使用handler发送消息,都是使用默认的构造函数构造handler,然后使用send方法发送。这样发送的消息都是普通消息也就是同步消息,发出去的消息就会在MessageQueue中排队。异步消息正常情况下跟同步消息没有区别,只有在设置了同步屏障之后,才会出现差异。
同步屏障就是在消息队列中插入一个屏障,插入之后,所有的同步消息都会被屏蔽,不能被执行,但是异步消息却不受影响,可以继续执行。
插入消息屏障
正常插入消息会调用enqueueMessage方法,同时将handler赋值给message的target。
//将消息插入消息队列
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);
}
在MessageQueue中进行判断,如果target为空也就是这个message没有对应的handler则会报异常。
boolean enqueueMessage(Message msg, long when) {
if (msg.target == null) {
throw new IllegalArgumentException(“Message must have a target.”);
}
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
…
// 如果需要唤醒,则唤醒
if (needWake) {
nativeWake(mPtr);
}
通过MessageQueue的postSyncBarrier方法插入屏障,message的target属性为null
private int postSyncBarrier(long when) {
synchronized (this) {
final int token = mNextBarrierToken++;
//msg没有为target属性赋值
final Message msg = Message.obtain();
…
//根据时间插入到MessageQueue中
if (when != 0) {
while (p != null && p.when <= when) {
prev = p;
p = p.next;
}
}
if (prev != null) { // invariant: p == prev.next
msg.next = p;
prev.next = msg;
} else {
msg.next = p;
mMessages = msg;
}
//返回一个序号,通过它可以对屏障消息进行撤销
return token;
}
}
经过以上的操作,我们可以总结出
-
屏障消息和普通消息的区别是屏障消息没有target属性,普通消息有target属性是因为要将消息分发给target指向的handler处理
-
屏障消息会插入到MessageQueue中合适的位置,这个消息以后的普通消息将被屏蔽
-
postSyncBarrier返回一个int类型的数值,通过这个数值可以撤销屏障
-
postSyncBarrier方法是私有的,如果我们想调用它就得使用反射
-
插入普通消息会唤醒消息队列,但是插入屏障不会
如何发送异步消息
通常我们发送的都是普通消息,如果想发送异步消息
- 可以在创建handler时使用如下的构造器中的一种,同时将async参数设置为true,这样这个handler发送的消息就都是异步消息了。
public Handler(boolean async) {
this(null, async);
}
public Handler(@NonNull Looper looper, @Nullable Callback callback) {
this(looper, callback, false);
}
public Handler(@Nullable Callback callback, boolean async) {
…
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;
mAsynchronous = async;
}
- 除了这种方式还可以直接设置消息的类型为异步消息
an async) {
…
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;
mAsynchronous = async;
}
- 除了这种方式还可以直接设置消息的类型为异步消息