Java.util.TimerTask 类

java.util.TimerTask.scheduledExecutionTime()方法返回该任务最近一次实际执行的计划执行时间.

语法

public long scheduledExecutionTime()

参数

无需参数。

返回值

以 Date.getTime() 返回的格式返回该任务最近一次执行的计划时间。如果任务尚未开始第一次执行,则返回值未定义。

异常

无。

示例:

In下面的示例中,java.util.TimerTask.scheduledExecutionTime()方法用于获取给定任务最近实际执行的计划执行时间。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建定时器
    Timer timer = new Timer();

    //创建一个定时器任务
    TimerTask tt = new TimerTask() {
      public void run() {
        System.out.println("Working on the task.");
      };
    };
    
    //调度要执行的任务
    //立即执行
    //1000毫秒周期的任务
    timer.schedule(tt, 0, 1000);

    //检查计划执行时间
    System.out.println("Time is :" + tt.scheduledExecutionTime());
  }
}

上述代码的输出将是:

Working on the task.
Time is :1620445689194
Working on the task.
Working on the task.
Working on the task.
Working on the task.
Working on the task.
and so on ...