![](/img/trans.png)
[英]Cancelling a task scheduled in a ScheduledExecutorService keeps executor alive
[英]Scheduled task keeping application alive
我有一个使用javafx制作的应用程序,它使用scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(runnable, initialDelay, 24*60*60*1000, TimeUnit.Milliseconds)
来scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(runnable, initialDelay, 24*60*60*1000, TimeUnit.Milliseconds)
每天运行的可运行scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(runnable, initialDelay, 24*60*60*1000, TimeUnit.Milliseconds)
即使我使用scheduledFuture.cancel(true)
取消了此任务,但在用户关闭应用程序后,我的应用程序仍在后台运行。
我很确定,只有这种安排才能使应用程序保持活动状态。 如果我不安排时间,应用程序将关闭。
在scheduledExecutorService
上调用shutdown()
可使程序正常关闭。
scheduledExecutorService.shutdown();
根据javadocs,
启动有序关闭,在该关闭中执行先前提交的任务
调用cancel(true)
只会取消任务,而使执行程序服务保持活动状态,而shutdown()
也会终止执行程序服务,从而允许程序退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.