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


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


描述

這個schedule(TimerTask task,long delay)方法用於安排指定的任務在指定的延遲後執行。

聲明

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

public void schedule(TimerTask task,long delay)

參數

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

  • delay- 這是執行任務之前的延遲(以毫秒為單位)。

返回值

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 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

相關用法


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