當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。