Android Timer schedule

來源:互聯網
上載者:User

標籤:

timer.schedule(new MyTask(),long time1,long timer2);

今天算是徹底的搞懂了這個以前讓我為之頭疼的方法。

以下我就重點介紹一下:

第一個參數。是 TimerTask 類,在包:import java.util.TimerTask .使用者要繼承該類,並實現 public void run() 方法,由於 TimerTask 類 實現了 Runnable 介面。

第二個參數的意思是,當你調用該方法後。該方法必定會調用 TimerTask 類 TimerTask 類 中的 run() 方法,這個參數就是這兩者之間的差值,轉換成漢語的意思就是說,使用者調用 schedule() 方法後,要等待這麼長的時間才幹夠第一次運行 run() 方法。

第三個參數的意思就是,第一次調用之後。從第二次開始每隔多長的時間調用一次 run() 方法。

[附:]

  技術人員在實現內部辦公系統與外部網站一體化的時候,最重要的步驟就是從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的過程中不能非常快的理解到含義。


Android Timer schedule

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.