Timer Schedule參數說明

來源:互聯網
上載者:User

標籤:style   blog   java   使用   io   ar   div   時間   

Timer是一種定時器工具,用來在一個後台線程計劃執行指定任務。它可以計劃執行一個任務一次或反覆多次。
TimerTask一個抽象類別,它的子類代表一個可以被Timer計劃的任務。

schedule的意思(時間表、進度表)
timer.schedule(new MyTask(event.getServletContext()), 0,60*60*1000);

第一個參數"newMyTask(event.getServletContext())":
是 TimerTask 類,在包:import java.util.TimerTask .使用者要繼承該類,並實現 publicvoid run() 方法,因為 TimerTask 類實現了 Runnable 介面。

第二個參數"0"的意思是:(0就表示無延遲)
當你調用該方法後,該方法必然會調用 TimerTask 類 TimerTask 類 中的 run()方法,這個參數就是這兩者之間的差值,轉換成漢語的意思就是說,使用者調用 schedule() 方法後,要等待這麼長的時間才可以第一次執行run() 方法。

第三個參數"60*60*1000"的意思就是:
(單位是毫秒60*60*1000為一小時)
(單位是毫秒3*60*1000為三分鐘)
第一次調用之後,從第二次開始每隔多長的時間調用一次 run() 方法

例子:

 public TimercreateJobber(TimerTask o, String cronExpress) throws Exception{
  Timer timer = newTimer();
  timer.schedule(o, 0,Integer.parseInt(cronExpress));
  return timer;
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.