JAVA如果我每天将timertask安排在12:00之后会发生什么?

JAVA如果我每天将timertask安排在12:00之后会发生什么?,java,timer,timertask,Java,Timer,Timertask,我使用以下代码来安排计时器(java.util.timer): 我希望计时器任务在每天下午12:00运行。 我的问题是如果应用程序在12:00之后运行会发生什么。比如说16点。timer任务是否会在第二天12:00运行?timer类的文档说明了有关该方法的以下内容 在固定延迟执行中,每次执行都是相对于前一次执行的实际执行时间进行调度的。如果执行因任何原因(如垃圾收集或其他后台活动)而延迟,则后续执行也将延迟。从长远来看,执行频率通常会略低于指定周期的倒数(假设系统时钟底层Object.wait(

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

我希望计时器任务在每天下午12:00运行。
我的问题是如果应用程序在12:00之后运行会发生什么。比如说16点。timer任务是否会在第二天12:00运行?

timer类的文档说明了有关该方法的以下内容

在固定延迟执行中,每次执行都是相对于前一次执行的实际执行时间进行调度的。如果执行因任何原因(如垃圾收集或其他后台活动)而延迟,则后续执行也将延迟。从长远来看,执行频率通常会略低于指定周期的倒数(假设系统时钟底层Object.wait(long)是准确的)。由于上述原因,如果计划的第一次是在过去,则计划立即执行


因此,我们可以从上面了解到,任务将立即安排和执行,然后根据您的程序将在24小时后再次执行。因此,如果它是16:00,那么它将立即执行,并将在第二天16:00再次执行。

计时器类的文档说明了有关该方法的以下内容

在固定延迟执行中,每次执行都是相对于前一次执行的实际执行时间进行调度的。如果执行因任何原因(如垃圾收集或其他后台活动)而延迟,则后续执行也将延迟。从长远来看,执行频率通常会略低于指定周期的倒数(假设系统时钟底层Object.wait(long)是准确的)。由于上述原因,如果计划的第一次是在过去,则计划立即执行


因此,我们可以从上面了解到,任务将立即安排和执行,然后根据您的程序将在24小时后再次执行。因此,如果它是16:00,那么它将立即执行,并将在第二天16:00再次执行。

计时器类的文档说明了有关该方法的以下内容

在固定延迟执行中,每次执行都是相对于前一次执行的实际执行时间进行调度的。如果执行因任何原因(如垃圾收集或其他后台活动)而延迟,则后续执行也将延迟。从长远来看,执行频率通常会略低于指定周期的倒数(假设系统时钟底层Object.wait(long)是准确的)。由于上述原因,如果计划的第一次是在过去,则计划立即执行


因此,我们可以从上面了解到,任务将立即安排和执行,然后根据您的程序将在24小时后再次执行。因此,如果它是16:00,那么它将立即执行,并将在第二天16:00再次执行。

计时器类的文档说明了有关该方法的以下内容

在固定延迟执行中,每次执行都是相对于前一次执行的实际执行时间进行调度的。如果执行因任何原因(如垃圾收集或其他后台活动)而延迟,则后续执行也将延迟。从长远来看,执行频率通常会略低于指定周期的倒数(假设系统时钟底层Object.wait(long)是准确的)。由于上述原因,如果计划的第一次是在过去,则计划立即执行


因此,我们可以从上面了解到,任务将立即安排和执行,然后根据您的程序将在24小时后再次执行。因此,如果是16:00,那么它将立即执行,并将在第二天16:00执行。

< P>你可以考虑使用A,作为< /P> 它实际上是一个更通用的替代品 定时器/定时器任务组合

另外,Java8提供了一些有用的工具来进行所需的时间计算。例如:

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);
}
private final ScheduledExecutorService scheduler=Executors.newScheduledThreadPool(1);
公共作废计划(Runnable命令){
LocalDateTime currentTime=LocalDateTime.now();
LocalDateTime executionDate=LocalDateTime.of(currentTime.getYear(),
currentTime.getMonth(),
currentTime.getDayOfMonth(),
12,0);//上午12:00开始执行
长时间延迟;
if(currentTime.isAfter(执行日期)){
//以第二天为例,如果我们超过了执行日期
initialDelay=currentTime.until(executionDate.plusDays(1),ChronoUnit.MILLIS);
}否则{
initialDelay=currentTime.until(executionDate,ChronoUnit.MILLIS);
}
长延迟=时间单位.HOURS.toMillis(24);//24小时后重复
ScheduledFuture x=scheduler.scheduleWithFixedDelay(命令、初始延迟、延迟、时间单位.毫秒);
}

< /代码> 您可以考虑使用a,as < /p> 它实际上是一个更通用的替代品 定时器/定时器任务组合

另外,Java8提供了一些有用的工具来进行所需的时间计算。例如:

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);
}
private final ScheduledExecutorService scheduler=Executors.newScheduledThreadPool(1);
公共作废计划(Runnable命令){
LocalDateTime currentTime=LocalDateTime.now();
LocalDateTime executionDate=LocalDateTime.of(currentTime.getYear(),
if(date.compareTo("00:00:00") == 0){
    //TODO
}