Thread.sleep()在Android中导致ANR

时间:2017-06-01 16:37:24

标签: android multithreading android-anr-dialog android-thread

此代码在后台线程上运行:

@Override
public void run() {
    while (true) {
        a = a+1;

        try {
            Thread.sleep(100);
        }
        catch (Throwable t) {
            Log.e("","", t);
        }           
    }
}

在开发者控制台中,我看到了ANR报告:

  

ANR输入调度超时(等待发送非键事件,因为   触摸的窗口尚未处理完某些输入事件   超过500.0ms前送到它。)

"myThread" prio=5 tid=7 SUSPENDED
  | group="main" sCount=1 dsCount=0 obj=0x11111111 self=0x22222222
  | sysTid=11111 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=333333333
  | state=S schedstat=( 0 0 0 ) utm=2 stm=3 core=1
  at java.lang.VMThread.sleep (Native Method)
  at java.lang.Thread.sleep (Thread.java:1013)
  at java.lang.Thread.sleep (Thread.java:995)
  at com.example.MyThread.run (MyThread.java:123)

如何修复此ANR?

1 个答案:

答案 0 :(得分:0)

问题是您在while(true)循环中创建增量命令。