当前位置:网站首页>ScheduledExecutorService - 定时周期执行任务
ScheduledExecutorService - 定时周期执行任务
2022-07-31 07:50:00 【CWeQin】
项目需求
定时调用报警器,对数据进行报警判断
技术选型
单机定时任务技术
- Timer
- ScheduledExecutorService
- Spring Task
分布式定时任务技术
- Quartz
- Elastic-Job
- XXL-JOB
- PowerJob
程序在单机下运行,故只考虑单机定时任务。前期考虑使用 Timer不过其缺陷较多,比如一个 Timer 一个线程,这就导致 Timer 的任务的执行只能串行执行,一个任务执行时间过长的话会影响其他任务(性能非常差),再比如发生异常时任务直接停止(Timer 只捕获了 InterruptedException )。
ScheduledExecutorService使用
ScheduledThreadPoolExecutor继承自ThreadPoolExecutor。它主要用来在给定的延迟之后运 行任务,或者定期执行任务。ScheduledThreadPoolExecutor的功能与Timer类似,但 ScheduledThreadPoolExecutor功能更强大、更灵活。Timer对应的是单个后台线程,而 ScheduledThreadPoolExecutor可以在构造函数中指定多个对应的后台线程数。
ScheduledExecutorService接口是java线程池中最重要的几个接口之一。
它除了支持原生线程池的功能之外,同时支持定时任务处理的功能。
在JDK中为它提供了一个默认的实现类:ScheduledThreadPoolExecutor。
ScheduledExecutorService包括三个方法:schedule()、scheduleAtFixedRate()、scheduleWithFixedDelay()。
schedule()方法
定义:在给定的延时之后,创建并执行一个启动一次性动作。
/** * * @param command the task to execute 要执行的任务 * @param delay the time from now to delay execution 从现在开始推迟执行的时间 * @param unit the time unit of the delay parameter 延迟参数的时间单位 **/
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);
scheduleAtFixedRate()方法 (以固定周期运行)
相比于schedule()方法,多了一个参数period,可以指定周期执行。一个周期 = 任务执行完成的时间 + 两次任务的间隔时间
/** * * @param command the task to execute 要执行的任务 * @param initialDelay the time to delay first execution 从现在开始推迟执行的时间 * @param period the period between successive executions 延迟首次执行的时间 * @param unit the time unit of the initialDelay and period parameters 延迟参数的时间单位 * **/
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
ScheduleWithFixedDelay()方法
/** * @param command the task to execute 要执行的任务 * @param initialDelay the time to delay first execution 从现在开始推迟执行的时间 * @param delay the delay between the termination of one * execution and the commencement of the next 一个执行的终止和下一个执行的开始之间的延迟 * @param unit the time unit of the initialDelay and delay parameters 延迟参数的时间单位 **/
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
参考文章:
Java多线程学习(八)线程池与Executor 框架
Spring Job?Quartz?XXL-Job?年轻人才做选择,艿艿全莽~
边栏推荐
- "C language game" entry-level chess game (robot enhanced version)
- 关于Error EPERM operation not permitted, mkdir...几种解决办法的比较
- linux redis6.2.6配置文件
- PowerCLi 一键批量部署OVA 到esxi 7
- 【MySQL功法】第3话 · MySQL中常见的数据类型
- MySQL中InnoDB的多版本并发控制(MVCC)的实现
- PowerCLi 通过自建PXE Server一键esxi7下批量部署常规New-VM
- SSM整合案例分析(详解)
- 【C#】判断字符串中是否包含指定字符或字符串(Contains/IndexOf)
- Ubuntu安装Mysql5.7
猜你喜欢
随机推荐
基于golang的swagger超贴心、超详细使用指南【有很多坑】
[Interview: Concurrency 38: Multithreading: Thread Pool] Basic concepts of the ThreadPoolExecutor class
关于“算力”,这篇文章值得一看
正则表达式绕过
Small application project development, jingdong mall 】 【 uni - app custom search component (below) - search history
Ceph单节点部署
《c语言小游戏》入门级三子棋游戏(机器人加强版)
《c语言》青蛙跳台阶递归问题
[Interview: Concurrency 37: Multithreading: Thread Pool] Custom Thread Pool
MySQL installation to the last step in the write the configuration file failed?And after the installation steps
Reimbursement Process | By Tianfang
[MySQL exercises] Chapter 4 · Explore operators in MySQL with kiko
mysql插入新字段方法
C语言三子棋(井字棋)小游戏
《opencv学习笔记》-- 仿射变换
免安装版的Mysql安装与配置——详细教程
Collation and sharing of related classic papers and datasets in the field of deep learning communication
【idea 报错】 无效的目标发行版:17 的解决参考
重装系统后,hosts文件配置后不生效
SQLAlchemy使用教程
![[Interview: Concurrency 37: Multithreading: Thread Pool] Custom Thread Pool](/img/61/71131414c48bb77aa9160b61a68811.png)
![[MySQL exercises] Chapter 2 Basic operations of databases and data tables](/img/43/73a59a293d4708b6f9aeae990a7029.png)






