Java TimerTask run() 方法



描述

Java TimerTask run() 方法負責執行此計時器任務的操作。

宣告

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

public abstract void run()

引數

返回值

異常

執行計時器操作示例

以下示例演示瞭如何使用 Java Timer run() 方法根據計劃執行計時器操作。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類並實現 run() 方法的自定義類,該方法將在計劃時間執行。然後,我們建立了一個計時器物件,並使用 scheduleAtFixedRate() 方法安排了一個任務,該任務從現在開始每 100 毫秒執行一次。

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);

      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");
   }  
}

輸出

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

working on
working on
working on
working on
working on
working on

執行計時器操作作為守護程序示例

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

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);

      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");
   }  
}

輸出

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

working on
working on
working on
working on
working on
working on

執行命名計時器作為守護程序操作示例

以下示例演示瞭如何使用 Java Timer run() 方法根據計劃執行計時器操作。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類並實現 run() 方法的自定義類,該方法將在計劃時間執行。然後,我們建立了一個作為守護執行緒的計時器物件(並指定了名稱),並使用 scheduleAtFixedRate() 方法安排了一個任務,該任務從現在開始每 100 毫秒執行一次。

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);

      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");
   }  
}

輸出

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

working on
working on
working on
working on
working on
working on
java_util_timertask.htm
廣告