需求:
由於系統長期運作,各設備之間產生很多信息,一段時間后需要清除數據
考慮方案:
用schedule還是scheduleAtFixedRate,在此比較分析了下這兩個的區別
schedule和scheduleAtFixedRate的區別在於,如果指定開始執行的時間在當前系統運行時間之前,scheduleAtFixedRate會把已經過去的時間也作為周期執行,而schedule不會把過去的時間算上。
schedule和scheduleAtFixedRate 區別:
(1) 2個參數的schedule在制定任務計划時, 如果指定的計划執行時間scheduledExecutionTime<= systemCurrentTime,則task會被立即執行。scheduledExecutionTime不會因為某一個task的過度執行而改變。
(2) 3個參數的schedule在制定反復執行一個task的計划時,每一次執行這個task的計划執行時間隨着前一次的實際執行時間而變,也就是 scheduledExecutionTime(第n+1次)=realExecutionTime(第n次)+periodTime。也就是說如果第n 次執行task時,由於某種原因這次執行時間過長,執行完后的systemCurrentTime>= scheduledExecutionTime(第n+1次),則此時不做時隔等待,立即執行第n+1次task,而接下來的第n+2次task的 scheduledExecutionTime(第n+2次)就隨着變成了realExecutionTime(第n+1次)+periodTime。說 白了,這個方法更注重保持間隔時間的穩定。
(3)3個參數的scheduleAtFixedRate在制定反復執行一個task的計划時,每一次 執行這個task的計划執行時間在最初就被定下來了,也就是scheduledExecutionTime(第n次)=firstExecuteTime +n*periodTime;如果第n次執行task時,由於某種原因這次執行時間過長,執行完后的systemCurrentTime>= scheduledExecutionTime(第n+1次),則此時不做period間隔等待,立即執行第n+1次task,而接下來的第n+2次的 task的scheduledExecutionTime(第n+2次)依然還是firstExecuteTime+(n+2)*periodTime這 在第一次執行task就定下來了。說白了,這個方法更注重保持執行頻率的穩定。
package TimerMG; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; /*** * [Schedule] * @author Visec丶Dana * schedule方法:“fixed-delay”; * 如果第一次執行時間被delay了,隨后的執行時間按 照 上一次 實際執行完成的時間點 進行計算 */ public class ScheduleWay { public static void main(String[] args) throws ParseException { final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = dateFormatter.parse("2014-11-14 10:30:00"); Timer timer = new Timer(); timer.schedule(new TimerTask(){ public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("execute task! "+ dateFormatter.format(this.scheduledExecutionTime())); } },startDate, 5 * 1000); } }
execute task! 2014-11-14 11:24:14 execute task! 2014-11-14 11:24:19 execute task! 2014-11-14 11:24:24 execute task! 2014-11-14 11:24:29
ScheduleAtFixed
package TimerMG; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; /** * [ScheduleAtFixed] * @author Visec丶Dana * fixed-rate;如果第一次執行時間被delay了, * 隨后的執行時間按照 上一次開始的 時間點 進行計算, * 並且為了”catch up”會多次執行任務,TimerTask中的執行體需要考慮同步 */ public class ScheduleAtFixedRateWay{ public static void main(String[] args) throws ParseException { final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = dateFormatter.parse("2014-11-14 10:30:00"); Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask(){ public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("execute task! " + dateFormatter.format(this.scheduledExecutionTime())); } },startDate,5*1000); } }
execute task! 2014-11-14 10:30:00 execute task! 2014-11-14 10:30:05 execute task! 2014-11-14 10:30:10 execute task! 2014-11-14 10:30:15 execute task! 2014-11-14 10:30:20