为什么在子线程中使用Thread.sleep获取android ANR

时间:2013-10-21 09:53:26

标签: android android-anr-dialog

当我进行猴子测试时,发现了ANR。检查/ data / anr中的anr日志 NotifiThread中的代码(在MyService中定义)可能导致ANR:

Thread.sleep(Util.getIntervalTime(getBaseContext())); //(the first one in detail code).

问题是代码在子线程中运行(NotifiThread未被调用 Handler.post(Runnable)但由新的Thread()。start())调用。为什么我会得到ANR。 有人可以就这种情况给出一些建议,谢谢。 下面是详细代码和ANR日志。

详细代码:

public class MyService extends Service {

部分省略.....

class NotifiThread extends Thread {
public void run() {
while (isAvailable) {
try {
String json = null;
json = new HttpActions(getBaseContext()).getPushNotification(getApplicationContext());
if (null != json && json.length() > 0) {
JSONObject jsonObject = new JSONObject(json);
int code = jsonObject.getInt("code");
String msg = jsonObject.getString("msg");
if (0 == code) {
if (!jsonObject.isNull("info")) {
JSONArray ja = jsonObject.getJSONArray("info");
for (int i = 0; i < ja.length(); i++) {
String item = ja.getJSONObject(i).toString();
ULog.d(TAG, "Receive a pushMessage: " + item);
if (!TextUtils.isEmpty(item)) {
final Message mymsg = new Message();
mymsg.what = NotifyHandlerProvider.HANDLER_SET_STATUES_BAR;
mymsg.obj = item;
handlerProvider.handleMessage(mymsg);
}
}


}
}
else {
ULog.i(TAG, json.toString());
}
}
ULog.i(TAG, "intervale Time -->"+Util.getIntervalTime(getBaseContext()));
Thread.sleep(Util.getIntervalTime(getBaseContext())); /////////here may be cause anr
//   Thread.sleep(30000); //test time 30Sec
} catch (InterruptedException e) {
ULog.e(TAG, e.getMessage());
e.printStackTrace();
} catch (TVException e) {
try {
ULog.i(TAG, "intervale Time -->"+Util.getIntervalTime(getBaseContext()));
Thread.sleep(Util.getIntervalTime(getBaseContext()));
//   Thread.sleep(30000); //test time 30S
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ULog.e(TAG, e.getMessage());
e.printStackTrace();
} catch (JSONException e) {
ULog.e(TAG, e.getMessage());
e.printStackTrace();
}
}
};
}

ANR

----- pid 28042 at 2013-10-21 13:06:03 ----- 
Cmd line: com.test.video 


DALVIK THREADS: 
(mutexes: tll=0 tsl=0 tscl=0 ghl=0) 


"Binder_5" prio=5 tid=19 NATIVE 
  | group="main" sCount=1 dsCount=0 obj=0x42a08ae8 self=0x7479f600 
  | sysTid=28605 nice=0 sched=0/0 cgrp=apps handle=1954414600 
  | state=S schedstat=( 58163235 63427446 882 ) utm=1 stm=4 core=0 
  #00  pc 00017004  /system/lib/libc.so (__ioctl+8) 
  #01  pc 0002a9ad  /system/lib/libc.so (ioctl+16) 
  #02  pc 00016ba1  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132) 
  #03  pc 00017363  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154) 
  #04  pc 0001b15d  /system/lib/libbinder.so 
  #05  pc 00011267  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+114) 
  #06  pc 0004e21b  /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+66) 
  #07  pc 00010dcd  /system/lib/libutils.so 
  #08  pc 0000e3f8  /system/lib/libc.so (__thread_entry+72) 
  #09  pc 0000dae4  /system/lib/libc.so (pthread_create+160) 
  at dalvik.system.NativeStart.run(Native Method) 


"Thread-573" prio=5 tid=1 VMWAIT 
  | group="main" sCount=1 dsCount=0 obj=0x421eaaf8 self=0x40b880c0 
  | sysTid=28042 nice=0 sched=0/0 cgrp=apps handle=1074359516 
  | state=S schedstat=( 23552699271 9147967005 63188 ) utm=1977 stm=378 core=1 
  #00  pc 00018124  /system/lib/libc.so (__futex_syscall3+8) 
  #01  pc 0000e43c  /system/lib/libc.so (__pthread_cond_timedwait_relative+48) 
  #02  pc 0000e498  /system/lib/libc.so (__pthread_cond_timedwait+60) 
  #03  pc 0004d239  /system/lib/libdvm.so 
  #04  pc 0003cf05  /system/lib/libdvm.so 
  #05  pc 0004efed  /system/lib/libandroid_runtime.so (android::AndroidRuntime::start(char const*, char const*)+452) 
  #06  pc 0000126b  /system/bin/app_process 
  #07  pc 0001273f  /system/lib/libc.so (__libc_init+38) 
  #08  pc 00000dac  /system/bin/app_process 
  at dalvik.system.NativeStart.run(Native Method) 


"Binder_4" prio=5 tid=16 NATIVE 
  | group="main" sCount=1 dsCount=0 obj=0x41d11028 self=0x743347c0 
  | sysTid=28103 nice=0 sched=0/0 cgrp=apps handle=1896592128 
  | state=S schedstat=( 132144092 125415546 1964 ) utm=6 stm=7 core=1 
  #00  pc 00017004  /system/lib/libc.so (__ioctl+8) 
  #01  pc 0002a9ad  /system/lib/libc.so (ioctl+16) 
  #02  pc 00016ba1  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132) 
  #03  pc 00017363  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154) 
  #04  pc 0001b15d  /system/lib/libbinder.so 
  #05  pc 00011267  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+114) 
  #06  pc 0004e21b  /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+66) 
  #07  pc 00010dcd  /system/lib/libutils.so 
  #08  pc 0000e3f8  /system/lib/libc.so (__thread_entry+72) 
  #09  pc 0000dae4  /system/lib/libc.so (pthread_create+160) 
  at dalvik.system.NativeStart.run(Native Method) 


"Binder_3" prio=5 tid=18 NATIVE 
  | group="main" sCount=1 dsCount=0 obj=0x41bfd4b8 self=0x74388018 
  | sysTid=28089 nice=0 sched=0/0 cgrp=apps handle=1950543768 
  | state=S schedstat=( 132114412 125727705 1949 ) utm=3 stm=10 core=0 
  #00  pc 00017004  /system/lib/libc.so (__ioctl+8) 
  #01  pc 0002a9ad  /system/lib/libc.so (ioctl+16) 
  #02  pc 00016ba1  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132) 
  #03  pc 00017363  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154) 
  #04  pc 0001b15d  /system/lib/libbinder.so 
  #05  pc 00011267  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+114) 
  #06  pc 0004e21b  /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+66) 
  #07  pc 00010dcd  /system/lib/libutils.so 
  #08  pc 0000e3f8  /system/lib/libc.so (__thread_entry+72) 
  #09  pc 0000dae4  /system/lib/libc.so (pthread_create+160) 
  at dalvik.system.NativeStart.run(Native Method) 


"AsyncTask #5" prio=5 tid=17 WAIT 
  | group="main" sCount=1 dsCount=0 obj=0x41b7c370 self=0x710c6890 
  | sysTid=28080 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1893610120 
  | state=S schedstat=( 857607967 1441615524 4827 ) utm=60 stm=25 core=3 
  at java.lang.Object.wait(Native Method) 
  - waiting on <0x41b7c4c8> (a java.lang.VMThread) held by tid=17 (AsyncTask #5) 
  at java.lang.Thread.parkFor(Thread.java:1231) 
  at sun.misc.Unsafe.park(Unsafe.java:323) 
  at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159) 
  at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
  at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413) 
  at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
  at java.lang.Thread.run(Thread.java:856) 


"AsyncTask #4" prio=5 tid=15 WAIT 
  | group="main" sCount=1 dsCount=0 obj=0x419c12c8 self=0x743328e0 
  | sysTid=28077 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1949520432 
  | state=S schedstat=( 596551542 1174248245 5467 ) utm=39 stm=20 core=3 
  at java.lang.Object.wait(Native Method) 
  - waiting on <0x419c13e8> (a java.lang.VMThread) held by tid=15 (AsyncTask #4) 
  at java.lang.Thread.parkFor(Thread.java:1231) 
  at sun.misc.Unsafe.park(Unsafe.java:323) 
  at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159) 
  at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
  at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413) 
  at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
  at java.lang.Thread.run(Thread.java:856) 


