我的布局有一个SurfaceView。有时当我的应用程序从后台切换到前台时,我会得到ANR。我认为原因是主线程被lock方法阻止。
最重要的部分是:
JNI:CheckJNI已关闭;解决方法已关闭;销= 1; globals = 383(加1 弱) DALVIK THREADS :(互斥:tll = 0 tsl = 0 tscl = 0 ghl = 0)
"主" prio = 5 tid = 1 WAIT |基团="主" 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方法) - 等待< 0x4160ff28> (java.lang.VMThread)由tid = 1(main)在java.lang.Thread.parkFor(Thread.java:1205)中保存 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) 在 java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:846) 在 java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1175) 在 java.util.concurrent.locks.ReentrantLock中的$ NonfairSync.lock(ReentrantLock.java:180) 在 java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:256) 在android.view.SurfaceView.updateWindow(SurfaceView.java:524)at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:239) 在android.view.View.dispatchWindowVisibilityChanged(View.java:8004)
在 android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) 在 android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) 在 android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) 在 android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) 在 android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1239) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1002)at android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5713) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761) 在android.view.Choreographer.doCallbacks(Choreographer.java:574)at android.view.Choreographer.doFrame(Choreographer.java:544)at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747) 在android.os.Handler.handleCallback(Handler.java:733)处 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) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)at dalvik.system.NativeStart.main(原生方法)
你看到线程(id = 1)正在等待线程持有的锁(tid = 1)!怎么会这样?我对此有错误的理解吗?
答案 0 :(得分: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 {
//..................
}