UncaughtExceptionHandler 小结,2024年大厂Android面经

================================

我们可以看下官方文档对于这个函数的解释:

Interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception.
When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler’s uncaughtException method, passing the thread and the exception as arguments. If a thread has not had its UncaughtExceptionHandler explicitly set, then its ThreadGroup object acts as its UncaughtExceptionHandler. If the ThreadGroup object has no special requirements for dealing with the exception, it can forward the invocation to the default uncaught exception handler.

当一个线程即将因不受捕获的异常即将终止时,JVM 尝试会使用 Thread.getUncaughtExceptionHandler() 方法获得该线程的 UncaughtExceptionHandler 对象并调用其 uncaughtException 方法,其参数为该线程和异常信息。如果没有设置,那么 ThreadGroup 将会作为默认的 UncaughtExceptionHandler,如果 ThreadGroup 也没有处理,那么会采用 default uncaught exception handler。

由此可见 UncaughtExceptionHandler 是由虚拟机通过 dispatchUncaughtException 触发的,调用链为:

  • UncaughtExceptionHandler:线程的成员变量,如果没有设置 UncaughtExceptionHandler,那么会调用 group 成员处理。

public class Thread implements Runnable {

public UncaughtExceptionHandler getUncaughtExceptionHandler() {
return uncaughtExceptionHandler != null ?
uncaughtExceptionHandler : group;
}

public final void dispatchUncaughtException(Throwable e) {
getUncaughtExceptionHandler().uncaughtException(this, e);
}
}

  • ThreadGroup:group 也是实现了 UncaughtExceptionHandler 接口,内部逻辑是先委托其 parent 处理,直到 parent 为空时,最终 parent 为空时才会走到调用 sDefaultUncaughtExceptionHandler。

public class ThreadGroup implements Thread.UncaughtExceptionHandler {

public void uncaughtException(Thread t, Throwable e) {
if (parent != null) {
parent.uncaughtException(t, e);
} else {
Thread.UncaughtExceptionHandler ueh =
Thread.getDefaultUncaughtExceptionHandler();
if (ueh != null) {
ueh.uncaughtException(t, e);
} else if (!(e instanceof ThreadDeath)) {
System.err.print("Exception in thread “”

  • t.getName() + “” ");
    e.printStackTrace(System.err);
    }
    }
    }
    }
  • sDefaultUncaughtExceptionHandler:线程的静态成员变量,用于处理该进程中的所有线程,也就是我们在上一部分所举的例子。

这里解释一下:

  • 对于子线程来说,其 ThreadGroup 为主线程 [name=main, maxpri=10]
  • 主线程的 parent 也是一个 ThreadGroup [name=system, maxpri=10],其 parent 为 null。

流程图如下: 流程图

四、为什么主线程会出现无法响应,而子线程不会

首先我们要知道应用程序和系统间的交互基于的是 消息驱动 的模型:

  • 事件源通过 Binder 调用传递到应用程序进程后,会将处理的消息加入到消息队列当中,例如按键、触摸、绘制等。
  • 应用程序再不断地从消息队列中取出消息进行处理。

应用程序是在其主线程进行处理这些消息的,这里是通过一个无限循环,即 Looper.loop(),没有消息时休眠,有消息时被唤醒去处理消息,因此主线程不能够结束,结束了就没法处理消息了。

即 ActivityThread.main 方法:

public static void main(String[] args) {
Looper.prepareMainLooper();
Looper.loop();
throw new RuntimeException(“Main thread loop unexpectedly exited”);
}

由于主线程结束了,那么就无法及时处理 AMS/WMS 发送的消息或者给予反馈,就会触发系统调用错误或者 ANR。
而子线程由于不涉及这些,结束就结束了,和任务执行完了没什么区别,因此也就不会产生异常。
无、默认的处理规则

假如我们没有设置自定义的 handler,那么系统是有自己一套默认的处理逻辑的,这段代码在应用进程启动时,即 RuntimeInit#commonInit 中:
protected static final void commonInit() {
LoggingHandler loggingHandler = new LoggingHandler();
RuntimeHooks.setUncaughtExceptionPreHandler(loggingHandler);
Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler));
}
KillApplicationHandler 会通过 Binder 调用到 AMS 端进行异常信息的记录,最后会调用 Process.killProcess(Process.myPid()) & System.exit(10) 结束掉应用进程,这也是为什么在没有设置自定的 handler 情况下,子线程当中发生异常会导致应用程序退出。

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个学习思路及方向,从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。

image

学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。**

[外链图片转存中…(img-qtVTHKQZ-1711806045713)]

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值