"Thread-550" prio=5 tid=14 TIMED_WAIT 
  | group="main" sCount=1 dsCount=0 obj=0x416a6308 self=0x710d5d68 
  | sysTid=28066 nice=0 sched=0/0 cgrp=apps handle=1893625880 
  | state=S schedstat=( 10528334 14255987 70 ) utm=0 stm=1 core=2 
  at java.lang.VMThread.sleep(Native Method) 
  at java.lang.Thread.sleep(Thread.java:1031) 
  at java.lang.Thread.sleep(Thread.java:1013) 
  at com.test.video.service.MyService$NotifiThread.run(MyService.java:508) 


"AsyncTask #3" prio=5 tid=13 WAIT 
  | group="main" sCount=1 dsCount=0 obj=0x415a4f18 self=0x70dec7b8 
  | sysTid=28064 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1893405336 
  | state=S schedstat=( 1123509415 1620520145 6104 ) utm=91 stm=21 core=2 
  at java.lang.Object.wait(Native Method) 
  - waiting on <0x415a5000> (a java.lang.VMThread) held by tid=13 (AsyncTask #3) 
  at java.lang.Thread.parkFor(Thread.java:1231) 
  at sun.misc.Unsafe.park(Unsafe.java:323) 
  at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159) 
  at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
  at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413) 
  at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
  at java.lang.Thread.run(Thread.java:856) 


