【Android】消息队列之异步消息

您所在的位置:网站首页 甘雨暴击率和暴击伤害 【Android】消息队列之异步消息

【Android】消息队列之异步消息

2024-07-17 14:19:45| 来源: 网络整理| 查看: 265

异步消息可以理解为是用来提升消息的重要性,在有异步消息时,会优先处理距当前时间最近的异步消息,同步消息都不会被处理,绘制界面就是一个异步消息;

同步消息:没有设置为异步的消息都是同步消息;

Message提供setAsynchronous用来设置异步消息;

一、异步消息的使用流程

1.使用MessageQueue的postSyncBarrier() 来加入同步屏障;同步屏障其实就是一个handler为空的消息,它也存放在消息队列中;

2.往消息队列中加入异步消息,跟平时创建消息没有什么不同,只是额外调用setAsynchronous(true)来设置异步消息;

3.线程执行过程中,是怎么取消息的,异步消息为什么会优先于同步消息;【介绍下原理,可以不关注】

Looper的loop会一直循环取消息进行处理,我们主要关注的是MessageQueue的next方法,它在消息队列头部是屏障消息的时候,会在它后面找最近的异步消息,如果时间合适的话就返回这个消息,如果时间不ok就休眠等待;

这里有个隐含意思:同步屏障消息在队列头部才有效,才需要处理异步消息,如果同步屏障消息不在头部的话,不影响它前面的同步消息的处理;

可以看下next关键代码:

Message msg = mMessages; //target为空表示是同步屏障消息 if (msg != null && msg.target == null) { // Stalled by a barrier. Find the next asynchronous message in the queue. do { prevMsg = msg; msg = msg.next; } while (msg != null && !msg.isAsynchronous()); }

4.异步消息执行时,使用MessageQueue的removeSyncBarrier来删除同步屏障;

二、绘制消息的处理过程

1.加入同步屏障;

我们都知道绘制消息都是在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(); } }

2.设置异步消息;

硬件底层向应用层发出垂直同步信号后,会回调Choreographer内部类FrameDisplayEventReceiver的onVsync方法,其内部主要是向消息队列中放入异步消息;异步消息执行时会调用FrameDisplayEventReceiver的run方法来处理;

private final class FrameDisplayEventReceiver extends DisplayEventReceiver implements Runnable { private boolean mHavePendingVsync; private long mTimestampNanos; private int mFrame; public FrameDisplayEventReceiver(Looper looper, int vsyncSource) { super(looper, vsyncSource); } // TODO(b/116025192): physicalDisplayId is ignored because SF only emits VSYNC events for // the internal display and DisplayEventReceiver#scheduleVsync only allows requesting VSYNC // for the internal display implicitly. @Override public void onVsync(long timestampNanos, long physicalDisplayId, int frame) { // Post the vsync event to the Handler. // The idea is to prevent incoming vsync events from completely starving // the message queue. If there are no messages in the queue with timestamps // earlier than the frame time, then the vsync event will be processed immediately. // Otherwise, messages that predate the vsync event will be handled first. long now = System.nanoTime(); if (timestampNanos > now) { Log.w(TAG, "Frame time is " + ((timestampNanos - now) * 0.000001f) + " ms in the future! Check that graphics HAL is generating vsync " + "timestamps using the correct timebase."); timestampNanos = now; } if (mHavePendingVsync) { Log.w(TAG, "Already have a pending vsync event. There should only be " + "one at a time."); } else { mHavePendingVsync = true; } mTimestampNanos = timestampNanos; mFrame = frame; Message msg = Message.obtain(mHandler, this); msg.setAsynchronous(true); mHandler.sendMessageAtTime(msg, timestampNanos / TimeUtils.NANOS_PER_MS); } @Override public void run() { mHavePendingVsync = false; doFrame(mTimestampNanos, mFrame); } }

3.删除同步屏障;

第2步中的doFrame会执行Choreographer里设置的所有回调消息,包括第1步中设置的Choreographer.CALLBACK_TRAVERSAL消息;

这会执行到ViewRootImpl的TraversalRunnable,TraversalRunnable主要就是调用doTraversal方法;

void doTraversal() { if (mTraversalScheduled) { mTraversalScheduled = false; //删除同步屏障 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier); if (mProfile) { Debug.startMethodTracing("ViewAncestor"); } performTraversals(); if (mProfile) { Debug.stopMethodTracing(); mProfile = false; } } }

 



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