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


Java java.util.Timer.schedule()用法及代碼示例


描述

這個schedule(TimerTask task, Date time)方法用於安排指定任務在指定時間執行。

聲明

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

public void schedule(TimerTask task, Date time)

參數

  • task- 這是要安排的任務。

  • time- 這是要執行任務的時間。

返回值

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

相關用法


注:本文由純淨天空篩選整理自 java.util.Timer.schedule() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。