timer.schedule(new MyTask(),long time1,long timer2);
今天算是徹底的搞懂了這個曾經(jīng)讓我為之頭疼的方法。下面我就重點介紹一下:
附:
技術人員在實現(xiàn)內部辦公系統(tǒng)與外部網(wǎng)站一體化的時候,最重要的步驟就是從OA系統(tǒng)讀取數(shù)據(jù),并且根據(jù)網(wǎng)站模板生成最終的靜態(tài)頁面。這里就需要一個定時任務,循環(huán)的執(zhí)行。
技術人員在寫定時任務的時候,想當然的以為Timer.schedule(TimerTask task, longdelay)
就是重復的執(zhí)行task。程序運行后發(fā)現(xiàn)只運行了一次,總覺得是task里的代碼有問題,花了很長時間調試代碼都沒有結果。
仔細研讀java api
,發(fā)現(xiàn):
schedule(TimerTask task, long delay)
的注釋:Schedules thespecified task for execution after the specifieddelay。大意是在延時delay毫秒后執(zhí)行task。并沒有提到重復執(zhí)行
schedule(TimerTask task, long delay, long period)
的注釋:Schedulesthe specified task for repeated fixed-delay execution, beginningafter the specified delay。大意是在延時delay毫秒后重復的執(zhí)行task,周期是period毫秒。
這樣問題就很明確schedule(TimerTask task, longdelay)
只執(zhí)行一次,schedule(TimerTask task, long delay, longperiod)
才是重復的執(zhí)行。關鍵的問題在于程序員誤以為schedule就是重復的執(zhí)行,而沒有仔細的研究API,一方面也是英文能力不夠,瀏覽API的過程中不能很快的理解到含義。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VeVb武林網(wǎng)的支持。
新聞熱點
疑難解答
圖片精選