ANR with 3 threads - using locks

3

Main Thread: ("main" prio=5 tid=1)
Makes an ArrayList in onCreate()
Makes an object for locking. (lock)
Starts a Thread: ("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();
    }
}

Also start an other thread with 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()
        }
    }
}

The whole system works fine.
BUT:
Sometimes it gets an ANR

  • Both threads running nicely (one is simulating my GameWorld, the other is painting it on a SurfaceView)
  • I let them run for a long time (result is nice - smooth movements)
  • Then I touch the screen:
  • The 2 threads are running nicely - the surface view is refreshing nicely
  • Other UI components not refreshing (I invalidate them in the runOnUiThread)

After some seconds (it should be 5 as I know, but it is a little bit longer):
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)

Interesting parts of the trace_myPackage.txt:

The main thread:

"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)

(nothing from my code)

The thread that runs on infite loop (not the timer one):

"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)

The thread which is scheduled by the timer:

"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)

Both run method is like 4 ms to process.

What I think happens here:

  • The main Thread wants to lock something (I don't lock there anything, but I don't know what it is waiting for)
  • I think it is not waiting for the other 2 threads, because the trace file says: "held by tid=1 (main)"

Please help me if you know the problem

EDIT:

The Main Thread dies before touching it (other elements not refreshing). But ANR comes when I touch the screen

java
android
multithreading
asked on Stack Overflow Jan 16, 2015 by Tomi • edited Jan 16, 2015 by Tomi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0