5

My layout has a SurfaceView. Sometimes when my app switches from background to foreground, I get ANR. I think the reason is that the main thread is blocked by the lock method.

The most important part is :

JNI: CheckJNI is off; workarounds are off; pins=1; globals=383 (plus 1 weak)

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

"main" prio=5 tid=1 WAIT | group="main" sCount=1 dsCount=0 obj=0x4160fe58 self=0x41529b58 | sysTid=19249 nice=0 sched=0/0 cgrp=apps handle=1074282836 | state=S schedstat=( 140142818375 80057200431 501675 ) utm=11559 stm=2455 core=1 at java.lang.Object.wait(Native Method) - waiting on <0x4160ff28> (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 android.view.SurfaceView.updateWindow(SurfaceView.java:524) at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:239) at android.view.View.dispatchWindowVisibilityChanged(View.java:8004)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1239) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1002) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5713) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5120) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:818) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634) at dalvik.system.NativeStart.main(Native Method)

You see that thread(id=1) is waiting on a lock held by the thread(tid=1)! How could that be ? Do I have a wrong understanding about this?

suitianshi
  • 3,300
  • 1
  • 17
  • 34

1 Answers1

0

http://androidxref.com/4.4.2_r2/xref/libcore/libdvm/src/main/java/java/lang/Thread.java

 public void parkFor(long nanos) {
    VMThread vmt = vmThread;

    if (vmt == null) {
        // Running threads should always have an associated vmThread.
        throw new AssertionError();
    }
    synchronized (vmt) {
        switch (parkState) {
            case ParkState.PREEMPTIVELY_UNPARKED: {
                parkState = ParkState.UNPARKED;
                break;
            }
            case ParkState.UNPARKED: {
                long millis = nanos / NANOS_PER_MILLI;
                nanos %= NANOS_PER_MILLI;

                parkState = ParkState.PARKED;
                try {
                    vmt.wait(millis, (int) nanos);//<==//here  , maybe it's a normal stack.
                } catch (InterruptedException ex) {
                    interrupt();
                } finally {
                //..................
                }
Larry Liang
  • 109
  • 1
  • 3