我的项目中遇到 NullPointerException,我想评估这个问题的严重性。异常是在线程完成其任务之前抛出的,并且无论如何都会死亡。 我的代码未能捕获此异常,因此线程已死亡。
这是对情况的简单模拟:
public class Test extends Thread {
public static void main(String[] args) throws InterruptedException {
Test thread = new Test();
thread.start();
while(true) {
System.out.println("I'm still here!");
Thread.sleep(1000);
}
}
public void run() {
String s = null;
int len = s.length(); // causes NullPinterException
}
}
我的问题是:这个可怜的线程现在会发生什么?它的linux文件描述符被释放了吗?这种代码是否会出现稳定性或内存问题?
最佳答案
处理方式与任何其他终止线程没有什么不同。之前发生的一件事是搜索 UncaughtExceptionHandler根据规则(特定Thread
、ThreadGroup
、所有线程),但除此之外,“正常”清理过程如下。与“正常”终止相比,当线程因未捕获的异常而终止时,不会产生有关系统资源(取决于Thread
实现)或内存问题的具体后果。
关于java - Linux 中死掉的 java 线程发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13378513/