
线程池-----ScheduledExecutorService实现定时任务
发布日期:2021-05-07 10:02:36
浏览次数:15
分类:精选文章
本文共 2831 字,大约阅读时间需要 9 分钟。
接口scheduleAtFixedRate原型定义及参数说明:
public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
command:执行线程;
initialDelay:初始化延时; period:两次开始执行最小间隔时间; unit:计时单位;1、使用scheduleAtFixedRate()方法实现周期性执行
如:每隔100毫秒,执行一次run任务,得到执行后的打印::
import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class ScheduledExecutorServiceTest { public static void main(String[] args) { ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("run "+ System.currentTimeMillis()); } }, 0, 100, TimeUnit.MILLISECONDS); }
2、使用scheduleAtFixedRate()方法实现周期性定时任务
有时候我们希望一个任务被安排在凌晨3点(访问较少时)周期性的执行一个比较耗费资源的任务,可以使用下面方法设定每天在固定时间执行一次任务。
import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;@Componentpublic class ExecutorTest { public static final ScheduledExecutorService service = Executors.newScheduledThreadPool(4); public static void main(String[] args) { long oneDay = 24 * 60 * 60 * 1000; long initDelay = getTimeMillis("03:00:00") - System.currentTimeMillis(); initDelay = initDelay > 0 ? initDelay : oneDay + initDelay; service.scheduleAtFixedRate(new MyScheduled1(),initDelay,oneDay, TimeUnit.MILLISECONDS); service.scheduleAtFixedRate(new MyScheduled2(),initDelay,oneDay,TimeUnit.MILLISECONDS); }/** * 获取指定时间对应的毫秒数 * @param time "HH:mm:ss" * @return */private static long getTimeMillis(String time) { try { DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd"); Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time); return curDate.getTime(); } catch (ParseException e) { e.printStackTrace(); } return 0;
自定义线程:
public class MyScheduled1 implements Runnable{ @Override public void run() { System.out.println("my scheduled 01-----"); }}public class MyScheduled2 implements Runnable { @Override public void run() { System.out.println("my scheduled 02 ------"); }}
注意与另一个接口的区别:
接口scheduleWithFixedDelay原型定义及参数说明
public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
command:执行线程
initialDelay:初始化延时 period:前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间) unit:计时单位由此可见,ScheduleAtFixedRate 是基于固定时间间隔进行任务调度,ScheduleWithFixedDelay 取决于每次任务执行的时间长短,是基于不固定时间间隔进行任务调度
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月08日 07时02分15秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Vue之Element标签页保留用户操作缓存。
2019-03-05
智能合约开发实践(1)
2019-03-05
MATLAB——操作矩阵的常用函数
2019-03-05
CMake自学记录,看完保证你知道CMake怎么玩!!!
2019-03-05
ORB-SLAM2:LoopClosing线程学习随笔【李哈哈:看看总有收获篇】
2019-03-05
牛客练习赛56 D 小翔和泰拉瑞亚(线段树)
2019-03-05
NC15553 数学考试(线性DP)
2019-03-05
MySQL隐藏文件.mysql_history风险
2019-03-05
js求阶乘
2019-03-05
小程序图片正确使用方式(防止发布之后不显示)
2019-03-05
Java学习
2019-03-05
Js函数
2019-03-05
L1-009 N个数求和 (20 分)
2019-03-05
L2-031 深入虎穴 (25 分)
2019-03-05
Unity之PlayerPrefs
2019-03-05
简单的xml读取存储方法(未优化)
2019-03-05
Nginx---惊群
2019-03-05