void schedule(TimerTask task, long delay)
描述 (Description)
schedule(TimerTask task,long delay)方法用于在指定的延迟之后调度指定的任务以执行。
声明 (Declaration)
以下是java.util.Timer.schedule()方法的声明。
public void schedule(TimerTask task,long delay)
参数 (Parameters)
task - 这是要安排的任务。
delay - 这是执行任务之前的延迟(以毫秒为单位)。
返回值 (Return Value)
NA
异常 (Exception)
IllegalArgumentException - 如果time.getTime()为负数,则抛出此异常。
IllegalStateException - 如果已安排或取消任务,取消计时器或终止计时器线程,则抛出此异常。
例子 (Example)
以下示例显示了java.util.Timer.schedule()的用法
package com.iowiki;
import java.util.*;
public class TimerDemo {
public static void main(String[] args) {
// creating timer task, timer
TimerTask tasknew = new TimerScheduleDelay();
Timer timer = new Timer();
// scheduling the task at interval
timer.schedule(tasknew, 100);
}
// this method performs the task
public void run() {
System.out.println("timer working");
}
}
让我们编译并运行上面的程序,这将产生以下结果。
timer working