淺談java定時器的發(fā)展歷程
在開發(fā)中,我們經(jīng)常需要一些周期性的操作,例如每隔幾分鐘就進行某一項操作。這時候我們就要去設置個定時器,Java中最方便、最高效的實現(xiàn)方式是用java.util.Timer工具類,再通過調(diào)度java.util.TimerTask任務。
Timer是一種工具,線程用其安排以后在后臺線程中執(zhí)行的任務??砂才湃蝿請?zhí)行一次,或者定期重復執(zhí)行。實際上是個線程,定時調(diào)度所擁有的TimerTasks。
TimerTask是一個抽象類,它的子類由Timer安排為一次執(zhí)行或重復執(zhí)行的任務。實際上就是一個擁有run方法的類,需要定時執(zhí)行的代碼放到run方法體內(nèi)。
java在jdk1.3中推出了定時器類Timer,而后在jdk1.5后由DouLea從新開發(fā)出了支持多線程的ScheduleThreadPoolExecutor,從后者的表現(xiàn)來看,可以考慮完全替代Timer了。
Timer與ScheduleThreadPoolExecutor對比:
1.Timer始于jdk1.3,其原理是利用一個TimerTask數(shù)組當作隊列,將所有定時任務添加到此隊列里面去。然后啟動一個線程,當隊列為空時,此線程會阻塞,當隊列里面有數(shù)據(jù)時,線程會去除一個TimerTask來判斷
是否到時間需要運行此任務,如果運行時間小于或等于當前時間時則開始運行任務。由于其單線程的本質(zhì),所以會帶來幾個問題(詳細代碼在后面):
第一,當我們添加到定時器中的任務比較耗時時,由于此定時器是單線程順序執(zhí)行定時器任務,所以會影響后續(xù)任務的按時執(zhí)行。
Java代碼
//問題一示例: m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000); m_timer.scheduleAtFixedRate(new TaskNormal(), 5000, 3000); 運行結(jié)果: 14:44:29: timer is sleeping 10 seconds 14:44:39: Task Normal executed 14:44:39: timer is sleeping 10 seconds 14:44:49: Task Normal executed 14:44:49: Task Normal executed 14:44:49: timer is sleeping 10 seconds 結(jié)果分析:TaskNormal任務無法保證3秒運行一次,其只能等待TaskUseLongTime運行結(jié)束后才可以。
第二,Timer中的線程僅僅會捕獲InterruptedException異常,所以如果我們自定義的定時任務里面沒有捕獲可能出現(xiàn)的異常而導致異常拋出后,
//問題二示例: m_timer.schedule(new TaskThrowException(), 1000); m_timer.schedule(new TaskNormal(), 2000); 運行結(jié)果: 14:47:37: Throw exception Exception in thread "Timer-0" java.lang.RuntimeException at timer_test.TimerTest$TaskThrowException.run(TimerTest.java:85) at java.util.TimerThread.mainLoop(Timer.java:512) at java.util.TimerThread.run(Timer.java:462) 結(jié)果分析: 當前一個任務拋出異常后,后面的TaskNormal任務無法繼續(xù)運行
會導致我們的Timer線程停止,從而另后續(xù)的任務無法執(zhí)行。
第三,其無法處理多個同時發(fā)生的定時任務
//問題三示例:
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 15000);
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 15000);
運行結(jié)果:
14:50:16: timer1 is sleeping 10 seconds
14:50:26: timer2 is sleeping 10 seconds
14:50:36: timer2 is sleeping 10 seconds
結(jié)果分析:
我的啟動時間均是1秒以后,但是timer1和timer2啟動的時間明顯不一致
代碼示例:
package timer_test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest
{
private final Timer m_timer = new Timer();
public static void main(String[] args)
{
new TimerTest().test();
}
public void test()
{
//問題一示例:
m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000);
m_timer.scheduleAtFixedRate(new TaskNormal(), 5000, 3000);
//問題二示例:
// m_timer.schedule(new TaskThrowException(), 1000);
// m_timer.schedule(new TaskNormal(), 2000);
//問題三示例:
// m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 5000);
// m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 5000);
}
private class TaskUseLongTime extends TimerTask
{
private String m_taskName = "timer";
public TaskUseLongTime(){
}
public TaskUseLongTime(String taskName)
{
m_taskName = taskName;
}
@Override
public void run()
{
try
{
System.out.println(getCurrentTime()+": "+m_taskName+" is sleeping 10 seconds");
Thread.sleep(10000);
}
catch (InterruptedException e)
{
}
}
}
private class TaskNormal extends TimerTask
{
@Override
public void run()
{
System.out.println(getCurrentTime()+": Task Normal executed");
}
}
private class TaskThrowException extends TimerTask
{
@Override
public void run()
{
System.out.println(getCurrentTime()+": Throw exception");
throw new RuntimeException();
}
}
private String getCurrentTime()
{
return new SimpleDateFormat("HH:mm:ss").format(new Date());
}
}
2.ScheduleThreadPoolExecutor
ScheduleThreadPoolExecutor始于jdk1.5,是由DouLea先生編寫的,其利用ThreadPoolExecutor和DelayQueue巧妙的結(jié)合完成了多線程定時器的實現(xiàn),解決了Timer中由于單線程而導致的上述三個缺陷。
問題一中的問題是因為單線程順序執(zhí)行導致后續(xù)任務無法按時完成,我們看到多線程可以很容易的解決此問題,同時我們注意到TaskUseLongTime的執(zhí)行時間為10s(請看后續(xù)代碼),我們定時任務間隔是5秒,但是從結(jié)果中發(fā)現(xiàn)我們的任務執(zhí)行間隔卻是10秒,所以我們可以判斷ScheduleThreadPoolExecutor是采用每線程每任務的模式工作的。
//問題一: m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000, TimeUnit.MILLISECONDS); m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS); 運行結(jié)果: 14:54:37: Task Normal executed 14:54:37: timer is sleeping 10 seconds 14:54:42: Task Normal executed 14:54:47: Task Normal executed 14:54:47: timer is sleeping 10 seconds 14:54:52: Task Normal executed
問題二中我們發(fā)現(xiàn)當拋出異常的任務執(zhí)行后不影響其他任務的運行,同時我們發(fā)現(xiàn)在運行結(jié)果里面沒有將我們的異常拋出,這是因為ScheduleThreadPoolExecutor類在執(zhí)行完定時任務后會返回一個ScheduledFuture運行結(jié)果,不論結(jié)果是順利完成還是有異常均會保存在這里。
//問題二: m_timer.scheduleAtFixedRate(new TaskThrowException(), 1000, 5000, TimeUnit.MILLISECONDS); m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS); 運行結(jié)果: 14:58:36: Throw exception 14:58:36: Task Normal executed 14:58:41: Task Normal executed 14:58:46: Task Normal executed 14:58:51: Task Normal executed 14:58:56: Task Normal executed
問題三由于是多線程所以我們可以保證我們的定時任務可以同時執(zhí)行
//問題三:
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 5000, TimeUnit.MILLISECONDS);
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 5000, TimeUnit.MILLISECONDS);
運行結(jié)果:
15:01:12: timer1 is sleeping 10 seconds
15:01:12: timer2 is sleeping 10 seconds
15:01:22: timer2 is sleeping 10 seconds
15:01:22: timer1 is sleeping 10 seconds
15:01:32: timer1 is sleeping 10 seconds
15:01:32: timer2 is sleeping 10 seconds
詳細代碼:
package timer_test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ScheduleThreadPoolExecutorTest
{
private final ScheduledThreadPoolExecutor m_timer = new ScheduledThreadPoolExecutor(10);
public static void main(String[] args)
{
ScheduleThreadPoolExecutorTest timerTest = new ScheduleThreadPoolExecutorTest();
timerTest.test();
try
{
Thread.sleep(100000);
}
catch (InterruptedException e)
{
}
finally
{
timerTest.shutdown();
}
}
public void shutdown()
{
m_timer.shutdown();
}
public void test()
{
//問題一:
// m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000, TimeUnit.MILLISECONDS);
// m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS);
//問題二:
// m_timer.scheduleAtFixedRate(new TaskThrowException(), 1000, 5000, TimeUnit.MILLISECONDS);
// m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS);
//問題三:
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 5000, TimeUnit.MILLISECONDS);
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 5000, TimeUnit.MILLISECONDS);
}
private class TaskUseLongTime implements Callable<Integer>, Runnable
{
private String m_taskName = "timer";
private TaskUseLongTime(){
}
private TaskUseLongTime(String taskName)
{
m_taskName = taskName;
}
public void run()
{
try
{
System.out.println(getCurrentTime()+": "+m_taskName+" is sleeping 10 seconds");
Thread.sleep(10000);
}
catch (InterruptedException e)
{
}
}
public Integer call() throws Exception
{
run();
return 0;
}
}
@SuppressWarnings("unused")
private class TaskNormal implements Callable<Integer>, Runnable
{
public Integer call() throws Exception
{
run();
return 0;
}
public void run()
{
System.out.println(getCurrentTime()+": Task Normal executed");
}
}
@SuppressWarnings("unused")
private class TaskThrowException implements Callable<Integer>, Runnable
{
public Integer call() throws Exception
{
System.out.println(getCurrentTime()+": Throw exception");
throw new RuntimeException();
}
public void run()
{
System.out.println(getCurrentTime()+": Throw exception");
throw new RuntimeException();
}
}
private String getCurrentTime()
{
return new SimpleDateFormat("HH:mm:ss").format(new Date());
}
}
總結(jié)
以上就是本文關于淺談java定時器的發(fā)展歷程的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
- java 定時器線程池(ScheduledThreadPoolExecutor)的實現(xiàn)
- Javaweb 定時器功能代碼實例
- Java線程Timer定時器用法詳細總結(jié)
- Java自帶定時任務ScheduledThreadPoolExecutor實現(xiàn)定時器和延時加載功能
- 輕松實現(xiàn)Rxjava定時器功能
- 基于Rxjava實現(xiàn)輪詢定時器
- Java定時器Timer使用方法詳解
- RxJava2.x實現(xiàn)定時器的實例代碼
- Java定時器例子_動力節(jié)點Java學院整理
- java Quartz定時器任務與Spring task定時的幾種實現(xiàn)方法
- Java 定時器的使用示例
相關文章
springboot項目組引入JMeter的實現(xiàn)步驟
本文主要介紹了springboot項目組引入JMeter的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
詳解java CountDownLatch和CyclicBarrier在內(nèi)部實現(xiàn)和場景上的區(qū)別
這篇文章主要介紹了詳解java CountDownLatch和CyclicBarrier在內(nèi)部實現(xiàn)和場景上的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
解決Spring security5.5.7報錯Encoded password does
這篇文章主要介紹了解決Spring security5.5.7出現(xiàn)Encoded password does not look like BCrypt異常問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Java數(shù)組與二維數(shù)組及替換空格實戰(zhàn)真題講解
數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲固定大小的同類型元素,這篇文章主要介紹了Java數(shù)組與二維數(shù)組及替換空格實戰(zhàn)真題講解2022-07-07
解決Springboot項目中很多頁面出現(xiàn)Whitelabel Error Page(404)的問題
最近在接手的前后端項目中發(fā)現(xiàn)其默認路徑不是主機+端口(如:http://localhost:3453/)的形式,很多頁面的訪問是加了一個層級,只要訪問頁面就會出現(xiàn)Whitelabel Error Page(404),所以本文給大家提供了解決方案,需要的朋友可以參考下2024-02-02
java8新特性之stream的collect實戰(zhàn)教程
這篇文章主要介紹了java8新特性之stream的collect實戰(zhàn)教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

