如果执行try或catch代码的线程被中断或杀死,即使整个应用程序继续运行,finally块也可能不会执行。
我尝试了下面的代码来理解它,但是它不能像Java文档中提到的那样工作
public class TestFinally extends Thread {
public void run() {
try {
Thread.sleep(1000);
System.out.println("task");
} catch (InterruptedException e) {
// throw new RuntimeException("Thread interrupted..." + e);
} finally {
System.out.println("I am not executing");
}
}
public static void main(String args[]) {
TestFinally t1 = new TestFinally();
t1.start();
try {
t1.interrupt();
} catch (Exception e) {
System.out.println("Exception handled " + e);
}
}
}