Most visited

Recently visited

Added in API level 1

TimerTask

public abstract class TimerTask
extends Object implements Runnable

java.lang.Object
   ↳ java.util.TimerTask


可以安排一次性或由计时器重复执行的任务。

也可以看看:

Summary

Protected constructors

TimerTask()

创建一个新的计时器任务。

Public methods

boolean cancel()

取消此计时器任务。

abstract void run()

此计时器任务执行的操作。

long scheduledExecutionTime()

返回此任务的最近 实际执行的 计划执行时间。

Inherited methods

From class java.lang.Object
From interface java.lang.Runnable

Protected constructors

TimerTask

Added in API level 1
TimerTask ()

创建一个新的计时器任务。

Public methods

cancel

Added in API level 1
boolean cancel ()

取消此计时器任务。 如果该任务已计划为一次执行且尚未运行或尚未排定,则该任务将永不运行。 如果任务已经计划重复执行,它将永远不会再次运行。 (如果在发生此调用时任务正在运行,则该任务将运行至完成,但不会再次运行。)

请注意,从重复计时器任务的 run方法内调用此方法可绝对保证计时器任务不会再次运行。

这种方法可能会被重复调用; 第二次和后续的呼叫都不起作用。

Returns
boolean 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. (Loosely speaking, this method returns true if it prevents one or more scheduled executions from taking place.)

run

Added in API level 1
void run ()

此计时器任务执行的操作。

scheduledExecutionTime

Added in API level 1
long scheduledExecutionTime ()

返回此任务的最近实际执行的计划执行时间。 (如果在任务执行过程中调用此方法,则返回值是正在执行的任务执行的预定执行时间。)

通常从任务的运行方法中调用此方法,以确定任务的当前执行是否足够及时以保证执行预定活动:

   public void run() {
       if (System.currentTimeMillis() - scheduledExecutionTime() >=
           MAX_TARDINESS)
               return;  // Too late; skip this execution.
       // Perform the task
   }
 
This method is typically not used in conjunction with fixed-delay execution repeating tasks, as their scheduled execution times are allowed to drift over time, and so are not terribly significant.

Returns
long 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.

也可以看看:

Hooray!