午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

Java 定時器的多種實現方式

瀏覽:115日期:2022-08-11 10:08:35
目錄一、前言(1)Timer(2)DelayedQueue 延遲隊列(3)ScheduledThreadPoolExecutor(4)ScheduledThreadPoolExecutor一、前言

定時器有三種表現形式:

按固定周期定時執行 延遲一定時間后執行 指定某個時刻執行

JDK 提供了三種常用的定時器實現方式,分別為:

Timer DelayedQueue 延遲隊列 ScheduledThreadPoolExecutor(1)Timer

發現 eureka 中大量使用了 Timer 定時器:

Timer 屬于 JDK 比較早期版本的實現,它可以實現固定周期的任務,以及延遲任務。 Timer 會起動一個異步線程去執行到期的任務,任務可以只被調度執行一次,也可以周期性反復執行多次。

Timer 是如何使用的,示例代碼如下:

Timer timer = new Timer();timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() {// 業務代碼 }}, 5000, 5000); // 5s 后調度一個周期為 5s 的定時任務 TimerTask 是實現了 Runnable 接口的抽象類 Timer 負責調度和執行 TimerTask

Timer 的內部構造,如下:

public class Timer { // 小根堆,run操作 O(1)、新增 O(logn)、cancel O(logn) private final TaskQueue queue = new TaskQueue(); // 創建另外線程,任務處理,會輪詢 queue private final TimerThread thread = new TimerThread(queue); public Timer(String name) {thread.setName(name);thread.start(); }}

Timer 它是存在不少設計缺陷的,所以并不推薦用戶使用:

Timer 是單線程模式,如果某個 TimerTask 執行時間很久,會影響其他任務的調度。 Timer 的任務調度是基于系統絕對時間的,如果系統時間不正確,可能會出現問題。 TimerTask 如果執行出現異常,Timer 并不會捕獲,會導致線程終止,其他任務永遠不會執行。(2)DelayedQueue 延遲隊列

特征如下:

DelayedQueue 是 JDK 中一種可以延遲獲取對象的阻塞隊列,其內部是采用優先級隊列 PriorityQueue 存儲對象 DelayQueue 中的每個對象都必須實現 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 延遲后開始執行任務,每 2s 重復執行一次 }}

ScheduledThreadPoolExecutor 使用了阻塞隊列 DelayedWorkQueue。

(4)ScheduledThreadPoolExecutor

線程應該是最常見的實現方案,創建一個線程執行任務即可,舉例幾個不同的寫法,代碼如下

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 定時器的多種實現方式的詳細內容,更多關于Java 定時器的實現的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 老色批av | 久久国产高清 | 天天操天天干天天插 | 一二三av | 成人黄色小视频在线观看 | 国产精品国产三级国产aⅴ 国产三级福利 | 亚洲专区在线播放 | 欧美黄色精品 | 秋霞成人av | 在线视频中文字幕 | 欧美三级国产 | 久久久免费看片 | 色视频一区 | 香蕉视频成人在线观看 | 亚洲少妇一区二区三区 | 午夜啊啊啊 | 色小妹av | 亚洲成人精品一区二区 | 懂色av懂色av粉嫩av分享吧 | 精品国产一区二区三区在线观看 | 久久一级黄色片 | 国产日韩欧美日韩大片 | 国产成人久久久 | 久久狠狠高潮亚洲精品 | 91高跟黑色丝袜呻吟在线观看 | 欧美五月婷婷 | 在线看一区二区 | av永久在线 | 久久艹在线观看 | 国产精品毛片va一区二区三区 | 亚洲午夜久久久久久久久久久 | 欧美另类在线观看 | 亚洲一区二区av在线 | 成人自拍一区 | 中国a一片一级一片 | 一区二区视频免费 | 男女爽爽爽免费视频 | 嫩草在线观看视频 | 欧美91 | 欧美精品免费一区二区三区 | 亚洲日本视频 |