ScheduledThreadPoolExecutor

总结

  继承自 ThreadPoolExecutor 实现了 ScheduledExecutorService 接口

  提供了基于线程池的定时任务调度功能,允许你安排任务在未来某个时间点执行一次,或者周期性地重复执行。  

特性

  • 定时任务:可以安排任务在指定延迟后执行
  • 周期性任务:可以安排任务按照固定的时间间隔重复执行
  • 线程池管理:利用线程池来管理和复用线程,减少线程创建和销毁的开销。
  • 灵活的任务取消:可以通过返回的 Future 对象取消尚未开始的任务。

方法

  • schedule(Runnable command, long delay, TimeUnit unit):

    • 安排一个一次性任务,在给定的延迟之后执行
    • 返回一个 ScheduledFuture<?> 对象,可以用它来取消任务或检查任务的状态
  • schedule(Callable<V> callable, long delay, TimeUnit unit):

    • 安排一个带结果的一次性任务,在给定的延迟之后执行
    • 返回一个 ScheduledFuture<V> 对象,可以用它来获取任务的结果或取消任务
  • scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit):

    • 按照固定的速率周期性地执行任务。
    • 第一次执行会在初始延迟之后进行,然后每隔指定的时间间隔执行一次,无论上次任务是否完成。
  • scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit):

    • 按照固定的延迟周期性地执行任务。
    • 第一次执行会在初始延迟之后进行,然后在每次任务完成后等待指定的延迟时间再执行下一次任务。

示例  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
一次性任务:
 
public static void main(String[] args) {
        // 创建一个具有3个核心线程的ScheduledThreadPoolExecutor
        ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(3);
 
        // 在5秒后执行一个一次性任务
        executor.schedule(() -> {
            System.out.println("Task executed at: " + System.currentTimeMillis());
        }, 5, TimeUnit.SECONDS);
 
        // 关闭线程池
        executor.shutdown();
    }
 
 
固定速率执行的任务:
 
public static void main(String[] args) {
        // 创建一个具有3个核心线程的ScheduledThreadPoolExecutor
        ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(3);
 
        // 每隔2秒执行一次任务,初始延迟为1秒
        executor.scheduleAtFixedRate(() -> {
            System.out.println("Task executed at: " + System.currentTimeMillis());
        }, 1, 2, TimeUnit.SECONDS);
 
        // 运行一段时间后关闭线程池
        try {
            Thread.sleep(10000); // 让主线程休眠10秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        executor.shutdown();
    }
 
 
固定延迟执行的任务:
 
public static void main(String[] args) {
        // 创建一个具有3个核心线程的ScheduledThreadPoolExecutor
        ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(3);
 
        // 每次任务执行完毕后等待2秒再执行下一次任务,初始延迟为1秒
        executor.scheduleWithFixedDelay(() -> {
            System.out.println("Task started at: " + System.currentTimeMillis());
            try {
                Thread.sleep(3000); // 模拟耗时操作
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Task finished at: " + System.currentTimeMillis());
        }, 1, 2, TimeUnit.SECONDS);
 
        // 运行一段时间后关闭线程池
        try {
            Thread.sleep(15000); // 让主线程休眠15秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        executor.shutdown();
    }

  

 

posted on   anpeiyong  阅读(91)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2019-09-20 《计算机组成原理》---运算方法与运算器
2019-09-20 《计算机组成原理》---计算机中数据信息的表示
2019-09-20 《计算机组成原理》---概论

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示