Java定時任務schedule和scheduleAtFixedRate的異同_第1頁
Java定時任務schedule和scheduleAtFixedRate的異同_第2頁
Java定時任務schedule和scheduleAtFixedRate的異同_第3頁
Java定時任務schedule和scheduleAtFixedRate的異同_第4頁
Java定時任務schedule和scheduleAtFixedRate的異同_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第Java定時任務schedule和scheduleAtFixedRate的異同相同點

任務執(zhí)行未超時,則下次執(zhí)行時間=上次執(zhí)行開始時間+period任務執(zhí)行超時,則下次執(zhí)行時間=上次執(zhí)行結束時間

不同點

schedule在錯過以后會按照自己的節(jié)奏繼續(xù)走,可能會導致丟失任務,但時間間隔穩(wěn)定。scheduleAtFixedRate在錯過后,會努力追趕進度,以確保保持執(zhí)行頻率的穩(wěn)定。

代碼示例

我們進行一個簡單的測試,在下面的代碼中,每3秒就會有一個間隔5秒的線程休眠(模擬超時),對比一下二者輸出數據的區(qū)別。

packagecom;

importjava.text.ParseException;

importjava.text.SimpleDateFormat;

importjava.util.Date;

importjava.util.Timer;

importjava.util.TimerTask;

publicclassTimerTest{

publicstaticvoidmain(String[]args){

//任務此時已經啟動

Timert=newTimer();

TimerTasktask=newCustomTimerTask("test");

//添加任務

/*

*schedule保持間隔時間的穩(wěn)定

*scheduleAtFixedRate保持執(zhí)行頻率的穩(wěn)定

*/

t.schedule(task,newDate(),2000);

//

t.scheduleAtFixedRate(task,newDate(),2000);

}

classCustomTimerTaskextendsTimerTask{

privateStringname;

intcount=1;

privatefinalSimpleDateFormatdateFormatter=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");

publicCustomTimerTask(Stringname){

=name;

}

@Override

publicvoidrun(){

try{

System.out.println("線程:"+count);

System.out.println("startTime="+dateFormatter.format(newDate()));

if(count%3==0){

Thread.sleep(5000);

}

count++;

System.out.println("endTime="+dateFormatter.format(newDate())+"\r\n");

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

輸出數據對比

schedule

線程:1

startTime=2025-02-2510:20:28

endTime=2025-02-2510:20:28

線程:2

startTime=2025-02-2510:20:30

endTime=2025-02-2510:20:30

線程:3

startTime=2025-02-2510:20:32

endTime=2025-02-2510:20:37

線程:4

startTime=2025-02-2510:20:37

endTime=2025-02-2510:20:37

線程:5

startTime=2025-02-2510:20:39

endTime=2025-02-2510:20:39

線程:6

startTime=2025-02-2510:20:41

endTime=2025-02-2510:20:46

線程:7

startTime=2025-02-2510:20:46

endTime=2025-02-2510:20:46

線程:8

startTime=2025-02-2510:20:48

endTime=2025-02-2510:20:48

線程:9

startTime=2025-02-2510:20:50

endTime=2025-02-2510:20:55

線程:10

startTime=2025-02-2510:20:55

endTime=2025-02-2510:20:55

線程:11

startTime=2025-02-2510:20:57

endTime=2025-02-2510:20:57

可以看到,schedule在線程4的位置,它的開始時間是上一個線程的結束時間,它有在努力追趕,但后續(xù)并沒有把之前落下的線程補回來。

scheduleAtFixedRate

線程:1

startTime=2025-02-2510:18:59

endTime=2025-02-2510:18:59

線程:2

startTime=2025-02-2510:19:01

endTime=2025-02-2510:19:01

線程:3

startTime=2025-02-2510:19:03

endTime=2025-02-2510:19:08

線程:4

startTime=2025-02-2510:19:08

endTime=2025-02-2510:19:08

線程:5

startTime=2025-02-2510:19:08

endTime=2025-02-2510:19:08

線程:6

startTime=2025-02-2510:19:09

endTime=2025-02-2510:19:14

線程:7

startTime=2025-02-2510:19:14

endTime=2025-02-2510:19:14

線程:8

startTime=2025-02-2510:19:14

endTime=2025-02-2510:19:14

線程:9

startTime=2025-02-2510:19:15

endTime=2025-02-2510:19:20

線程:10

startTime=2025-02-2510:19:20

endTime=2025-02-2510:1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論