news 2026/6/26 10:03:25

ScheduledExecutorService计划任务方法总结

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ScheduledExecutorService计划任务方法总结

一、常用方法

  • schedule

特性说明
✅ 单次执行schedule仅执行一次,不同于scheduleAtFixedRatescheduleWithFixedDelay的周期性执行。
✅ 异步执行任务在后台线程池中执行,不会阻塞调用线程。
✅ 支持返回值与异常使用Callable可获取结果或捕获异常(通过future.get())。
✅ 可取消在任务执行前可调用cancel()取消。
❌ 不保证精确时间实际执行时间受系统负载、线程调度等因素影响,只是近似延迟。
❌ 不自动重试若任务抛出异常,不会重试(与周期任务不同)。
private static void schedule(ScheduledExecutorService service, final int sleepTime){ service.schedule(new Runnable() { @Override public void run() { long start = new Date().getTime(); System.out.println("schedule 开始执行时间 " + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } long end = new Date().getTime(); System.out.println("schedule 执行花费时间= " + (end - start) / 1000+"s"); System.out.println("schedule 结束执行时间 " + DateFormat.getTimeInstance().format(new Date())); System.out.println(">>>>>>>>>>>>>>>>>>>"); } },5,TimeUnit.SECONDS); }

结论:只会执行一次,比较简单

  • scheduleAtFixedRate

延迟固定时间频率执行任务

任务执行耗时3s,period间隔为5s

scheduleAtFixedRate(service, 3000); private static void scheduleAtFixedRate(ScheduledExecutorService service, final int sleepTime) { service.scheduleAtFixedRate(new Runnable() { public void run() { long start = new Date().getTime(); System.out.println("scheduleAtFixedRate 开始执行时间 " + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // long end = System.currentTimeMillis(); long end = new Date().getTime(); System.out.println("scheduleAtFixedRate 执行花费时间= " + (end - start) / 1000); System.out.println("scheduleAtFixedRate 结束执行时间 " + DateFormat.getTimeInstance().format(new Date())); System.out.println(">>>>>>>>>>>>>>>>>>>"); } }, 1000, 5000, TimeUnit.MILLISECONDS); }

场景一:任务执行耗时时长小于period间隔,任务执行完成后,会等到了延迟时间,再执行下一次任务,本次任务开始时间+延迟时间间隔=下次任务开始时间

场景二:任务执行耗时时长大于period间隔,任务执行耗时5秒,period间隔为3秒,本次任务完成后,下次任务启动会立即执行.

scheduleAtFixedRate(service, 5000); private static void scheduleAtFixedRate(ScheduledExecutorService service, final int sleepTime) { service.scheduleAtFixedRate(new Runnable() { public void run() { long start = new Date().getTime(); System.out.println("scheduleAtFixedRate 开始执行时间 " + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // long end = System.currentTimeMillis(); long end = new Date().getTime(); System.out.println("scheduleAtFixedRate 执行花费时间= " + (end - start) / 1000+"s"); System.out.println("scheduleAtFixedRate 结束执行时间 " + DateFormat.getTimeInstance().format(new Date())); System.out.println(">>>>>>>>>>>>>>>>>>>"); } }, 1000, 3000, TimeUnit.MILLISECONDS); }

  • ​​​​​scheduleWithFixedDelay

scheduleWithFixedDelay(service, 2000); private static void scheduleWithFixedDelay(ScheduledExecutorService service, final int sleepTime) { service.scheduleWithFixedDelay(new Runnable() { public void run() { long start = new Date().getTime(); System.out.println("scheduleWithFixedDelay 开始执行时间:" + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } long end = new Date().getTime(); System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "s"); System.out.println("scheduleWithFixedDelay执行完成时间:" + DateFormat.getTimeInstance().format(new Date())); System.out.println("======================================"); } }, 1000, 3000, TimeUnit.MILLISECONDS); }

场景一:任务执行耗时小于period间隔,任务执行耗时2秒,period间隔为3秒

结论:本次任务执行结束时间+period=下次任务开始时间

scheduleWithFixedDelay(service, 5000); private static void scheduleWithFixedDelay(ScheduledExecutorService service, final int sleepTime) { service.scheduleWithFixedDelay(new Runnable() { public void run() { long start = new Date().getTime(); System.out.println("scheduleWithFixedDelay 开始执行时间:" + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } long end = new Date().getTime(); System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "s"); System.out.println("scheduleWithFixedDelay执行完成时间:" + DateFormat.getTimeInstance().format(new Date())); System.out.println("======================================"); } }, 1000, 3000, TimeUnit.MILLISECONDS); }

场景一:任务执行耗时大于period间隔,任务执行耗时5秒,period间隔为3秒

结论:本次任务执行结束时间+period=下次任务开始时间,delay是计算上一个任务执行结束的时间和本次任务开始时间的差值,此值和任务的执行时间就没有关系了

特性scheduleAtFixedRatescheduleWithFixedDelay
间隔基准上次开始时间上次结束时间
适用场景需要严格频率(如每5秒上报一次)需要固定空闲间隔(如每次处理完等3秒再处理)
任务超时影响可能导致任务“追赶”甚至连续执行始终保证任务间有 delay 间隔
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/15 18:02:42

含可再生能源的配电网最佳空调负荷优化控制Matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。 🍎 往期回顾关注个人主页:Matlab科研工作室 👇 关注我领取海量matlab电子书和数学建模资料 &#x1…

作者头像 李华
网站建设 2026/6/22 20:54:37

计算机深度学习毕设实战-基于python-cnn深度学习的是否有污渍识别基于python机器学习深度学习的是否有污渍识别

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

作者头像 李华
网站建设 2026/6/23 17:26:29

pytorch深度学习笔记16

目录 摘要 1.AdaGrad 2.RMSProp 3.Adam 摘要 本篇文章继续学习尚硅谷深度学习教程,学习内容是AdaGrad,​​​​​​​RMSProp,Adam 1.AdaGrad AdaGrad(Adaptive Gradient,自适应梯度)会为每个参数适当…

作者头像 李华
网站建设 2026/6/15 11:41:24

如何用云服务器搭建PUBG服务器?

云服务器搭建PUBG服务器完整指南一、服务器配置要求硬件配置推荐根据PUBG游戏的性能需求,建议选择以下配置:最低配置:CPU:Intel Core i5-4430 / AMD FX-6300内存:8GB RAM存储:50GB可用空间(推荐…

作者头像 李华