实际上,我希望程序在单击红色X时退出,但我不希望跳过finally{}块!我在GUI中手动放置了最重要的finally块部分,但我真的不想这样做,因为我希望GUI与实际程序解耦。
class GUI { // ...
...
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
try {
processObject.getIndicatorFileStream().close();
} catch (Exception ignore) {}
System.exit(0);
}
});
...
}
但我更希望像这样进行通话:
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
请确保在每个线程退出后,所有的finally{}块都被调用。
我知道这实际上是期望的。如果应用程序从另一个线程(比如GUI线程)关闭,那么主线程就会停在它的轨迹上。
简而言之,我该如何确保System.exit(0)或JFrame.EXIT_ON_CLOSE仍会导致每个线程的finally块执行?