Java 定時(shí)器的多種實(shí)現(xiàn)方式
一、前言
定時(shí)器有三種表現(xiàn)形式:
- 按固定周期定時(shí)執(zhí)行
- 延遲一定時(shí)間后執(zhí)行
- 指定某個(gè)時(shí)刻執(zhí)行
JDK 提供了三種常用的定時(shí)器實(shí)現(xiàn)方式,分別為:
TimerDelayedQueue延遲隊(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;
}
@Override
public int compareTo(Delayed o) {
return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));
}
@Override
public 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;
@Component
public 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;
@Component
public 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)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring-boot整合ehcache實(shí)現(xiàn)緩存機(jī)制的方法
spring-boot是一個(gè)快速的集成框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過(guò)程。這篇文章主要介紹了spring-boot整合ehcache實(shí)現(xiàn)緩存機(jī)制,需要的朋友可以參考下2018-01-01
java調(diào)用webService接口的代碼實(shí)現(xiàn)
本文主要介紹了java調(diào)用webService接口的代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Java中SPI機(jī)制的實(shí)現(xiàn)詳解
SPI(Service?Provider?Interface),是?JDK?內(nèi)置的一種服務(wù)提供發(fā)現(xiàn)機(jī)制,可以用來(lái)啟用框架擴(kuò)展和替換組件,下面我們就來(lái)看看Java中SPI機(jī)制的具體實(shí)現(xiàn)2024-01-01
JAVA中數(shù)組從小到大排序的2種方法實(shí)例
JAVA中在運(yùn)用數(shù)組進(jìn)行排序功能時(shí)一般有多種解決方案,下面這篇文章主要給大家介紹了關(guān)于JAVA中數(shù)組從小到大排序的2種方法,文中都給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2023-03-03
Java JDK與cglib動(dòng)態(tài)代理有什么區(qū)別
這篇文章主要介紹了Java JDK動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理的區(qū)別文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-03-03

