首頁 > 軟體

Java 定時器的多種實現方式

2021-06-07 10:01:10

一、前言

定時器有三種表現形式:

  • 按固定週期定時執行
  • 延遲一定時間後執行
  • 指定某個時刻執行

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 介面,並重寫 compareTogetDelay 方法

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 延遲後開始執行任務,每 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;

@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 定時器的多種實現方式的詳細內容,更多關於Java 定時器的實現的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com