android – 主线程被Object.wait自行阻止?
作者:互联网
我的布局有一个SurfaceView.有时当我的应用程序从后台切换到前台时,我会得到ANR.我认为原因是主线程被lock方法阻止.
最重要的部分是:
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)
你看到线程(id = 1)正在等待线程持有的锁(tid = 1)!怎么会这样?我对此有错误的理解吗?
解决方法:
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 {
//..................
}
标签:android,android-anr-dialog 来源: https://codeday.me/bug/20190711/1431650.html