目录

void schedule(TimerTask task, Date time)

描述 (Description)

schedule(TimerTask task, Date time)方法用于调度指定任务以在指定时间执行。

声明 (Declaration)

以下是java.util.Timer.schedule()方法的声明。

public void schedule(TimerTask task, Date time)

参数 (Parameters)

  • task - 这是要安排的任务。

  • time - 这是执行任务的时间。

返回值 (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 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
↑回到顶部↑
WIKI教程 @2018