程序员开发实例大全宝库

网站首页 > 编程文章 正文

Handler 消息队列中的同步屏障——Message

zazugpt 2024-08-12 03:08:36 编程文章 60 ℃ 0 评论

Message 分为3种:普通消息(同步消息)、屏障消息(同步屏障)和异步消息。我们通常使用的都是普通消息,而屏障消息就是在消息队列中插入一个屏障,在屏障之后的所有普通消息都会被挡着,不能被处理。不过异步消息却例外,屏障不会挡住异步消息

因此可以这样认为:屏障消息就是为了确保异步消息的优先级,设置了屏障后,只能处理其后的异步消息,同步消息会被挡住,除非撤销屏障。

那什么是同步屏障机制呢?

同步屏障机制是一套为了让某些特殊的消息得以更快被执行的机制

  • 注意这里我在同步屏障之后加上了机制二字,原因是单纯的同步屏障并不起作用,他需要和其他的Handler组件配合才能发挥作用。
  • 这里我们假设一个场景:我们向主线程发送了一个UI绘制操作Message,而此时消息队列中的消息非常多,那么这个Message的处理可能会得到延迟,绘制不及时造成界面卡顿。同步屏障机制的作用,是让这个绘制消息得以越过其他的消息,优先被执行。
  • MessageQueue中的Message,有一个变量isAsynchronous,他标志了这个Message是否是异步消息;标记为true称为异步消息,标记为false称为同步消息。同时还有另一个变量target,标志了这个Message最终由哪个Handler处理。

那么这些同步消息什么时候可以被处理呢?

那就需要先移除这个同步屏障,即调用 removeSyncBarrier()

我们的手机屏幕刷新频率有不同的类型,60Hz、120Hz 等。60Hz 表示屏幕在一秒内刷新 60 次,也就是每隔 16.6ms 刷新一次

  • 屏幕会在每次刷新的时候发出一个 VSYNC 信号,通知 CPU 进行绘制计算。
  • 具体到我们的代码中,可以认为就是执行 onMesure()、onLayout()、onDraw() 这些方法。好了,大概了解这么多就可以了。

了解过 view 绘制原理的读者应该知道,view 绘制的起点是在 viewRootImpl.requestLayout() 方法开始,这个方法会去执行上面的三大绘制任务,就是测量布局绘制;但是,重点来了:

调用 requestLayout() 方法之后,并不会马上开始进行绘制任务,而是会给主线程设置一个同步屏障,并设置 ASYNC 信号监听;当 ASYNC 信号的到来,会发送一个异步消息到主线程 Handler,执行我们上一步设置的绘制监听任务,并移除同步屏障

这里我们只需要明确一个情况: 调用 requestLayout() 方法之后会设置一个同步屏障,知道 ASYNC 信号到来才会执行绘制任务并移除同步屏障

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n99" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n208" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">api29
 @Override
 public void requestLayout() {
 if (!mHandlingLayoutInLayoutRequest) {
 checkThread();
 mLayoutRequested = true;
 scheduleTraversals();//发送同步屏障
 }
 }</pre></pre>

那,这样在等待 ASYNC 信号的时候主线程什么事都没干?

是的;这样的好处是:

保证在 ASYNC 信号到来之时,绘制任务可以被及时执行,不会造成界面卡顿。但这样也带来了相对应的代价:

  • 我们的同步消息最多可能被延迟一帧的时间,也就是 16ms,才会被执行
  • 主线程 Looper 造成过大的压力,在 VSYNC 信号到来之时,才集中处理所有消息

改善这个问题办法就是:使用异步消息

当我们发送异步消息到 MessageQueue 中时,在等待 VSYNC 期间也可以执行我们的任务,让我们设置的任务可以更快得被执行且减少主线程 Looper 的压力

异步消息机制本身就是为了避免界面卡顿,那我们直接使用异步消息,会不会有隐患?

这里我们需要思考一下,什么情况的异步消息会造成界面卡顿: 异步消息任务执行过长、异步消息海量

  • 如果异步消息执行时间太长,那即时是同步任务,也会造成界面卡顿,这点应该都很好理解
  • 其次,若异步消息海量到达影响界面绘制,那么即使是同步任务,也是会导致界面卡顿的
  • 原因是 MessageQueue 是一个链表结构,海量的消息会导致遍历速度下降,也会影响异步消息的执行效率。

所以我们应该注意的一点是:

不可在主线程执行重量级任务,无论异步还是同步

那,我们以后岂不是可以直接使用异步 Handler 来取代同步 Handler 了?是,也不是

  • 同步 Handler 有一个特点是会遵循与绘制任务的顺序,设置同步屏障之后,会等待绘制任务完成,才会执行同步任务;
  • 而异步任务与绘制任务的先后顺序无法保证,在等待 VSYNC 的期间可能被执行,也有可能在绘制完成之后执行。
  • 建议是:如果需要保证与绘制任务的顺序,使用同步 Handler;其他,使用异步 Handler

