gpt4 book ai didi

java - 执行者或线程防止程序关闭

转载 作者:搜寻专家 更新时间:2023-11-01 02:59:34 25 4
gpt4 key购买 nike

在我的程序中,创建了一些线程并给定了一个结束的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?我找不到关闭它的方法。

我应该做哪些其他清理工作?

最佳答案

是的,这正是因为您没有在 newSingleThreadExecutor 上调用关闭,但如果您有其他正在运行的线程,它们也可能会阻止应用程序退出。要强制 VM 关闭(所有线程都将终止),您可以调用 System.exit(0);

您将 Executor 服务创建为:

ExecutorService service = Executors.newSingleThreadScheduledExecutor();

\\ use the service to do work
\\ to shutdown the service
service.shutdown();

\\ to wait till the service terminate
service.awaitTermination();

关于java - 执行者或线程防止程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39477750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com