Java TimerTask scheduledExecutionTime() 方法



描述

scheduledExecutionTime() 方法用於返回此任務最近實際執行的計劃執行時間。

宣告

以下是 java.util.TimerTask.scheduledExecutionTime() 方法的宣告。

public long scheduledExecutionTime()

引數

返回值

方法呼叫返回此任務最近一次執行的計劃發生時間。

異常

以固定速率執行計時器示例

以下示例演示了 Java Timer scheduledExecutionTime() 方法的使用,用於列印任務最近一次執行的執行時間。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類的自定義類,並實現了將在計劃時間執行的 run() 方法。然後我們建立了一個計時器物件,並使用 scheduleAtFixedRate() 計劃了一個任務,以從現在開始每 100 毫秒執行一次。然後我們使用 scheduledExecutionTime() 方法列印了執行時間。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTaskDemo {
   public static void main(String[] args) {

      // creating timer task, timer
      TimerTask tasknew = new CustomTimerTask();
      Timer timer = new Timer();

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);
      
      // checking scheduled execution time
      System.out.println("Time is :"+tasknew.scheduledExecutionTime());

      try {
         Thread.sleep(500);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Time is :1675410212458
working on
working on
working on
working on
working on
working on

以固定速率執行作為守護程序的計時器示例

以下示例演示了 Java Timer scheduledExecutionTime() 方法的使用,用於列印任務最近一次執行的執行時間。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類的自定義類,並實現了將在計劃時間執行的 run() 方法。然後我們建立了一個作為守護執行緒的計時器物件,並使用 scheduleAtFixedRate() 計劃了一個任務,以從現在開始每 100 毫秒執行一次。然後我們使用 scheduledExecutionTime() 方法列印了執行時間。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTaskDemo {
   public static void main(String[] args) {

      // creating timer task, timer
      TimerTask tasknew = new CustomTimerTask();
      Timer timer = new Timer(true);

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);

      // checking scheduled execution time
      System.out.println("Time is :"+tasknew.scheduledExecutionTime());

      try {
         Thread.sleep(500);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Time is :1675410564119
working on
working on
working on
working on
working on
working on

以固定速率執行命名守護程序計時器示例

以下示例演示了 Java Timer scheduledExecutionTime() 方法的使用,用於列印任務最近一次執行的執行時間。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類的自定義類,並實現了將在計劃時間執行的 run() 方法。然後我們建立了一個作為守護執行緒的計時器物件,並賦予其一個名稱,並使用 scheduleAtFixedRate() 計劃了一個任務,以從現在開始每 100 毫秒執行一次。然後我們使用 scheduledExecutionTime() 方法列印了執行時間。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTaskDemo {
   public static void main(String[] args) {

      // creating timer task, timer
      TimerTask tasknew = new CustomTimerTask();
      Timer timer = new Timer("test",true);

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);
      
      // checking scheduled execution time
      System.out.println("Time is :"+tasknew.scheduledExecutionTime());

      try {
         Thread.sleep(500);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Time is :1675410583664
working on
working on
working on
working on
working on
working on
java_util_timertask.htm
廣告

© . All rights reserved.