looper无线循环为什么没有anr

2020-08-10  本文已影响0人  小马奔腾走天下

造成anr的不是主线程阻塞,而是,ui线程,不能及时处理looper的message消息。

在5秒 没有响应用户触摸事件;广播10秒没有处理完毕。 

Looper.loop() 的下一行代码,直接抛出异常。  正常情况,无线循环下方,不应该会被执行到。

如果执行了,就是异常。   ActivityThread类的main() 方法里面执行了 Looper.loop()

public final class ActivityThread extends ClientTransactionHandler {

public static void main(String[] args) {

Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");

    // Install selective syscall interception

    AndroidOs.install();

    // CloseGuard defaults to true and can be quite spammy.  We

// disable it here, but selectively enable it later (via

// StrictMode) on debug builds, but using DropBox, not logs.

    CloseGuard.setEnabled(false);

    Environment.initForCurrentUser();

    // Make sure TrustedCertificateStore looks in the right place for CA certificates

    final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());

    TrustedCertificateStore.setDefaultUserDirectory(configDir);

    Process.setArgV0("<pre-initialized>");

    Looper.prepareMainLooper();

    // Find the value for {@link #PROC_START_SEQ_IDENT} if provided on the command line.

// It will be in the format "seq=114"

    long startSeq =0;

    if (args !=null) {

for (int i = args.length -1; i >=0; --i) {

if (args[i] !=null && args[i].startsWith(PROC_START_SEQ_IDENT)) {

startSeq = Long.parseLong(

args[i].substring(PROC_START_SEQ_IDENT.length()));

            }

}

}

ActivityThread thread =new ActivityThread();

    thread.attach(false, startSeq);

    if (sMainThreadHandler ==null) {

sMainThreadHandler = thread.getHandler();

    }

if (false) {

Looper.myLooper().setMessageLogging(new

                LogPrinter(Log.DEBUG, "ActivityThread"));

    }

// End of event ActivityThreadMain.

    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

    Looper.loop();

    throw new RuntimeException("Main thread loop unexpectedly exited");

}

}

上一篇下一篇

猜你喜欢

热点阅读