【发布时间】:2017-01-21 12:15:33
【问题描述】:
在我的程序中创建了一些线程并给出了一个结束的Task(这样线程应该死掉),我还有一个Executors.newSingleThreadScheduledExecutor 和Executors.newSingleThreadExecutor,我注意到如果我按下一个关闭按钮,我制作:
@FXML private void handleExit(){
gameManager.cleanup();
Stage stage = (Stage)menuBarTopMenu.getScene().getWindow();
Platform.exit();
stage.close();
}
我在 intelij 中没有得到 Process finished with exit code 0。
所以我什至将我提到的那些线程设置为守护进程,这就是清理工作:
public void cleanup(){
updateTime.shutdown(); // it's Executors.newSingleThreadScheduledExecutor
updateTime.shutdownNow();
}
但我仍然没有看到成功退出。难道是因为我没有关闭Executors.newSingleThreadExecutor?我找不到关闭它的方法。
我还应该做什么其他清理工作?
【问题讨论】:
-
在您尝试退出后检查线程转储(或使用诸如 visualvm 之类的工具)并查看仍在运行的内容。
-
@NicolasFilotto 这不是我无法关闭的服务,而是
newSingleThreadExecutor。 -
它是一回事,它只是你在重复答案中的一个特定用例
-
我会做一个线程转储来确定哪些非守护线程实际上仍在运行
标签: java multithreading javafx javafx-8