当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.util.TimerTask用法及代码示例


TimerTask是java.util包中定义的抽象类。 TimerTask 类定义了一个可以安排运行一次或重复多次的任务。为了定义TimerTask对象,需要实现该类并重写run方法。当计时器对象安排 run 方法执行时,它会被隐式调用。

注意:TimerTask 类的实例用于定义需要定期运行的任务。

构造函数:

  • TimerTask():创建一个新的定时器任务

声明:

public abstract class TimerTask
        extends Object
        implements Runnable

方法:

  • cancel(): java.util.TimerTask.cancel()取消此定时任务
  • 用法:
public boolean cancel()
返回:
true if this task is scheduled for one-time execution and
has not yet run, or this task is scheduled for repeated execution. 
Returns false if the task was scheduled for one-time 
execution and has already run, or if the task was never scheduled, 
or if the task was already cancelled.
  • run(): java.util.TimerTask.run()该定时器任务要执行的操作
  • 用法:
public abstract void run()
Description:
The action to be performed by this timer task
  • scheduledExecutionTime(): java.util.TimerTask.scheduledExecutionTime()返回该任务最近一次实际执行的计划执行时间
  • 用法:
public long scheduledExecutionTime()
Returns: 
the time at which the most recent execution of this task was 
scheduled to occur, in the format returned by Date.getTime(). 
The return value is undefined if the task has yet to 
commence its first execution

Methods inherited from class java.lang.Object

  • clone
  • equals
  • finalize
  • getClass
  • hashCode
  • notify
  • notifyAll
  • toString
  • wait

Java program to demonstrate usage of TimerTask class

Java


// Java program to demonstrate 
// working of TimerTask class
import java.util.Timer;
import java.util.TimerTask;
class Helper extends TimerTask
{
    public static int i = 0;
    public void run()
    {
        System.out.println("Timer ran" + ++i);
        if(i == 4)
        {
            synchronized(Test.obj)
            {
                Test.obj.notify();
            }
        }
    }
     
}
public class Test
{
    public static Test obj;
    public static void main(String[] args) throws InterruptedException
    {
        obj = new Test();
         
        // creating an instance of timer class
        Timer timer = new Timer();
         
        // creating an instance of task to be scheduled
        TimerTask task = new Helper();
         
        // scheduling the timer instance
        timer.schedule(task, 1000, 3000);
         
        // fetching the scheduled execution time of 
        // the most recent actual execution of the task
        System.out.println(task.scheduledExecutionTime());
         
        synchronized(obj)
        {
            //this thread waits until i reaches 4
            obj.wait();
        }
         
        //canceling the task assigned 
        System.out.println("Cancel the timer task: " + task.cancel());
         
        // at this point timer is still running 
        // without any task assigned to it
     
        // canceling the timer instance created
        timer.cancel();
    }
}

输出:

1495715853591
Timer ran 1
Timer ran 2
Timer ran 3
Timer ran 4
Cancel the timer task: true

参考:



相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.util.TimerTask class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。