"AsyncTask #2" prio=5 tid=12 WAIT 
  | group="main" sCount=1 dsCount=0 obj=0x41626ed0 self=0x710c9938 
  | sysTid=28063 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1893395672 
  | state=S schedstat=( 945973203 1552117896 5193 ) utm=75 stm=19 core=1 
  at java.lang.Object.wait(Native Method) 
  - waiting on <0x41626ff0> (a java.lang.VMThread) held by tid=12 (AsyncTask #2) 
  at java.lang.Thread.parkFor(Thread.java:1231) 
  at sun.misc.Unsafe.park(Unsafe.java:323) 
  at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159) 
  at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
  at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413) 
  at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
  at java.lang.Thread.run(Thread.java:856) 


"AsyncTask #1" prio=5 tid=11 WAIT 
  | group="main" sCount=1 dsCount=0 obj=0x416af260 self=0x710b3c30 
  | sysTid=28062 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1893382640 
  | state=S schedstat=( 774461097 1362864121 5559 ) utm=54 stm=23 core=3 
  at java.lang.Object.wait(Native Method) 
  - waiting on <0x415ad128> (a java.lang.VMThread) held by tid=11 (AsyncTask #1) 
  at java.lang.Thread.parkFor(Thread.java:1231) 
  at sun.misc.Unsafe.park(Unsafe.java:323) 
  at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159) 
  at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
  at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413) 
  at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
  at java.lang.Thread.run(Thread.java:856) 


"Binder_2" prio=5 tid=10 NATIVE 
  | group="main" sCount=1 dsCount=0 obj=0x415a4b18 self=0x70e4d2b0 
  | sysTid=28054 nice=0 sched=0/0 cgrp=apps handle=1896643992 
  | state=S schedstat=( 142965634 150228508 2048 ) utm=5 stm=9 core=0 
  #00  pc 00017004  /system/lib/libc.so (__ioctl+8) 
  #01  pc 0002a9ad  /system/lib/libc.so (ioctl+16) 
  #02  pc 00016ba1  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132) 
  #03  pc 00017363  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154) 
  #04  pc 0001b15d  /system/lib/libbinder.so 
  #05  pc 00011267  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+114) 
  #06  pc 0004e21b  /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+66) 
  #07  pc 00010dcd  /system/lib/libutils.so 
  #08  pc 0000e3f8  /system/lib/libc.so (__thread_entry+72) 
  #09  pc 0000dae4  /system/lib/libc.so (pthread_create+160) 
  at dalvik.system.NativeStart.run(Native Method) 


"Binder_1" prio=5 tid=9 NATIVE 
  | group="main" sCount=1 dsCount=0 obj=0x415a49f8 self=0x710b7df0 
  | sysTid=28053 nice=0 sched=0/0 cgrp=apps handle=1896663640 
  | state=S schedstat=( 140215235 144292144 2123 ) utm=7 stm=7 core=0 
  #00  pc 00017004  /system/lib/libc.so (__ioctl+8) 
  #01  pc 0002a9ad  /system/lib/libc.so (ioctl+16) 
  #02  pc 00016ba1  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132) 
  #03  pc 00017363  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154) 
  #04  pc 0001b15d  /system/lib/libbinder.so 
  #05  pc 00011267  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+114) 
  #06  pc 0004e21b  /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+66) 
  #07  pc 00010dcd  /system/lib/libutils.so 
  #08  pc 0000e3f8  /system/lib/libc.so (__thread_entry+72) 
  #09  pc 0000dae4  /system/lib/libc.so (pthread_create+160) 
  at dalvik.system.NativeStart.run(Native Method) 


"FinalizerWatchdogDaemon" daemon prio=5 tid=8 WAIT 
  | group="system" sCount=1 dsCount=0 obj=0x415a1988 self=0x70deccf0 
  | sysTid=28052 nice=0 sched=0/0 cgrp=apps handle=1893606648 
  | state=S schedstat=( 620418 13513387 17 ) utm=0 stm=0 core=1 
  at java.lang.Object.wait(Native Method) 
  - waiting on <0x412ee740> (a java.lang.Daemons$FinalizerWatchdogDaemon) 
  at java.lang.Object.wait(Object.java:364) 
  at java.lang.Daemons$FinalizerWatchdogDaemon.waitForObject(Daemons.java:230) 
  at java.lang.Daemons$FinalizerWatchdogDaemon.run(Daemons.java:207) 
  at java.lang.Thread.run(Thread.java:856) 


