Android 定時任務高度【schedule】與【scheduleAtFixedRate】區別

來源:互聯網
上載者:User

標籤:android

在android中調度定時任務有兩種方法

1.schedule

2.scheduleAtFixedRate


這兩種方法的區別在於 首次調用時間(Date when)這個參數


<span style="font-family:SimHei;font-size:14px;">        // TimerTask        task = new TimerTask() {@Overridepublic void run() {// 輸出當前毫秒為單位的時間Log.i("Test", ">>>" + System.currentTimeMillis());}};        // 擷取毫秒為單位的目前時間long crt = System.currentTimeMillis();        timer = new Timer();        // 調度,以當前毫秒時間減10秒//        timer.schedule(task, new Date(crt - 10 * 1000), 2000);        timer.scheduleAtFixedRate(task, new Date(crt - 10 * 1000), 2000);</span>


schedule調度不會去計算首次調用時間  它會立即調用一次 在之後每2秒調用一次

scheduleAtFixedRate 調度則會計算首次調用時間,由於我們首次調用時間設定為目前時間減10秒,那麼在執行時會先計算出這10秒內應task應執行的次數,再去按設定頻率去執行。


聯繫我們

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