溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

淺談java定時(shí)器的發(fā)展歷程

發(fā)布時(shí)間:2020-09-28 18:56:08 來源:腳本之家 閱讀:138 作者:hacksin 欄目:編程語言

在開發(fā)中,我們經(jīng)常需要一些周期性的操作,例如每隔幾分鐘就進(jìn)行某一項(xiàng)操作。這時(shí)候我們就要去設(shè)置個(gè)定時(shí)器,Java中最方便、最高效的實(shí)現(xiàn)方式是用java.util.Timer工具類,再通過調(diào)度java.util.TimerTask任務(wù)。

Timer是一種工具,線程用其安排以后在后臺(tái)線程中執(zhí)行的任務(wù)。可安排任務(wù)執(zhí)行一次,或者定期重復(fù)執(zhí)行。實(shí)際上是個(gè)線程,定時(shí)調(diào)度所擁有的TimerTasks。

TimerTask是一個(gè)抽象類,它的子類由Timer安排為一次執(zhí)行或重復(fù)執(zhí)行的任務(wù)。實(shí)際上就是一個(gè)擁有run方法的類,需要定時(shí)執(zhí)行的代碼放到run方法體內(nèi)。

java在jdk1.3中推出了定時(shí)器類Timer,而后在jdk1.5后由DouLea從新開發(fā)出了支持多線程的ScheduleThreadPoolExecutor,從后者的表現(xiàn)來看,可以考慮完全替代Timer了。

Timer與ScheduleThreadPoolExecutor對(duì)比:

1.Timer始于jdk1.3,其原理是利用一個(gè)TimerTask數(shù)組當(dāng)作隊(duì)列,將所有定時(shí)任務(wù)添加到此隊(duì)列里面去。然后啟動(dòng)一個(gè)線程,當(dāng)隊(duì)列為空時(shí),此線程會(huì)阻塞,當(dāng)隊(duì)列里面有數(shù)據(jù)時(shí),線程會(huì)去除一個(gè)TimerTask來判斷

是否到時(shí)間需要運(yùn)行此任務(wù),如果運(yùn)行時(shí)間小于或等于當(dāng)前時(shí)間時(shí)則開始運(yùn)行任務(wù)。由于其單線程的本質(zhì),所以會(huì)帶來幾個(gè)問題(詳細(xì)代碼在后面):

第一,當(dāng)我們添加到定時(shí)器中的任務(wù)比較耗時(shí)時(shí),由于此定時(shí)器是單線程順序執(zhí)行定時(shí)器任務(wù),所以會(huì)影響后續(xù)任務(wù)的按時(shí)執(zhí)行。

Java代碼

//問題一示例: 
m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000); 
m_timer.scheduleAtFixedRate(new TaskNormal(), 5000, 3000); 
 
運(yùn)行結(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任務(wù)無法保證3秒運(yùn)行一次,其只能等待TaskUseLongTime運(yùn)行結(jié)束后才可以。 

第二,Timer中的線程僅僅會(huì)捕獲InterruptedException異常,所以如果我們自定義的定時(shí)任務(wù)里面沒有捕獲可能出現(xiàn)的異常而導(dǎo)致異常拋出后,

//問題二示例: 
m_timer.schedule(new TaskThrowException(), 1000); 
m_timer.schedule(new TaskNormal(), 2000); 
 
運(yùn)行結(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é)果分析: 
當(dāng)前一個(gè)任務(wù)拋出異常后,后面的TaskNormal任務(wù)無法繼續(xù)運(yùn)行 

會(huì)導(dǎo)致我們的Timer線程停止,從而另后續(xù)的任務(wù)無法執(zhí)行。

第三,其無法處理多個(gè)同時(shí)發(fā)生的定時(shí)任務(wù)

//問題三示例: 
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 15000); 
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 15000); 
 
運(yùn)行結(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é)果分析: 
我的啟動(dòng)時(shí)間均是1秒以后,但是timer1和timer2啟動(dòng)的時(shí)間明顯不一致 

代碼示例:

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é)合完成了多線程定時(shí)器的實(shí)現(xiàn),解決了Timer中由于單線程而導(dǎo)致的上述三個(gè)缺陷。

問題一中的問題是因?yàn)閱尉€程順序執(zhí)行導(dǎo)致后續(xù)任務(wù)無法按時(shí)完成,我們看到多線程可以很容易的解決此問題,同時(shí)我們注意到TaskUseLongTime的執(zhí)行時(shí)間為10s(請(qǐng)看后續(xù)代碼),我們定時(shí)任務(wù)間隔是5秒,但是從結(jié)果中發(fā)現(xiàn)我們的任務(wù)執(zhí)行間隔卻是10秒,所以我們可以判斷ScheduleThreadPoolExecutor是采用每線程每任務(wù)的模式工作的。

//問題一: 
m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000, TimeUnit.MILLISECONDS); 
m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS); 
 
運(yùn)行結(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)當(dāng)拋出異常的任務(wù)執(zhí)行后不影響其他任務(wù)的運(yùn)行,同時(shí)我們發(fā)現(xiàn)在運(yùn)行結(jié)果里面沒有將我們的異常拋出,這是因?yàn)镾cheduleThreadPoolExecutor類在執(zhí)行完定時(shí)任務(wù)后會(huì)返回一個(gè)ScheduledFuture運(yùn)行結(jié)果,不論結(jié)果是順利完成還是有異常均會(huì)保存在這里。

//問題二: 
m_timer.scheduleAtFixedRate(new TaskThrowException(), 1000, 5000, TimeUnit.MILLISECONDS); 
m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS); 
 
運(yùn)行結(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 

問題三由于是多線程所以我們可以保證我們的定時(shí)任務(wù)可以同時(shí)執(zhí)行

//問題三: 
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 5000, TimeUnit.MILLISECONDS); 
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 5000, TimeUnit.MILLISECONDS); 
 
運(yùn)行結(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 

詳細(xì)代碼:

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é)

以上就是本文關(guān)于淺談java定時(shí)器的發(fā)展歷程的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的定時(shí)器代碼解析

Java多線程定時(shí)器Timer原理及實(shí)現(xiàn)

java定時(shí)器timer的使用方法代碼示例

如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI