java.util.Timer.schedule() 方法
描述
schedule(TimerTask task, Date time) 方法用于调度指定任务在指定时间执行。
声明
以下是 java.util.Timer.schedule() 方法的声明。
public void schedule(TimerTask task, Date time)
参数
task − 这是要安排的任务。
时间 − 这是要执行任务的时间。
返回值
NA
异常
IllegalArgumentException − 如果 time.getTime() 为负数,则会引发此异常。
IllegalStateException − 如果任务已被调度或取消、定时器被取消或定时器线程终止,则会抛出此错误。
示例
下面的例子展示了 java.util.Timer.schedule() 的用法。
package com.tutorialspoint;
import java.util.*;
public class TimerDemo {
public static void main(String[] args) {
// creating timer task, timer
TimerTask tasknew = new TimerCancel();
Timer timer = new Timer();
// scheduling the task
timer.schedule(tasknew, new Date());
}
// this method performs the task
public void run() {
System.out.println("working on");
}
}
让我们编译并运行上面的程序,这将产生以下结果.
working on

