ANR有3个线程 - 使用锁

时间:2015-01-16 18:05:16

标签: java android multithreading

主线程(“main”prio = 5 tid = 1)
在onCreate()中创建一个ArrayList
制作一个锁定对象。 (
启动一个线程:(“Thread-6024”prio = 5 tid = 29)

myThread= new MyThread();
myThread.start();

MyThread的:

public class PainterThread extends Thread{
@Override
public void run() {
    while(true) {
        synchronized(lock){doMyStuff()}
        Thread.yield();
    }
}

还用Timer启动另一个线程(“Timer-1”prio = 5 tid = 26)

myTicker = new MyTicker();
timer = new Timer();
timer.scheduleAtFixedRate(myTicker, 0, PERIOD);

MyTicker:

class MyTicker extends TimerTask{
    @Override
    public void run() {
        synchronized(lock){
            doSomeOtherStuff()
        }
    }
}

整个系统运行正常。
BUT:
有时会得到一个ANR

  • 两个线程运行良好(一个是模拟我的GameWorld, 另一种是在SurfaceView上绘画)
  • 我让他们跑了很长时间(结果很好 - 运动顺畅)
  • 然后我触摸屏幕:
  • 2个线程运行良好 - 表面视图令人耳目一新 很好地
  • 其他UI组件未刷新(我在其中使它们无效) runOnUiThread)

几秒钟后(据我所知它应该是5,但它会长一点):
logcat的:

01-16 18:02:19.320    9279-9279/myPackage W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x420face0)
01-16 18:02:19.380    9279-9279/myPackage V/GAV4﹕ Thread[main,5,main]: Tracking Exception: IllegalStateException (@View:measure:16533) {main}
01-16 18:02:19.380    9279-9279/myPackage V/GAV4﹕ Thread[main,5,main]: Dispatch call queued. Dispatch will run once initialization is complete.
01-16 18:15:20.240    9279-9288/myPackage I/dalvikvm﹕ threadid=3: reacting to signal 3
01-16 18:15:20.350    9279-9288/myPackage I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
01-16 18:15:24.430    9279-9279/myPackage A/libc﹕ Fatal signal 6 (SIGABRT) at 0x0000025a (code=0), thread 9279 (myPackage)

trace_myPackage.txt的有趣部分:

主线:

"main" prio=5 tid=1 WAIT
  | group="main" sCount=1 dsCount=0 obj=0x420fbde0 self=0x420ea490
  | sysTid=6215 nice=0 sched=0/0 cgrp=apps handle=1075671380
  | state=S schedstat=( 16020076685 14431356669 20182 ) utm=1384 stm=218 core=0
  at java.lang.Object.wait(Native Method)
  - waiting on <0x420fbeb0> (a java.lang.VMThread) held by tid=1 (main)
  at java.lang.Thread.parkFor(Thread.java:1205)
  at sun.misc.Unsafe.park(Unsafe.java:325)
  at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:813)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:846)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1175)
  at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:180)
  at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:256)
  at com.google.android.gms.analytics.x.dY((null):-1)
  at com.google.android.gms.analytics.GoogleAnalytics.dY((null):-1)
  at com.google.android.gms.analytics.ExceptionReporter.uncaughtException((null):-1)
  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
  at dalvik.system.NativeStart.main(Native Method)

(我的代码没有)

在infite循环(而不是计时器)上运行的线程:

"Thread-6024" prio=5 tid=29 SUSPENDED
  | group="main" sCount=1 dsCount=0 obj=0x42b0d8a8 self=0x4eb18e68
  | sysTid=6394 nice=0 sched=0/0 cgrp=apps handle=1319854912
  | state=S schedstat=( 19956711686 10027926621 12249 ) utm=1909 stm=86 core=1
  at myPackage.methodInSynchronizedBlock(mySource.java:~42)
 myPackage.MyThread.run(MyThread.java:14)

计时器安排的线程:

"Timer-1" prio=5 tid=26 MONITOR
  | group="main" sCount=1 dsCount=0 obj=0x42a70438 self=0x4ec3bc80
  | sysTid=6355 nice=0 sched=0/0 cgrp=apps handle=1336864072
  | state=S schedstat=( 1956706646 728161668 2848 ) utm=180 stm=15 core=1
  at myPackage.otherFunctionInSynchronizedBlock(myOtherSource.java:~90)
  - waiting to lock <0x42739230> (a java.lang.Object) held by tid=29 (Thread-6024)
  at myPackage.MyTicker.run(MyTicker.java:76)
  at java.util.Timer$TimerImpl.run(Timer.java:284)

两个运行方法都需要4毫秒来处理。

我认为这里发生了什么:

  • 主线程想要锁定某些东西(我没有锁定任何东西, 但我不知道它在等什么。)
  • 我认为它不是在等待其他2个线程,因为跟踪 文件说:“由tid = 1(主要)持有”

如果您知道问题,请帮助我

编辑:

主线程在触摸之前死亡(其他元素没有刷新)。但是当我触摸屏幕时会出现ANR

0 个答案:

没有答案