在 Android 系统里面为了更快响应UI刷新在 ViewRootImpl.scheduleTraversals 也有应用:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n130" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n228" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> android.view.ViewRootImpl#scheduleTraversals
 void scheduleTraversals() {
 if (!mTraversalScheduled) {
 mTraversalScheduled = true;
 //开启同步屏障
 mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
 //发送异步消息
 mChoreographer.postCallback(
 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
 if (!mUnbufferedInputDispatch) {
 scheduleConsumeBatchedInput();
 }
 notifyRendererOfFramePending();
 pokeDrawLockIfNeeded();
 }
 }</pre></pre>

postCallback() 最终走到了 ChoreographerpostCallbackDelayedInternal():

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n132" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n230" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> 最后调用 android.os.MessageQueue#enqueueMessage
 boolean enqueueMessage(Message msg, long when) {}</pre></pre>


同步屏障是通过 MessageQueue的postSyncBarrier 方法插入到消息队列的

  • MessageQueue 中的 Message,有一个变量 isAsynchronous,他标志了这个 Message 是否是异步消息;标记为 true 称为异步消息,标记为 false 称为同步消息
  • 同时还有另一个变量 target,标志了这个 Message 最终由哪个 Handler 处理

MessageQueue #postSyncBarrier 方法的源码如下:

重点在于: 没有给 Message 赋值 target 属性,且插入到 Message 队列头部 这个 target==null 的特殊 Message 就是同步屏障

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n142" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n235" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">android.os.MessageQueue#postSyncBarrier()
public int postSyncBarrier() {
 return postSyncBarrier(SystemClock.uptimeMillis());
}
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) {
 final int token = mNextBarrierToken++;
 //1、屏障消息和普通消息的区别是屏障消息没有tartget。
 final Message msg = Message.obtain();
 msg.markInUse();
 msg.when = when;
 msg.arg1 = token;

 Message prev = null;
 Message p = mMessages;
 //2、根据时间顺序将屏障插入到消息链表中适当的位置
 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;
 }
 //3、返回一个序号,通过这个序号可以撤销屏障
 return token;
 }
}</pre></pre>
  • postSyncBarrier 返回一个 int 类型的数值,通过这个数值可以撤销屏障
  • postSyncBarrier方法是私有的,如果我们想调用它就得使用反射
  • 插入普通消息会唤醒消息队列,但是插入屏障不会

添加异步消息有两种办法:

  • 使用异步类型的Handler发送的全部Message都是异步的
  • 给Message标志异步.
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n156" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n243" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> public Handler(boolean async) {
 this(null, async);
 }
 final boolean mAsynchronous;
 if (mAsynchronous) {
 msg.setAsynchronous(true);
 }</pre></pre>

异步类型的 Handler 构造器是标记为 hide,我们无法使用,但在 api28 之后添加了两个重要的方法:

  • 通过这两个 api 就可以创建异步 Handler 了,而异步 Handler 发出来的消息则全是异步的
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n161" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n245" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">public static Handler createAsync(@NonNull Looper looper) {
 if (looper == null) throw new NullPointerException("looper must not be null");
 return new Handler(looper, null, true);
}


public static Handler createAsync(@NonNull Looper looper, @NonNull Callback callback) {
 if (looper == null) throw new NullPointerException("looper must not be null");
 if (callback == null) throw new NullPointerException("callback must not be null");
 return new Handler(looper, callback, true);
}

public void setAsynchronous(boolean async) {
 if (async) {
 flags |= FLAG_ASYNCHRONOUS;
 } else {
 flags &= ~FLAG_ASYNCHRONOUS;
 }</pre></pre>

总结

  • 同步屏障可以通过 MessageQueue.postSyncBarrier 函数来设置。该方法发送了一个没有 target 的 Message 到 Queue 中
  • 在 next 方法中获取消息时,如果发现没有 target 的 Message,则在一定的时间内跳过同步消息,优先执行异步消息。
  • 再换句话说,同步屏障为 Handler 消息机制增加了一种简单的优先级机制,异步消息的优先级要高于同步消息
  • 在创建 Handler 时有一个 async 参数,传 true 表示此 handler 发送的时异步消息。ViewRootImpl.scheduleTraversals 方法就使用了同步屏障,保证 UI 绘制优先执行

这篇文章,其实不难;主要是将 Android 消息屏障机制 IdelHandler (同步屏障 sync barrier,异步消息 )以及在开发当中经常用到的一些知识点,总结了一下

想要往向更深入学习难免需要寻找很多的学习资料辅助,我在这里推荐网上整合的一套 《 Android Handler 消息机制学习手册》;鉴于出自大佬之手,可以帮助到大家, 能够少走些弯路

Handler 机制之 Thread

Handler 机制之 Thread Local

Handler 机制之 Callable、Future 和 Fulure Task

篇幅原因,就不在这里为大家赘述了,有需要的小伙伴:可以私信发送"面试"即可领取这份《 Android Handler 消息机制学习手册》,助你早日成为底层原理大师!

最后大家如果觉得手册内容有用的话,可以点赞分享一下哦~

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表