news 2026/4/23 13:30:48

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/4/23 11:18:40

分布式驱动汽车稳定性控制模型的探索与实现

分布式驱动汽车稳定性控制。 采用纯Simulink模型搭建,包括控制策略和车辆动力学模型。 采用分层式直接横摆力矩控制,上层包括模型预测MPC,滑模控制SMC,PID控制,LQR控制。 可灵活对四种控制器对比和选择。 另外下层基于…

作者头像 李华
网站建设 2026/4/23 10:39:18

基于springboot的多媒体素材库的开发与应用(11691)

有需要的同学,源代码和配套文档领取,加文章最下方的名片哦 一、项目演示 项目演示视频 二、资料介绍 完整源代码(前后端源代码SQL脚本)配套文档(LWPPT开题报告)远程调试控屏包运行 三、技术介绍 Java…

作者头像 李华
网站建设 2026/4/19 14:56:24

【远程协助】内网 IT 运维远程协助系统的最小可用架构

需求核心 运维需要随时查看终端画面并远程协助,但又不能使用公网工具,最小可用系统需要尽量简洁。最小可用架构 终端 Agent:屏幕采集 输入执行。运维控制台:浏览器或轻客户端。中继服务器:统一入口与权限控制。 必备功…

作者头像 李华