Java 定時(shí)器的多種實(shí)現(xiàn)方式
定時(shí)器有三種表現(xiàn)形式:
按固定周期定時(shí)執(zhí)行 延遲一定時(shí)間后執(zhí)行 指定某個(gè)時(shí)刻執(zhí)行JDK 提供了三種常用的定時(shí)器實(shí)現(xiàn)方式,分別為:
Timer DelayedQueue 延遲隊(duì)列 ScheduledThreadPoolExecutor(1)Timer發(fā)現(xiàn) eureka 中大量使用了 Timer 定時(shí)器:
Timer 屬于 JDK 比較早期版本的實(shí)現(xiàn),它可以實(shí)現(xiàn)固定周期的任務(wù),以及延遲任務(wù)。 Timer 會(huì)起動(dòng)一個(gè)異步線程去執(zhí)行到期的任務(wù),任務(wù)可以只被調(diào)度執(zhí)行一次,也可以周期性反復(fù)執(zhí)行多次。Timer 是如何使用的,示例代碼如下:
Timer timer = new Timer();timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() {// 業(yè)務(wù)代碼 }}, 5000, 5000); // 5s 后調(diào)度一個(gè)周期為 5s 的定時(shí)任務(wù) TimerTask 是實(shí)現(xiàn)了 Runnable 接口的抽象類 Timer 負(fù)責(zé)調(diào)度和執(zhí)行 TimerTask
Timer 的內(nèi)部構(gòu)造,如下:
public class Timer { // 小根堆,run操作 O(1)、新增 O(logn)、cancel O(logn) private final TaskQueue queue = new TaskQueue(); // 創(chuàng)建另外線程,任務(wù)處理,會(huì)輪詢 queue private final TimerThread thread = new TimerThread(queue); public Timer(String name) {thread.setName(name);thread.start(); }}
Timer 它是存在不少設(shè)計(jì)缺陷的,所以并不推薦用戶使用:
Timer 是單線程模式,如果某個(gè) TimerTask 執(zhí)行時(shí)間很久,會(huì)影響其他任務(wù)的調(diào)度。 Timer 的任務(wù)調(diào)度是基于系統(tǒng)絕對(duì)時(shí)間的,如果系統(tǒng)時(shí)間不正確,可能會(huì)出現(xiàn)問題。 TimerTask 如果執(zhí)行出現(xiàn)異常,Timer 并不會(huì)捕獲,會(huì)導(dǎo)致線程終止,其他任務(wù)永遠(yuǎn)不會(huì)執(zhí)行。(2)DelayedQueue 延遲隊(duì)列特征如下:
DelayedQueue 是 JDK 中一種可以延遲獲取對(duì)象的阻塞隊(duì)列,其內(nèi)部是采用優(yōu)先級(jí)隊(duì)列 PriorityQueue 存儲(chǔ)對(duì)象 DelayQueue 中的每個(gè)對(duì)象都必須實(shí)現(xiàn) Delayed 接口,并重寫 compareTo 和 getDelay 方法DelayedQueue 的使用方法如下:
public class DelayQueueTest { public static void main(String[] args) throws Exception {BlockingQueue<SampleTask> delayQueue = new DelayQueue<>();long now = System.currentTimeMillis();delayQueue.put(new SampleTask(now + 1000));delayQueue.put(new SampleTask(now + 2000));delayQueue.put(new SampleTask(now + 3000));for (int i = 0; i < 3; i++) { System.out.println(new Date(delayQueue.take().getTime()));} } static class SampleTask implements Delayed {long time;public SampleTask(long time) { this.time = time;}public long getTime() { return time;}@Overridepublic int compareTo(Delayed o) { return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));}@Overridepublic long getDelay(TimeUnit unit) { return unit.convert(time - System.currentTimeMillis(), TimeUnit.MILLISECONDS);} }}(3)ScheduledThreadPoolExecutor
JDK 提供了功能更加豐富的 ScheduledThreadPoolExecutor
public class ScheduledExecutorServiceTest { public static void main(String[] args) {ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);executor.scheduleAtFixedRate(() -> System.out.println('Hello World'), 1000, 2000, TimeUnit.MILLISECONDS); // 1s 延遲后開始執(zhí)行任務(wù),每 2s 重復(fù)執(zhí)行一次 }}
ScheduledThreadPoolExecutor 使用了阻塞隊(duì)列 DelayedWorkQueue。
(4)ScheduledThreadPoolExecutor線程應(yīng)該是最常見的實(shí)現(xiàn)方案,創(chuàng)建一個(gè)線程執(zhí)行任務(wù)即可,舉例幾個(gè)不同的寫法,代碼如下
4.1.使用thread + runnable
package com.yezi_tool.demo_basic.test;import org.springframework.stereotype.Component;import java.util.Date;@Componentpublic class ThreadTest { private Integer count = 0; public ThreadTest() {test1(); } public void test1() {new Thread(() -> { while (count < 10) {System.out.println(new Date().toString() + ': ' + count);count++;try { Thread.sleep(3000);} catch (InterruptedException e) { e.printStackTrace();} }}).start(); }}
4.2.使用線程池 + runnable
package com.yezi_tool.demo_basic.test;import org.springframework.stereotype.Component;import java.util.Date;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;@Componentpublic class ThreadTest { private static final ExecutorService threadPool = Executors.newFixedThreadPool(5);// 線程池 private Integer count = 0; public ThreadTest() {test2(); } public void test2() {threadPool.execute(() -> { while (count < 10) {System.out.println(new Date().toString() + ': ' + count);count++;try { Thread.sleep(3000);} catch (InterruptedException e) { e.printStackTrace();} }}); }}
以上就是Java 定時(shí)器的多種實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于Java 定時(shí)器的實(shí)現(xiàn)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼2. python 浮點(diǎn)數(shù)四舍五入需要注意的地方3. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法4. python開發(fā)一款翻譯工具5. 利用CSS制作3D動(dòng)畫6. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))7. Springboot 全局日期格式化處理的實(shí)現(xiàn)8. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題9. SpringBoot+TestNG單元測試的實(shí)現(xiàn)10. PHP實(shí)現(xiàn)簡單線性回歸之?dāng)?shù)學(xué)庫的重要性
