JAVA 如果我在那个时间之后的每天下午 12:00 安排我的定时器任务会怎样?
JAVA What happen if I schedule my timertask everyday at 12:00 PM after that time?
我正在使用以下代码来安排计时器 (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 之后 运行ning 会发生什么。假设 16:00。请问定时任务运行第二天在12:00?
定时器 Class 的文档说明了以下关于方法 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 再次执行。
您可以考虑使用 ScheduledThreadPoolExecutor,因为
It is effectively a more versatile replacement for the
Timer/TimerTask combination
(link)
此外,Java 8 提供了一些有用的工具来计算所需的时间。例如:
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void schedule(Runnable command) {
LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime executionDate = LocalDateTime.of(currentTime.getYear(),
currentTime.getMonth(),
currentTime.getDayOfMonth(),
12, 0); // begin execution at 12:00 AM
long initialDelay;
if(currentTime.isAfter(executionDate)){
// take the next day, if we passed the execution date
initialDelay = currentTime.until(executionDate.plusDays(1), ChronoUnit.MILLIS);
} else {
initialDelay = currentTime.until(executionDate, ChronoUnit.MILLIS);
}
long delay = TimeUnit.HOURS.toMillis(24); // repeat after 24 hours
ScheduledFuture<?> x = scheduler.scheduleWithFixedDelay(command, initialDelay, delay , TimeUnit.MILLISECONDS);
}
您可以给一个时间 11:59 下午,然后您的问题就会得到解决。
它调用是因为 12:00 PM 日期将更改,因此它会调用您的任务。
所以将时间 12:00 PM 更改为 11:59
我一直在寻找同一个问题的答案,并且找到了一个可能的解决方案。请记住,我是一个新手,这样做可能会犯下许多编程罪行。
如果你的计时器是 运行 无论如何为什么不像这样检查特定时间:
if(date.compareTo("00:00:00") == 0){
//TODO
}
我正在使用以下代码来安排计时器 (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 之后 运行ning 会发生什么。假设 16:00。请问定时任务运行第二天在12:00?
定时器 Class 的文档说明了以下关于方法 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 再次执行。
您可以考虑使用 ScheduledThreadPoolExecutor,因为
It is effectively a more versatile replacement for the Timer/TimerTask combination (link)
此外,Java 8 提供了一些有用的工具来计算所需的时间。例如:
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void schedule(Runnable command) {
LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime executionDate = LocalDateTime.of(currentTime.getYear(),
currentTime.getMonth(),
currentTime.getDayOfMonth(),
12, 0); // begin execution at 12:00 AM
long initialDelay;
if(currentTime.isAfter(executionDate)){
// take the next day, if we passed the execution date
initialDelay = currentTime.until(executionDate.plusDays(1), ChronoUnit.MILLIS);
} else {
initialDelay = currentTime.until(executionDate, ChronoUnit.MILLIS);
}
long delay = TimeUnit.HOURS.toMillis(24); // repeat after 24 hours
ScheduledFuture<?> x = scheduler.scheduleWithFixedDelay(command, initialDelay, delay , TimeUnit.MILLISECONDS);
}
您可以给一个时间 11:59 下午,然后您的问题就会得到解决。 它调用是因为 12:00 PM 日期将更改,因此它会调用您的任务。 所以将时间 12:00 PM 更改为 11:59
我一直在寻找同一个问题的答案,并且找到了一个可能的解决方案。请记住,我是一个新手,这样做可能会犯下许多编程罪行。
如果你的计时器是 运行 无论如何为什么不像这样检查特定时间:
if(date.compareTo("00:00:00") == 0){
//TODO
}