閱讀340 返回首頁    go 阿裏雲 go 技術社區[雲棲]


java關於Timer schedule執行定時任務

 

公司技術人員在實現內部辦公係統與外部網站一體化的時候,最重要的步驟就是從OA係統讀取數據,並且根據網站模板生成最終的靜態頁麵。這裏就需要一個定時任務,循環的執行。

技術人員在寫定時任務的時候,想當然的以為Timer.schedule(TimerTask task, long delay)就是重複的執行task。程序運行後發現隻運行了一次,總覺得是task裏的代碼有問題,花了很長時間調試代碼都沒有結果。

仔細研讀java api,發現:

schedule(TimerTask task, long delay)的注釋:Schedules the specified task for execution after the specified delay。大意是在延時delay毫秒後執行task。並沒有提到重複執行

schedule(TimerTask task, long delay, long period)的注釋:Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay。大意是在延時delay毫秒後重複的執行task,周期是period毫秒。

這樣問題就很明確schedule(TimerTask task, long delay)隻執行一次,schedule(TimerTask task, long delay, long period)才是重複的執行。關鍵的問題在於程序員誤以為schedule就是重複的執行,而沒有仔細的研究API,一方麵也是英文能力不夠,瀏覽API的過程中不能很快的理解到含義。

最後更新:2017-04-02 06:51:55

  上一篇:go new IO學習筆記(一)
  下一篇:go XSLT之計數循環