Moosphan / Android-Daily-Interview

:pushpin:每工作日更新一道 Android 面试题,小聚成河,大聚成江,共勉之~

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

2019-10-23:请谈谈什么是同步屏障?

Moosphan opened this issue · comments

2019-10-23:请谈谈什么是同步屏障?

停止同步消息的处理,只处理异步消息,一般异步消息为系统的某些事件

高大上的名词

高大上的名词

同解。

commented

handler.getLooper().getQueue().postSyncBarrier()加入同步屏障后,Message.obtain()获取一个target为null的msg,并根据当前时间将该msg插入到链表中。
在Looper.loop()循环取消息中 Message msg = queue.next(); target为空时,取链表中的异步消息。
通过setAsynchronous(true)来指定为异步消息

应用场景:ViewRootImpl scheduleTraversals中加入同步屏障 并在view的绘制流程中post异步消息,保证view的绘制消息优先执行

  • 我们平时使用Handler发送的消息都是普通消息, 同步屏障不会唤醒消息队列
  • 同步屏障没有target,因为它不需要分发
  • 它的主要作用在于挡住同步消息,只允许程序处理异步消息,保证异步消息能够优先处理

参考文章

Handler机制——同步屏障

Handler 的消息发送有三种
普通消息
异步消息
屏障消息
发送了屏障消息后 不接收同步消息,只允许接收异步消息