バックグラウンド:
デバッグ環境で は、メインスレッドのメソッド本体の実行時間を指定時間と比較し、スタック情報を比較し、指定時間を超える時間のかかるメソッド本体を対象に最適化して削減します。 ANR の発生。
このツールクラスは、主に時間のかかるスタック情報と指定時間を超える時間のかかる時間をメインスレッドのルーパーに出力します。メインスレッドのルーパーでは、検証時間が独自に定義され、メインスレッドでの時間のかかる操作を積極的にチェックします。問題が発生する前に防止します。
原理:
このツールクラスは、時間のかかる操作を最適化するための最もシンプルで直接的な処理ツールです。AndroidのANRの判断基準は誰もが知っています。
最も単純な文は次のとおりです。ANR-アプリケーションは応答しません。アクティビティは5秒、BroadCastReceiverは10秒、サービスは20秒です。
次に、このツールクラスの解決策は、メインスレッドのスタック情報を比較および監視し、タイムアウトを出力することです。
Looper.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;
}
- 上記のコメント間の時間は、特定のタスクを実行する際のメインスレッドの時間です。この時間を指定した時間と比較することで、メインスレッドの時間のかかるスタック情報を監視できます。
使い方:
- 応用:
//主线程中方法体执行的时间与指定的时间做对比后的堆栈信息,针对性的优化超过指定时间的耗时方法体,
MainThreadDoctor.init(500)
- ビュー・ログ:
ログレベルは明確にするためにエラーレベルを使用します
ツール:
/**
* @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} 毫秒")
}
}
}
}
}