Compose 完全脱离 View 系统了吗?

背景:

debug 环境中主线程中方法体执行的时间与指定的时间做对比后的堆栈信息,针对性的优化超过指定时间的耗时方法体,减少 ANR 的发生

此工具类主要是通过向主线程Looper打印超过指定时间的耗时堆栈信息以及耗时时长,其中校验时间自已定义,主动查看主线程中的耗时操作,防患未然。

原理:

此工具类为最简单最直接处理、优化耗时操作的工具

大家都知道Android 对于ANR的判断标准:

最简单的一句话就是:ANR——应用无响应,Activity是5秒,BroadCastReceiver是10秒,Service是20秒

然后此工具类的方案就是将主线程的堆栈信息作时间对比监控,超时的打印出来

Looper.loop 解析:

  1. 应用之所以未退出,就是运行在loop 中,如果有阻塞loop 的操作就会发生ANR、崩溃
public static void loop() {
    final Looper me = myLooper();
    //....
    for (;;) {
        if (!loopOnce(me, ident, thresholdOverride)) {
            return;
        }
    }
}
  1. 主要看死循环

loopOnce

private static boolean loopOnce(final Looper me,
        final long ident, final int thresholdOverride) {
    Message msg = me.mQueue.next(); // might block
    if (msg == null) {
        // No message indicates that the message queue is quitting.
        return false;
    }

    // This must be in a local variable, in case a UI event sets the logger
    // *当有任务的时候打印Dispatching to *
    final Printer logging = me.mLogging;
    if (logging != null) {
        logging.println(">>>>> Dispatching to " + msg.target + " "
                + msg.callback + ": " + msg.what);
    }
    //.... 中间部分未任务执行的代码
    
    //执行结束之后打印 Finished to 
    if (logging != null) {
        logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
    }

    // Make sure that during the course of dispatching the
    // identity of the thread wasn't corrupted.
    final long newIdent = Binder.clearCallingIdentity();
    if (ident != newIdent) {
        Log.wtf(TAG, "Thread identity changed from 0x"
                + Long.toHexString(ident) + " to 0x"
                + Long.toHexString(newIdent) + " while dispatching to "
                + msg.target.getClass().getName() + " "
                + msg.callback + " what=" + msg.what);
    }

    msg.recycleUnchecked();

    return true;
}
  1. 上述注释之间的耗时就是主线程在执行某个任务时的耗时,我们只要拿这个时间和指定时间相比就能监控主线程的耗时堆栈信息了

使用方式:

  1. Application:
 //主线程中方法体执行的时间与指定的时间做对比后的堆栈信息,针对性的优化超过指定时间的耗时方法体,
MainThreadDoctor.init(500)
  1. 查看日志:

日志等级为明显起见使用error级别

工具类:

 /**
*  @author  kong
*  @date  2022/7/6 15:55
*  @description  在debug环境中主线程中方法体执行的时间与指定的时间做对比后的堆栈信息,针对性的优化超过指定时间的耗时方法体,减少ANR的发生
**/
object MainThreadDoctor {

    private  var startTime = 0L
    private  var currentJob: Job? = null
 private  const  val START = ">>>>> Dispatching"
    private  const  val END = "<<<<< Finished"

    fun init(diagnoseStandardTime: Long) {
        if (BuildConfigs.DEBUG) {
            diagnoseFromMainThread(diagnoseStandardTime)
        }
    }

    /**
*  @param diagnoseStandardTime 执行诊断的标准时间
*/
 fun diagnoseFromMainThread(diagnoseStandardTime: Long) {
        Looper.getMainLooper().setMessageLogging {
 if (it.startsWith(START)) {
                startTime = System.currentTimeMillis()
                currentJob = GlobalScope.launch(Dispatchers.IO) {
delay(diagnoseStandardTime)
                    val stackTrace = Looper.getMainLooper().thread.stackTrace
 val builder = StringBuilder()
                    for (s in stackTrace) {
                        builder.append(s.toString())
                        builder.append("\n")
                    }
                    PPLog.e("looperMessageMain $builder")
                }
}

            if (it.startsWith(END)) {
                if (currentJob?.isCompleted == false) {
                    currentJob?.cancel()
                } else {
                    PPLog.e("looperMessageMain 总时间 = ${System.currentTimeMillis() - startTime} 毫秒")
                }
            }
        }
}
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 定位项目中,如何选取定位方案,如何平衡耗电与实时位置的精度? 重要 开始定位,Application 持有一个全局...
    贤瑜阅读 463评论 0 0
  • Android面试题 Android面试题包括Android基础,还有一些源码级别的、原理这些等。所以想去大公司面...
    陈二狗想吃肉阅读 4,833评论 0 21
  • HashMap简介 HashMap由数组+链表+ 红黑树(临界值为8)组成的,可以存储null键和null值,线程...
    wayDevelop阅读 2,430评论 1 19
  • 1.要做一个尽可能流畅的ListView,你平时在工作中如何进行优化的? ①Item布局,层级越少越好,使用hie...
    fozero阅读 744评论 0 0
  • 面经专题系列:Android面经| 问题归纳Android面经| 回顾展望Android面经| 算法题解 @[to...
    heyrenly阅读 1,230评论 0 4