"FinalizerDaemon" daemon prio=5 tid=7 WAIT 
  | group="system" sCount=1 dsCount=0 obj=0x415a17d8 self=0x70deac60 
  | sysTid=28051 nice=0 sched=0/0 cgrp=apps handle=1896645744 
  | state=S schedstat=( 237348859 113429164 639 ) utm=18 stm=5 core=2 
  at java.lang.Object.wait(Native Method) 
  - waiting on <0x412da6d0> (a java.lang.ref.ReferenceQueue) 
  at java.lang.Object.wait(Object.java:401) 
  at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:102) 
  at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:73) 
  at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170) 
  at java.lang.Thread.run(Thread.java:856) 


"ReferenceQueueDaemon" daemon prio=5 tid=6 WAIT 
  | group="system" sCount=1 dsCount=0 obj=0x415a1670 self=0x710d4878 
  | sysTid=28050 nice=0 sched=0/0 cgrp=apps handle=1896697032 
  | state=S schedstat=( 43269535 17372391 384 ) utm=3 stm=1 core=1 
  at java.lang.Object.wait(Native Method) 
  - waiting on <0x412da5f8>  
  at java.lang.Object.wait(Object.java:364) 
  at java.lang.Daemons$ReferenceQueueDaemon.run(Daemons.java:130) 
  at java.lang.Thread.run(Thread.java:856) 


"Compiler" daemon prio=5 tid=5 VMWAIT 
  | group="system" sCount=1 dsCount=0 obj=0x415a1580 self=0x710ca9f0 
  | sysTid=28049 nice=0 sched=0/0 cgrp=apps handle=1893549920 
  | state=S schedstat=( 1281331973 447299814 15680 ) utm=66 stm=62 core=0 
  #00  pc 00018124  /system/lib/libc.so (__futex_syscall3+8) 
  #01  pc 0000e43c  /system/lib/libc.so (__pthread_cond_timedwait_relative+48) 
  #02  pc 0000e498  /system/lib/libc.so (__pthread_cond_timedwait+60) 
  #03  pc 00075aa1  /system/lib/libdvm.so 
  #04  pc 00056d37  /system/lib/libdvm.so 
  #05  pc 0000e3f8  /system/lib/libc.so (__thread_entry+72) 
  #06  pc 0000dae4  /system/lib/libc.so (pthread_create+160) 
  at dalvik.system.NativeStart.run(Native Method) 


"JDWP" daemon prio=5 tid=4 VMWAIT 
  | group="system" sCount=1 dsCount=0 obj=0x415a1388 self=0x70deb138 
  | sysTid=28048 nice=0 sched=0/0 cgrp=apps handle=1090180312 
  | state=S schedstat=( 1557552 1913854 12 ) utm=0 stm=0 core=1 
  #00  pc 0001712c  /system/lib/libc.so (select+20) 
  #01  pc 0006396b  /system/lib/libdvm.so 
  #02  pc 000664fd  /system/lib/libdvm.so 
  #03  pc 00056d37  /system/lib/libdvm.so 
  #04  pc 0000e3f8  /system/lib/libc.so (__thread_entry+72) 
  #05  pc 0000dae4  /system/lib/libc.so (pthread_create+160) 
  at dalvik.system.NativeStart.run(Native Method) 


"Signal Catcher" daemon prio=5 tid=3 RUNNABLE 
  | group="system" sCount=0 dsCount=0 obj=0x415a1290 self=0x710be808 
  | sysTid=28047 nice=0 sched=0/0 cgrp=apps handle=1090180536 
  | state=R schedstat=( 24663384 63246041 286 ) utm=0 stm=2 core=1 
  at dalvik.system.NativeStart.run(Native Method) 


"GC" daemon prio=5 tid=2 VMWAIT 
  | group="system" sCount=1 dsCount=0 obj=0x415a11b0 self=0x710b4130 
  | sysTid=28046 nice=0 sched=0/0 cgrp=apps handle=1090181064 
  | state=S schedstat=( 750353301 214129197 783 ) utm=71 stm=4 core=1 
  #00  pc 00018124  /system/lib/libc.so (__futex_syscall3+8) 
  #01  pc 0000e43c  /system/lib/libc.so (__pthread_cond_timedwait_relative+48) 
  #02  pc 0000e498  /system/lib/libc.so (__pthread_cond_timedwait+60) 
  #03  pc 0007477f  /system/lib/libdvm.so 
  #04  pc 00056d37  /system/lib/libdvm.so 
  #05  pc 0000e3f8  /system/lib/libc.so (__thread_entry+72) 
  #06  pc 0000dae4  /system/lib/libc.so (pthread_create+160) 
  at dalvik.system.NativeStart.run(Native Method) 

-----结束28042 -----

0 个答案:

没有答案