背景:
在 debug 环境中主线程中方法体履行的时刻与指定的时刻做比照后的仓库信息,针对性的优化超越指定时刻的耗时方法体,削减 ANR 的产生
此东西类首要是经过向主线程Looper打印超越指定线程池的七个参数时刻的耗时仓库信息以及监控摄像头品牌排行耗时时长,其中校验时刻自已定义,主动检查主线程中线程数是什么的耗时操作,未雨绸缪。
原理:
此线程是什么意思东西类为最简单最直接处理、优化耗时操作的东西 大家都知道Android 关于ANR的判别标准:
最简单的一句话便是:ANR——运用无响应,Activity是5秒,BroadCastReceiver是10秒,Servi线程池面试题ce是20秒
然后此东西类的计划便是将主线程的仓库信息作时刻比照监控,超时的打印出来
Looper.loo监控摄像头品牌排行p 解析:
- 运用之所以未退出,便是运行在loandroidstudio安装教程op 中,如果有阻塞loop监控拍下东航客机坠落瞬间 的操作就会产生ANR、溃散
public static void loop() { final Looper me = myLooper(); //.... for (;;) { if (!loopOnce(me, ident, thresholdOverride)) { return; } } }
- 首要看死循环 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; }
- 上述注释之间的耗时线程池面试题便是主线程在approve履行某个使命时的耗时,咱们只要拿这个时刻和指定时刻比较就能监控主appstore线程的耗时仓库信息了
运用方式:
- Application:
//主线程中方法体履行的时刻与指定的时刻做比照后的仓库信息,针对性的优化超越指定时刻的耗时方法体, MainThreadDoctor.init(500)
- 检查日志:

日志等级为明显起见运用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} 毫秒") } } } } }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)