gpt4 book ai didi

JAVA 如果我每天将计时器任务安排在 12 :00 PM after that time? 会发生什么

转载 作者:行者123 更新时间:2023-12-01 18:15:57 24 4
gpt4 key购买 nike

我使用以下代码来安排计时器 (java.util.Timer):

Timer mytimer = new Timer("My Timer");
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 12);
mytimer.schedule(mytask, c.getTime(), 24*60*60*1000);

我希望计时器任务每天中午 12:00 运行。我的问题是,如果应用程序在 12:00 之后运行,会发生什么情况。假设是 16:00。定时任务会在第二天 12:00 运行吗?

最佳答案

Timer 类的文档对方法 public void schedule(TimerTask task, Date firstTime, long period) 做了如下说明:

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate). As a consequence of the above, if the scheduled first time is in the past, it is scheduled for immediate execution.

所以从上面我们可以理解,任务会立即被调度执行,然后按照你的程序,24小时后再次执行。所以如果是16:00则立即执行,并在第二天16:00再次执行。

关于JAVA 如果我每天将计时器任务安排在 12 :00 PM after that time? 会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29517561/

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