gpt4 book ai didi

java - 计划任务使应用程序保持 Activity 状态

转载 作者:行者123 更新时间:2023-11-30 04:14:21 25 4
gpt4 key购买 nike

我有一个用 javafx 制作的应用程序,它使用 scheduledFuture = ScheduledExecutorService.scheduleAtFixedRate(runnable, initialDelay, 24*60*60*1000, TimeUnit.Milliseconds) 安排每天运行的可运行程序

即使我使用 scheduledFuture.cancel(true) 取消此任务,我的应用程序在用户关闭后仍会在后台保持 Activity 状态。

我很确定这个调度是保持应用程序存活的唯一方法。如果我不安排它,应用程序就会关闭。

最佳答案

调用shutdown() scheduledExecutorService 上允许程序正常关闭。

scheduledExecutorService.shutdown();

根据 javadocs,

Initiates an orderly shutdown in which previously submitted tasks are executed

调用 cancel(true)只是取消任务,使执行程序服务保持 Activity 状态,shutdown() 也会终止执行程序服务,允许程序退出。

关于java - 计划任务使应用程序保持 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18775623/

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