oppo手机java.util.concurrent.TimeoutException异常处理

今天收到了用户反馈app发帖会闪退,了解用户的手机是oppo和app版本后,就去听云bug列表查看。定位到听云bug如下:

java.util.concurrent.TimeoutException: android.content.res.AssetManager.finalize() timed out after 10 seconds
	android.content.res.AssetManager.destroy(Native Method)
	android.content.res.AssetManager.finalize(AssetManager.java:574)
	java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:197)
	java.lang.Daemons$FinalizerDaemon.run(Daemons.java:176)
	java.lang.Thread.run(Thread.java:818)

啥都看不出来,而且还纳闷为什么链接超时会引起闪退。。。
没办法,只能追踪报错的地方了java.lang.Daemons

 private static void finalizerTimedOut(Object object) {
            // The current object has exceeded the finalization deadline; abort!
            String message = object.getClass().getName() + ".finalize() timed out after "
                    + (MAX_FINALIZE_NANOS / NANOS_PER_SECOND) + " seconds";
            Exception syntheticException = new TimeoutException(message);
            // We use the stack from where finalize() was running to show where it was stuck.
            syntheticException.setStackTrace(FinalizerDaemon.INSTANCE.getStackTrace());
            Thread.UncaughtExceptionHandler h = Thread.getDefaultUncaughtExceptionHandler();
            // Send SIGQUIT to get native stack traces.
            try {
                Os.kill(Os.getpid(), OsConstants.SIGQUIT);
                // Sleep a few seconds to let the stack traces print.
                Thread.sleep(5000);
            } catch (Exception e) {
                System.logE("failed to send SIGQUIT", e);
            } catch (OutOfMemoryError ignored) {
                // May occur while trying to allocate the exception.
            }
            if (h == null) {
                // If we have no handler, log and exit.
                System.logE(message, syntheticException);
                System.exit(2);
            }
            // Otherwise call the handler to do crash reporting.
            // We don't just throw because we're not the thread that
            // timed out; we're the thread that detected it.
            h.uncaughtException(Thread.currentThread(), syntheticException);
        }

可以看出我们的报错信息android.content.res.AssetManager.finalize() timed out after 10 seconds其实就是finalizerTimedOut()方法中的message。
而当UncaughtExceptionHandler异常为null的时候,系统会抛出异常并退出app。

而解决方法有两种:
第一种是调用java.lang.Daemons类中的stop方法然后在重新启动守护线程。

 /**
     * 解决oppoR9 TimeoutExceptions
     */
    public void fix() {
        try {
            Class clazz = Class.forName("java.lang.Daemons$FinalizerWatchdogDaemon");

            Method method = clazz.getSuperclass().getDeclaredMethod("stop");
            method.setAccessible(true);

            Field field = clazz.getDeclaredField("INSTANCE");
            field.setAccessible(true);

            method.invoke(field.get(null));

        }
        catch (Throwable e) {
            e.printStackTrace();
        }
    }

第二种方法将timeout时间设置超长

 try {
            Class<?> c = Class.forName("java.lang.Daemons");
            Field maxField = c.getDeclaredField("MAX_FINALIZE_NANOS");
            maxField.setAccessible(true);
            maxField.set(null, Long.MAX_VALUE);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

这里只给了解决方法和关键代码,如果还想了解的更加彻底查看本文参考:
本文参考:
http://www.courtier.cc/2017/04/28/关于timeoutexception这件小事/

https://blog.csdn.net/zlmrche/article/details/81365204

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值