监控主线程耗时操作,从开发中解决ANR

背景:

debug 环境中主线程中方法体履行的时刻与指定的时刻做比照后的仓库信息,针对性的优化超越指定时刻的耗时方法体,削减 ANR 的产生

此东西类首要是经过向主线程Looper打印超越指定线程池的七个参数时刻的耗时仓库信息以及监控摄像头品牌排行耗时时长,其中校验时刻自已定义,主动检查主线程中线程数是什么的耗时操作,未雨绸缪。

原理:

线程是什么意思东西类为最简单最直接处理、优化耗时操作的东西 大家都知道Android 关于ANR的判别标准:

最简单的一句话便是:ANR——运用无响应,Activity是5秒,BroadCastReceiver是10秒,Servi线程池面试题ce是20秒

然后此东西类的计划便是将主线程的仓库信息作时刻比照监控,超时的打印出来

Looper.loo监控摄像头品牌排行p 解析:

  1. 运用之所以未退出,便是运行在loandroidstudio安装教程op 中,如果有阻塞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. 上述注释之间的耗时线程池面试题便是主线程在approve履行某个使命时的耗时,咱们只要拿这个时刻和指定时刻比较就能监控主appstore线程的耗时仓库信息了

运用方式:

  1. Application:
 //主线程中方法体履行的时刻与指定的时刻做比照后的仓库信息,针对性的优化超越指定时刻的耗时方法体,
MainThreadDoctor.init(500)
  1. 检查日志:

监控主线程耗时操作,从开发中处理ANR

日志等级为明显起见运用erAPPror级别

东西类:

 /**
*  @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} 毫秒")
                }
            }
        }
}
}

发表回复

提供最优质的资源集合

立即查看 了解详情