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
广告
© . All rights reserved.